Decode Ways
LeetCode 91 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
ways(i) = ways(i+1) [if s[i] ≠ '0'] + ways(i+2) [if s[i:i+2] ≤ 26].
ನಾನು Decode Ways (LeetCode 91) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — String DP / Word Break ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Reading the digits left to right, at each position you can decode one digit (if it isn't 0) or two digits (if they form 10–26). The number of decodings from a position is the sum of the decodings after each valid choice — a Fibonacci-like recurrence with validity checks.
Approach
- 1
A leading '0' at position i can't start any letter, so ways(i) = 0.
- 2
Take one digit: add ways(i+1).
- 3
Take two digits: if they make a number ≤ 26, add ways(i+2).
- 4
Reaching the end of the string counts as one complete decoding.
- 5
Tabulation fills dp from the end of the string back to 0.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ
lru_cache dfs(start).
dp[start] from dp[start+1] and dp[start+2].
Recursion + memoPythonO(n) time · O(n) space
from functools import lru_cache
class Solution:
def numDecodings(self, s: str) -> int:
#take one or two
@lru_cache(maxsize=None)
def dfs(start):
if start==len(s):
return 1
if s[start]=="0":
return 0
ways = 0
#take one
ways+=dfs(start+1)
#take two
end=start+2
if end<=len(s) and int(s[start:end])<=26:
ways+=dfs(end)
return ways
return dfs(0)Tabulation (right to left)PythonO(n) time · O(n) space
from functools import lru_cache
class Solution:
def numDecodings(self, s: str) -> int:
#take one or two
n=len(s)
dp=[0]*(n+1)
dp[n]=1
for start in range(n-1,-1,-1):
if s[start]=="0":
continue
ways = 0
#take one
ways+=dp[start+1]
#take two
end=start+2
if end<=len(s) and int(s[start:end])<=26:
ways+=dp[end]
dp[start]=ways
return dp[0]Decode Ways — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Decode Ways ಹಿಂದಿನ intuition ಏನು?
Reading the digits left to right, at each position you can decode one digit (if it isn't 0) or two digits (if they form 10–26). The number of decodings from a position is the sum of the decodings after each valid choice — a Fibonacci-like recurrence with validity checks.
Decode Ways (LeetCode 91) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. A leading '0' at position i can't start any letter, so ways(i) = 0. 2. Take one digit: add ways(i+1). 3. Take two digits: if they make a number ≤ 26, add ways(i+2). 4. Reaching the end of the string counts as one complete decoding. 5. Tabulation fills dp from the end of the string back to 0.
Decode Ways ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(n). Space: O(n). (Accepted approach: Tabulation (right to left).)
Decode Ways ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
'06' is invalid — the zero check comes before taking two digits. Two-digit values like '27' or '30' are not letters.
Decode Ways ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?
Recursion + memo (O(n) time · O(n) space): lru_cache dfs(start). Tabulation (right to left) (O(n) time · O(n) space): dp[start] from dp[start+1] and dp[start+2].