Skip to content

Commit

Permalink
Num Decodings
Browse files Browse the repository at this point in the history
  • Loading branch information
hyejjun committed Aug 23, 2024
1 parent d6b6770 commit c67f669
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions decode-ways/hyejjun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @param {string} s
* @return {number}
*/

var numDecodings = function (s) {
if (s == null || s.length === 0) {
return 0;
}

const n = s.length;
const dp = new Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = s[0] === '0' ? 0 : 1;

for (let i = 2; i <= n; i++) {
const oneDigit = parseInt(s.substring(i - 1, i));
const twoDigits = parseInt(s.substring(i - 2, i));

// Check if single digit decoding is possible
if (oneDigit >= 1 && oneDigit <= 9) {
dp[i] += dp[i - 1];
}

// Check if two digit decoding is possible
if (twoDigits >= 10 && twoDigits <= 26) {
dp[i] += dp[i - 2];
}
}

return dp[n];
};


console.log(numDecodings("12"));
console.log(numDecodings("226"));
console.log(numDecodings("06"));

/*
시간 복잡도: O(n)
공간 복잡도: O(n)
*/

0 comments on commit c67f669

Please sign in to comment.