-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class Solution { | ||
public int numDecodings(String s) { | ||
/** | ||
1. understanding | ||
- number to upper case alphabet mapping code: 1 -> A, ... 26 -> Z | ||
- many ways to decode each input string | ||
- also there can be no way to decode input string. | ||
- answer fits in 32-bit integer. | ||
2. example | ||
- 12: (1, 2), (12) | ||
- 226: (2, 2, 6), (2, 26), (22, 6) | ||
- 06: (0, x) | ||
3. strategy | ||
- iterate in reverse order, | ||
- at index k, dp[k] means the count of decode ways till that index. | ||
- dp[k-1] = 0 if num[k] == 0 | ||
- dp[k-1] = dp[k] + dp[k+1] if 1<= nums[k-1:k] < 27 | ||
- dp[k-1] = dp[k] | ||
- dp[n] = 1 -> assume that first empty input can be decoded in 1 way. | ||
4. complexity | ||
- time: O(N) | ||
- space: O(1) | ||
*/ | ||
int prev = 1; | ||
int current = 1; | ||
for (int i = s.length()-1; i >= 0; i--) { // O(1) | ||
if (s.charAt(i) == '0') { | ||
int tmp = current; | ||
current = 0; | ||
prev = tmp; | ||
} else if ( (i+1) < s.length() && Integer.parseInt(s.substring(i, i+2)) < 27) { | ||
int tmp = current; | ||
current = prev + current; | ||
prev = tmp; | ||
} else { | ||
prev = current; | ||
} | ||
} | ||
return current; | ||
} | ||
} | ||
|