forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhewchews.ts
More file actions
33 lines (28 loc) · 862 Bytes
/
Copy pathwhewchews.ts
File metadata and controls
33 lines (28 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function numDecodings(s: string): number {
// SC: O(N)
const memo: { [key: number]: number } = { [s.length]: 1 };
// TC: O(N)
const dfs = (start: number): number => {
if (start in memo) {
return memo[start];
}
if (s[start] === "0") {
// 0으로 시작하는 경우 가능한 경우의 수가 없음
memo[start] = 0;
} else if (
start + 1 < s.length &&
parseInt(s.substring(start, start + 2)) < 27
) {
// 다음에 오는 글자가 두글자 이상 있고, start start+1 두글자가 1~26 사이의 값인 경우
memo[start] = dfs(start + 1) + dfs(start + 2);
} else {
// 1글자만 남은 경우 or 첫 두글자가 27보다 큰 경우
memo[start] = dfs(start + 1);
}
return memo[start];
};
// SC: 재귀호출 O(N)
return dfs(0);
}
// TC: O(N)
// SC: O(N)