Decode Ways
Given a string ‘strNum’ which represents a number, your task is to find the ways to decode the given string ‘strNum’.
The format of encoding is as follows: ‘A’ - 1, ‘B’ - 2, ‘C’ - 3, ‘D’ - 4, ……………, ‘Z’ - 26.
Encoding is possible in letters from ‘A’ to ‘Z’. There is an encoding between character and number.
Example :
‘n = 226’ so we can decode ‘226’ in such a way-
‘BZ = 2-26’, as B maps to 2 and Z maps to 26.
‘BBF = 2-2-6’
‘VF = 22-6’
‘226; can be decoded in three ‘BZ’, ‘BBF’, ‘VF’ possible ways.
Point to be noticed we can’t decode ‘226’ as ‘226’ because we have no character which can directly map with ‘226’ we can only decode numbers from ‘1’ to ‘26’ only.
Input format :
The first line of input contains an integer ‘T’ denoting the number of test cases. The 'T' test cases follow.
The first line of each test case contains a single string ‘strNum’.
Output Format :
For each test case, print an integer denoting the ways of decodes.
Note :
1. You do not need to print anything, it has already been taken care of. Just implement the given function.
2. Ways of decodes can be very high, so return answer modulo 10^9+7.
Constraints :
1 <= T <= 50
1 <= N <= 10^4
Time limit: 1 second
CodingNinjas
author
2y
Brute Force
The basic idea is that, try to use each and every possible combination.
- Use a function ‘decodeWaysHelper(strNum,n)’, where ‘strNum’ is a given number as a string, ‘n’ is the size of ‘strNum...read more
CodingNinjas
author
2y
Dynamic Programming
The basic idea is that, if the given number is 'xyz19' then the possible answer can be the answer of('xyz') use '19' as another character(‘s’) and answer of ( 'xyz1' ) use '9' as an...read more
Help your peers!
Add answer anonymously...
Top Borderfree Mern Stack Developer interview questions & answers
Popular interview questions of Mern Stack Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app