Skip to content

Commit 81cb39f

Browse files
committed
decode-ways solution
1 parent e03134f commit 81cb39f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

decode-ways/ohgyulim.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
/* 시간 복잡도: O(N)
3+
* - for 루프: O(N)
4+
* 공간 복잡도: O(N), dp배열
5+
*/
6+
public int numDecodings(String s) {
7+
if (s.charAt(0) == '0') return 0;
8+
int[] dp = new int[s.length() + 1];
9+
dp[0] = 1;
10+
dp[1] = 1;
11+
12+
for (int i = 2; i <= s.length(); i++) {
13+
int one = Integer.parseInt(s.substring(i - 1, i));
14+
int two = Integer.parseInt(s.substring(i - 2, i));
15+
16+
if (one > 0) {
17+
dp[i] += dp[i - 1];
18+
}
19+
if (two >= 10 && two <= 26) {
20+
dp[i] += dp[i - 2];
21+
}
22+
}
23+
return dp[s.length()];
24+
}
25+
}
26+

0 commit comments

Comments
 (0)