String
Two pointers, palindromes, character counting and string matching with KMP and rolling hashes.
DSABeginner16 hrs8 patterns · 29 problems
0%
your progressCore idea
Fixed alphabets make counting O(1) space (26-int arrays). Palindromes expand from centers; matching uses prefix-function (KMP) or rolling hashes to avoid re-scanning.
Reach for it when
- Anagram, palindrome or substring questions
- Pattern matching inside a larger text
- Frequency of characters decides the answer
Template
def longest_palindrome(s):
best = ''
for c in range(len(s)):
for l, r in ((c, c), (c, c + 1)):
while l >= 0 and r < len(s) and s[l] == s[r]:
l, r = l - 1, r + 1
if r - l - 1 > len(best):
best = s[l + 1:r]
return bestComplexity
Counting O(n); expand-around-center O(n²); KMP / Rabin-Karp O(n + m).
Common pitfalls
- String concatenation in a loop is O(n²) — collect and join
- Off-by-one on even-length palindromes
- Hash collisions in Rabin-Karp without verification
Patterns in this course
Recognition cues
HashingTwo PointersPalindromeKMP/String MatchingSliding Window