# Strings

> Handle character counting, palindromes, parsing and basic pattern matching, including the string questions service-company tests love. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/strings
Last updated: 2026-11-28

Handle character counting, palindromes, parsing and basic pattern matching, including the string questions service-company tests love.

Beginner · about 8 hours · difficulty 2/5

Learn first: [Arrays: Core Techniques](https://www.internhack.in/roadmaps/tracks/dsa-placement/arrays)

String questions are very common in mass-recruiter OAs and in first-round interviews. Most rely on counting, two pointers or careful parsing.

### Key patterns

- **Frequency arrays**: a size-26 array beats a hash map for lowercase letters (Valid Anagram, Ransom Note).
- **Two pointers from both ends**: palindromes, reversing words, Valid Palindrome with non-alphanumeric characters.
- **Expand around centre**: Longest Palindromic Substring in O(n^2) with O(1) space.
- **Parsing**: String to Integer (atoi), compressing runs ("aaabb" to "a3b2"), counting words.
- **Pattern matching**: naive O(n\*m) first. KMP (prefix function) and Z-function give O(n + m) and appear in product-company rounds now and then. Rabin-Karp uses rolling hashes.

### Classic problems

Valid Anagram, Valid Palindrome, Reverse Words in a String, Longest Common Prefix, String Compression, Longest Palindromic Substring, String to Integer (atoi), Find the Index of the First Occurrence in a String, Roman to Integer.

### Pitfalls

- Building strings with += in a loop (O(n^2) in Java and Python).
- Forgetting uppercase, spaces, digits or Unicode in the input.
- In C++, `s.substr(i, len)` copies, so calling it inside a loop adds a hidden factor of n.

You are ready to move on when you can solve Longest Palindromic Substring by expanding around centre and explain what the KMP prefix function stores.

## Key concepts

- **Character frequency arrays**
- **Palindrome checks**
- **Expand around centre**
- **Parsing and string building**
- **KMP prefix function**
- **Z-function**
- **Rabin-Karp rolling hash**

## Mini project

Solve these 10 in 7 days, log time and pattern: Valid Anagram, Valid Palindrome, Reverse Words in a String, Longest Common Prefix, String Compression, Roman to Integer, String to Integer (atoi), Longest Palindromic Substring, Find the Index of the First Occurrence (with KMP), Count and Say.

## Check yourself

For Longest Palindromic Substring, why do you need to expand from both single characters and pairs of characters?

## Free resources

- [cp-algorithms: Prefix function (KMP)](https://cp-algorithms.com/string/prefix-function.html) (cp-algorithms)
- [cp-algorithms: Z-function](https://cp-algorithms.com/string/z-function.html) (cp-algorithms)
- [LeetCode problem set](https://leetcode.com/problemset/) (LeetCode)

Previous: [Arrays: Core Techniques](https://www.internhack.in/roadmaps/tracks/dsa-placement/arrays) · Next: [Hashing with Maps and Sets](https://www.internhack.in/roadmaps/tracks/dsa-placement/hashing)

## More in Arrays, Strings and Hashing

- [Arrays: Core Techniques](https://www.internhack.in/roadmaps/tracks/dsa-placement/arrays)
- [Strings](https://www.internhack.in/roadmaps/tracks/dsa-placement/strings)
- [Hashing with Maps and Sets](https://www.internhack.in/roadmaps/tracks/dsa-placement/hashing)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
