Skip to content

Conversation

Siddhram
Copy link

@Siddhram Siddhram commented Oct 13, 2025

This PR introduces a complete and well-documented implementation of the Decode Ways problem in R.

Overview

The implementation provides solutions for counting the number of ways to decode a numeric string. It includes a classic tabulation approach and two O(1)-space variants (forward and backward), along with concise examples and explanatory comments.

Features

  • Three approaches:
    • Tabulation DP: O(n) time, O(n) space
    • Forward O(1) DP: uses two rolling states (Fibonacci-style)
    • Backward O(1) DP: iterates from right to left
  • Proper handling of zeros ("0" invalid unless part of "10" or "20")
  • Helper function for two-digit validity (11–19, 21–26)
  • Embedded examples printing results from all three methods
  • Consistent structure and comments with other dynamic_programming scripts

Complexity

  • Time Complexity: O(n)
  • Space Complexity:
    • O(1) for forward/backward DP
    • O(n) for tabulation

Directory

  • Updated DIRECTORY.md to list “Decode Ways” under Dynamic Programming

Demonstration

Run the script to execute built-in examples:

From Windows command line:

Rscript "R/dynamic_programming/decode_ways.r"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant