We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e783ad8 commit f5eee34Copy full SHA for f5eee34
decode-ways/jun0811.js
@@ -0,0 +1,32 @@
1
+/**
2
+ * @param {string} s
3
+ * @return {number}
4
+ */
5
+
6
+function check(str) {
7
+ if (str.length == 2) {
8
+ if (str[0] == '0') return false;
9
+ }
10
+ if (str >= 1 && str <= 26) return true;
11
+ return false;
12
+}
13
14
+var numDecodings = function (s) {
15
+ if (s[0] == '0') return 0;
16
+ const dp = [1, 1];
17
18
+ for (let i = 2; i <= s.length; i++) {
19
+ let tmp = dp[i - 1] + dp[i - 2];
20
21
+ // 2글자 체크 -> dp[i-2]을 뺴줌
22
+ const two_c = String(s[i - 2]) + String(s[i - 1]);
23
+ if (!check(two_c)) tmp -= dp[i - 2];
24
25
+ // 1글자 체크 -> dp[i-1]을 빼줌
26
+ if (!check(s[i - 1])) tmp -= dp[i - 1];
27
28
+ if (tmp == 0) return 0;
29
+ dp[i] = tmp;
30
31
+ return dp[s.length];
32
+};
0 commit comments