Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

s =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

DP over positions: decode one digit or two digits when valid.

Approach

Let dp[i] = ways to decode prefix ending at i. Transition:

  • If s[i] != '0', add dp[i-1].
  • If 10 <= int(s[i-1..i]) <= 26, add dp[i-2]. Use rolling variables for O(1) space.

Why it works

Any valid decoding ends with either a single-letter code or a two-letter code.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)
Solution
Updated: 2026-02-23

Idea

DP over positions: decode one digit or two digits when valid.

Approach

Let dp[i] = ways to decode prefix ending at i. Transition:

  • If s[i] != '0', add dp[i-1].
  • If 10 <= int(s[i-1..i]) <= 26, add dp[i-2]. Use rolling variables for O(1) space.

Why it works

Any valid decoding ends with either a single-letter code or a two-letter code.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)