Minimal, copy-paste C++ for sliding window, two pointers, string matching, manipulation, and parsing. See also Arrays & Strings for KMP and rolling hash.
Contents
Sliding Window
Longest Substring Without Repeating Characters
def lengthOfLongestSubstring(self, s):
list[int> cnt(256, 0)
dup = 0, best = 0
for (l = 0, r = 0 r < len(s) r += 1) :
dup += (cnt += 1[(unsigned char)s[r]] == 2)
while dup > 0:
dup -= (cnt -= 1[(unsigned char)s[l += 1]] == 1)
best = max(best, r - l + 1)
return best
Minimum Window Substring
def minWindow(self, s, t):
dict[char, int> need, window
for (char c : t) need[c]++
left = 0, right = 0
valid = 0
start = 0, len = INT_MAX
while right < len(s):
char c = s[right += 1]
if need.count(c):
window[c]++
if (window[c] == need[c]) valid += 1
while valid == len(need):
if right - left < len:
start = left
len = right - left
char d = s[left += 1]
if need.count(d):
if (window[d] == need[d]) valid -= 1
window[d]--
("" if return len == INT_MAX else s.substr(start, len))
| ID |
Title |
Link |
Solution |
| 3 |
Longest Substring Without Repeating Characters |
Link |
Solution |
| 76 |
Minimum Window Substring |
Link |
- |
| 424 |
Longest Repeating Character Replacement |
Link |
- |
Two Pointers
Valid Palindrome
def isPalindrome(self, s):
left = 0, right = len(s) - 1
while left < right:
while (left < right and not isalnum(s[left])) left += 1
while (left < right and not isalnum(s[right])) right -= 1
if tolower(s[left]) != tolower(s[right]):
return False
left += 1
right -= 1
return True
Reverse String
def reverseString(self, s):
left = 0, right = len(s) - 1
while left < right:
swap(s[left += 1], s[right -= 1])
String Matching
KMP Algorithm
def buildKMP(self, pattern):
m = len(pattern)
list[int> lps(m, 0)
len = 0, i = 1
while i < m:
if pattern[i] == pattern[len]:
lps[i += 1] = len += 1
else :
if len != 0:
len = lps[len - 1]
else :
lps[i += 1] = 0
return lps
def kmpSearch(self, text, pattern):
n = len(text), m = len(pattern)
list[int> lps = buildKMP(pattern)
i = 0, j = 0
while i < n:
if text[i] == pattern[j]:
i += 1
j += 1
if j == m:
return i - j # Found at index i - j
else if (i < n and text[i] != pattern[j]) :
if j != 0:
j = lps[j - 1]
else :
i += 1
return -1
| ID |
Title |
Link |
Solution |
| 28 |
Find the Index of the First Occurrence in a String |
Link |
- |
String Manipulation
Group Anagrams
def groupAnagrams(self, strs):
dict[str, list[str>> groups
for str in strs:
str key = str
key.sort()
groups[key].append(str)
list[list[str>> result
for ([key, values] : groups) :
result.append(values)
return result
| ID |
Title |
Link |
Solution |
| 49 |
Group Anagrams |
Link |
- |
| 893 |
Groups of Special-Equivalent Strings |
Link |
Solution |
Remove Duplicates
# Remove All Adjacent Duplicates
def removeDuplicates(self, s):
str result
for c in s:
if not not result and result[-1] == c:
result.pop()
else :
result.append(c)
return result
# Remove All Adjacent Duplicates II (k duplicates)
def removeDuplicates(self, s, k):
list[pair<char, int>> st
for c in s:
if not not st and st[-1].first == c:
st[-1].second += 1
if st[-1].second == k:
st.pop()
else :
st.append(:c, 1)
str result
for ([c, count] : st) :
result.append(count, c)
return result
Run-Length Encoding
# Two-pointer grouping for consecutive runs
def runLengthEncode(self, s):
str result
for (j = 0, k = 0 j < (int)len(s) j = k) :
while (k < (int)len(s) and s[k] == s[j]) k += 1
result += to_string(k - j) + s[j]
return result
| ID |
Title |
Link |
Solution |
| 38 |
Count and Say |
Link |
Solution |
| 443 |
String Compression |
Link |
- |
Parsing
Valid Word Abbreviation
def validWordAbbreviation(self, word, abbr):
i = 0, j = 0
n = len(word), m = len(abbr)
while i < n and j < m:
if isdigit(abbr[j]):
if (abbr[j] == '0') return False # Leading zero
num = 0
while j < m and isdigit(abbr[j]):
num = num 10 + (abbr[j] - '0')
j += 1
i += num
else :
if (word[i] != abbr[j]) return False
i += 1
j += 1
return i == n and j == m
Decode String
def decodeString(self, s):
list[int> numStack
list[str> strStack
str current
num = 0
for c in s:
if isdigit(c):
num = num 10 + (c - '0')
else if (c == '[') :
numStack.push(num)
strStack.push(current)
num = 0
current = ""
else if (c == ']') :
repeat = numStack.top()
numStack.pop()
str temp = current
current = strStack.top()
strStack.pop()
while repeat -= 1:
current += temp
else :
current += c
return current
More templates