Strings
Treat a string as an ordered sequence and choose what counts as a character before you count, compare or search.
Picture the idea
An anagram is a pair of letter racks with identical inventories even when the letters sit in different positions.
When to use it
Look for positional comparisons, a frequency requirement, repeated prefixes or a contiguous text segment.
How it works
For a palindrome, compare the characters at the two ends. An unequal pair disproves symmetry immediately. Equal pairs leave a smaller middle segment.
- Compare opposing characters.
- Count frequencies by symbol.
- Advance a prefix match and reuse its border on mismatch.
A two-ended palindrome scan costs O(n) time and O(1) auxiliary space for directly indexable units. Frequency counting takes O(k) space for k distinct symbols.
Watch for this mistake
Byte length, UTF-16 code units, code points and visible characters can differ. Define the alphabet in the problem.
Confirm how case, spaces and punctuation should be handled before normalizing the input.