I am confused and Im not sure what to do. The code below is stuff that I jotted down as I was thinking about the solution.
here is the question with example:
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1 'B' -> 2 ... 'Z' -> 26
Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
Input: "12" Output: 2 Explanation: It could be decoded as "AB" (1 2) or "L" (12). Example 2: Input: "226" Output: 3 Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
const numDecodings = (s) => {
// const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
// //write an object with alphabets. The numbers will be keys and the letters will be value
const alphaObject = {
1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i',
10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q',
18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'
};
const countObj = {};
// console.log()
if (s >= "11") {
s.split("");
if (!countObj[alphaObject[s]]) {
return (countObj[alphaObject[s]] = 1);
}
//i return the length because
//i noticed from the examples that the outputs matched the length.
//since every digit represents a letter.
//And every letter is a way to do the problem
} else if (s <= "10") {
return 1;
} else if (s === "0") {
return 0;
}
// else if(s.length >= 4){
// //test case 1223
// //abbc
// //lw
// //abw
// //lbc
// //avc
// // return s.length + 2
// s.alphaObject
// }
};
//getting it wrong for testcase 0
console.log(numDecodings("14"));
