Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

175. Decode Ways

MediumDay 51 of 60Dynamic Programming

A message of letters A to Z was encoded as numbers using "A" -> "1", "B" -> "2", ..., "Z" -> "26". Given the digit string s, return the number of ways to decode it back into letters. A group cannot start with 0, so "06" is not a valid code for F. Return 0 if no decoding exists.

Example 1
Input: s = "12"
Output: 2
"AB" (1, 2) or "L" (12).
Example 2
Input: s = "226"
Output: 3
"BZ" (2, 26), "VF" (22, 6) or "BBF" (2, 2, 6).
Example 3
Input: s = "06"
Output: 0
A code cannot start with 0.
Constraints
  • 1 <= s.length <= 100
  • s contains only digits and may contain leading zeros
Hints
Frequently asked at: Meta, Amazon, Microsoft, Google
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min