A string is really an array of characters, so everything you know about arrays applies. Most placement string questions test palindromes, anagrams, character frequencies, and simple pattern searches.
Count or scan
Two ideas solve the majority of string problems: a frequency array (size 26 for lowercase letters) for anything about character counts, and two pointers for palindromes and reversals.
Palindrome check with two pointers
left = 0; right = len - 1
while left < right:
if s[left] != s[right]: return false
left++; right--
return trueO(n)→O(1)
Single scan; a 26-size count array is constant space.
⚡ The edge
- For anagrams, count characters with a 26-element array — that is O(n), beating the O(n log n) of sorting both strings.
- For palindromes, two pointers from both ends stop at the first mismatch, so you rarely scan the whole string.
Worked example
Are 'listen' and 'silent' anagrams?
- Anagrams contain the same letters with the same counts, just reordered, so compare character frequencies.
- Count each: both have l, i, s, t, e, n exactly once.
- All counts match, so they are anagrams.
Answer: Yes — they are anagrams (O(n) using a frequency count)
Worked example
Is 'madam' a palindrome?
left=0; right=4 compare s[0]='m' vs s[4]='m' -> equal compare s[1]='a' vs s[3]='a' -> equal left >= right -> stop
- Compare the outermost characters and move inward.
- m=m, then a=a; the middle character 'd' needs no comparison.
- No mismatch was found, so it reads the same both ways.
Answer: Yes — 'madam' is a palindrome
⚠ Watch out
- Decide up front whether case and spaces matter (is 'Madam' a palindrome?).
- A frequency array of size 26 assumes lowercase letters only — widen it for digits or mixed case.
- Strings are often immutable; building a new string in a loop can be O(n²).