ans = [0, 0] for i inrange(len(s)): dp[i][i] = True if i + 1 < len(s): dp[i][i+1] = s[i] == s[i+1] if dp[i][i+1]: ans[0], ans[1] = i, i+1
for j inrange(2, len(s)): for i inrange(0, len(s)): idx1, idx2 = i, i + j if idx1 + 1 < len(s) and idx2 < len(s): dp[idx1][idx2] = dp[idx1+1][idx2-1] and s[idx1] == s[idx2] if dp[idx1][idx2] and idx2 - idx1 + 1> ans[1] - ans[0]: ans[0], ans[1] = idx1, idx2 return s[ans[0]: ans[1] + 1]
No.125 - Valid Palindrome
Substring
No.3 - Longest Substring Without Repeating Characters Sliding window, HashTable to record the index
1 2 3 4 5 6 7 8 9 10
dic = {} max_len = 0 i, j = 0, 0 while j < len(s): if s[j] in dic: i = max(dic[s[j]] + 1, i) dic[s[j]] = j max_len = max(max_len, j - i + 1) j += 1 return max_len
No.340 - Longest Substring with At Most K Distinct Characters Sliding window, Hashtable to record the rightest index of the character
1 2 3 4 5 6 7 8 9 10 11 12
max_len = 0 left, right = 0, 0 dic = {} while right < len(s): if s[right] notin dic andlen(dic.keys()) >= k: left = min(dic.values()) dic.pop(s[left]) left += 1 dic[s[right]] = right max_len = max(max_len, right - left + 1) right += 1 return max_len
DP - dp[i] means the shortest substring with at most k distinct characters if you must use s[i], therefore, we have $s[i] = s[i - 1] + 1$ if there is no more than k distinct characters from bg to i $s[i] = i - bg_new$ where bg_new is where there are only k distince characters from bg_new to i
defminWindow(self, s: str, t: str) -> str: form_t = collections.Counter(t) current_cnt, target = 0, len(form_t) dic_cnt = {} left, right = 0, 0 min_len = s + t while right < len(s): if s[right] in form_t: dic_cnt[s[right]] = dic_cnt.get(s[right], 0) + 1 if dic_cnt[s[right]] == form_t[s[right]]: current_cnt += 1 while current_cnt == target: if s[left] in form_t: dic_cnt[s[left]] -= 1 if dic_cnt[s[left]] < form_t[s[left]]: current_cnt -= 1 tmp = s[left: right + 1] min_len = tmp iflen(tmp) < len(min_len) else min_len left += 1 right += 1 return min_len if min_len != s + t else""
No.14 - Longest Common Prefix
Pair Match or Mapping
No.20 - Valid Parentheses HashTable
No.10 - Regular Expression Matching DP
No.13 - Roman to Integer HashTable
No.17 - Letter Combinations of a Phone Number
No.91 - Decode Ways DP
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# dp[i] means s[:i+1] can be decoded in dp[i] ways dp = [0for _ inrange(len(s))] # max is dp[len(s)] if s[0] != "0": dp[0] = 1 else: return0 for i inrange(1, len(dp)): if s[i] != "0": dp[i] += dp[i - 1] if10 <= int(s[i-1:i+1]) <= 26: dp[i] += dp[i - 2] if i - 2 >= 0else1 return dp[-1]