Strings Coding Problems: 282 Questions with Solutions
282 strings coding problems — 135 easy · 117 medium · 30 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.
- Problems: 282
- By difficulty: 135 easy · 117 medium · 30 hard
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
- Cost: Free on every plan; sign in to run and submit
A string is an array of characters with its own habits: counting letters, comparing prefixes, reversing, checking palindromes, building an answer character by character. String problems reward knowing the cheap operations (index, compare, count in a fixed alphabet) from the expensive ones (repeated concatenation, substring scans) and choosing the representation — a character count, a two-pointer walk, a stack — that fits the question.
How strings works, step by step
s = "Don't nod."- Two pointers start at the two ends of "Don't nod." and walk inward. Anything that is not a letter or digit is skipped, and letters are compared in lower case, so spaces, punctuation and capitals cannot spoil the check.
- The right pointer is on a full stop, which is not a letter or digit, so it steps left to index 8 without comparing anything.
- 'D' and 'd' match once both are lower-cased to 'd', so this pair is fine — a single differing pair would end the check with false. Both pointers move inward, to 1 and 7.
- 'o' and 'o' are the same letter, so this pair is fine. That makes 2 matching pairs; the pointers close in to 2 and 6.
- 'n' and 'n' are the same letter, so this pair is fine. That makes 3 matching pairs; the pointers close in to 3 and 5.
- The left pointer is on an apostrophe, which is not a letter or digit, so it steps right to index 4 without comparing anything.
- The right pointer skips a space and lands on index 4, where the left pointer already is. The pointers have met, so every pair of letters has been checked.
- "Don't nod." is a valid palindrome: its letters read d-o-n-t-n-o-d both ways. Each index is visited at most once by one pointer, so the check is O(n) time and O(1) space, with no cleaned copy of the string.
Strings study plan
14 of the 282 Strings problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 268 in the full list below are practice at your own pace. Then move on to Hash Table.
Day 1
Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.
Day 2
Medium problems: the same pattern with one twist each. Name the twist before you code.
- Reverse String Easy
- String Compression Medium
Day 3
More mediums. Before coding each one, write down what state the pattern keeps and when it changes.
Day 4
More mediums. Before coding each one, write down what state the pattern keeps and when it changes.
Day 5
More mediums. Before coding each one, write down what state the pattern keeps and when it changes.
- Decode String Medium
- Partition Labels Medium
Day 6
Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.
Day 7
Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.
- Word Ladder Hard
Day 8
Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.
Strings: the essentials
When to reach for it
The input is text and the question is about its characters: is one string a rearrangement, prefix or subsequence of another; which piece is the longest with some property; how should it be parsed, encoded or compressed. Read the stated alphabet. "Lowercase English letters" means a 26-slot count array will do the job of a hash map. "The longest substring such that…" is usually a Sliding Window question.
The pattern
Treat the string as a read-only array. Compare by counts when order does not matter (anagrams), by aligned indices when it does (prefixes, palindromes), and by one pointer per string when one must appear inside the other in order (subsequences). Collect output in a list and join it once at the end, rather than adding to a string inside the loop.
def is_anagram(s, t):
if len(s) != len(t):
return False
count = [0] * 26
for a, b in zip(s, t):
count[ord(a) - ord("a")] += 1
count[ord(b) - ord("a")] -= 1
return all(c == 0 for c in count)
Cost
A scan is O(n). Strings are immutable in Java, Python, JavaScript and C#, so s = s + c in a loop can copy the whole string each time and cost O(n²); taking a substring is usually a copy too, so slicing inside a loop is not free.
Common mistakes
- Off-by-one on substring bounds: most libraries take
[start, end), end exclusive. - Comparing strings with
==in Java, which compares references, not contents — useequals. - Trying to change a character in place in Python or Java; convert to a list or a
char[]first. - Assuming lowercase letters when the constraints allow digits, spaces or upper case.
Start with
- Longest Common Prefix: aligned indices across several strings.
- Valid Anagram: counting over a fixed alphabet.
- String to Integer (atoi): careful parsing, signs and overflow.
All strings problems
Easy (135)
- Find the Original Typed String I
- Rearrange Characters to Make Target String Hash Table, Counting
- Number of Strings That Appear as Substrings in Word Array
- Circular Sentence
- Number of Senior Citizens Array
- Find Words Containing Character Array
- Strong Password Checker II
- Decode the Message Hash Table
- Count Prefixes of a Given String Array
- Count Asterisks
- Existence of a Substring in a String and Its Reverse Hash Table
- Number of Changing Keys
- Find the Encrypted String Simulation
- Minimum Moves to Convert String Greedy
- Minimum Recolors to Get K Consecutive Black Blocks Sliding Window
- Substrings of Size Three with Distinct Characters Hash Table, Sliding Window, Counting
- Find the K-Beauty of a Number Math, Sliding Window
- Shortest Word Distance Array, Two Pointers
- Shortest Distance to Target String in a Circular Array Array, Two Pointers
- DI String Match Array, Two Pointers, Greedy
- Check if Number Has Equal Digit Count and Digit Value Hash Table, Counting
- Check if The Number is Fascinating Math, Hash Table
- Number of Lines To Write String Array, Simulation
- Divide a String Into Groups of Size k Simulation
- Rings and Rods Hash Table, Bit Manipulation
- Check if All Characters Have Equal Number of Occurrences Hash Table, Counting
- Maximum Number of Words Found in Sentences Array
- Find Words That Can Be Formed by Characters Hash Table, Counting
- Count the Number of Vowel Strings in Range Array
- Check if Binary String Has at Most One Segment of Ones
- Minimum Changes to Make Alternating Binary String Greedy
- Most Common Word Hash Table
- Check If String Is a Prefix of Array Array
- Valid Word Abbreviation Two Pointers
- Student Attendance Record I
- Reverse String II Two Pointers
- Largest Substring Between Two Equal Characters Hash Table
- Find Common Characters Array, Hash Table
- Keyboard Row Array, Hash Table
- Minimum Index Sum of Two Lists Array, Hash Table
- Remove Trailing Zeros From a String
- Maximum Odd Binary Number Greedy
- Furthest Point From Origin Greedy
- Largest Odd Number in String Math, Greedy
- Sum of Digits of String After Convert Simulation
- Permutation Difference between Two Strings Hash Table
- Split Strings by Separator Array
- Maximum Value of a String in an Array Array
- Odd String Difference Array, Hash Table
- First Letter to Appear Twice Hash Table
- Percentage of Letter in String
- Largest 3-Same-Digit Number in String
- Minimum Number of Operations to Convert Time Greedy
- Find First Palindromic String in the Array Array, Two Pointers
- Capitalize the Title
- Check if All A's Appears Before All B's
- Count Common Words With One Occurrence Array, Hash Table
- Score of a String
- Longest Nice Substring Divide and Conquer
- Remove Palindromic Subsequences Two Pointers
- Redistribute Characters to Make All Strings Equal Array, Counting
- Maximum Number of Words You Can Type Hash Table
- Check If Word Equals Summation of Two Words Math
- Determine Color of a Chessboard Square Math
- Replace All Digits with Characters
- Number of Different Integers in a String Hash Table
- Maximum Repeating Substring
- Thousand Separator
- Check If a Word Occurs As a Prefix of Any Word in a Sentence
- Count Substrings with Only One Distinct Letter Math
- Remove Vowels from a String
- Greatest Common Divisor of Strings Math
- Verifying an Alien Dictionary Array, Hash Table
- Goat Latin
- Unique Morse Code Words Array, Hash Table
- Long Pressed Name Two Pointers
- Reverse Only Letters Two Pointers
- To Lower Case
- Count the Number of Consistent Strings Array, Hash Table, Bit Manipulation
- Find the Difference Hash Table, Bit Manipulation, Sorting
- Minimum String Length After Removing Substrings Stack, Simulation
- Crawler Log Folder Array, Stack
- Make The String Great Stack
- Baseball Game Array, Stack, Simulation
- Maximum Score After Splitting a String Prefix Sum
- Rearrange Spaces Between Words
- Shortest Distance to a Character Array, Two Pointers
- Reverse Prefix of Word Two Pointers, Stack
- Merge Strings Alternately Two Pointers
- Count Binary Substrings Two Pointers
- Check If Two String Arrays Are Equivalent Array
- Split a String in Balanced Strings Greedy, Counting
- Remove Outermost Parentheses Stack
- Determine if String Halves Are Alike Counting
- Maximum Number of Balloons Hash Table, Counting
- Unique Email Addresses Array, Hash Table
- Rotate String String Matching
- Buddy Strings Hash Table
- License Key Formatting
- Number of Segments in a String
- Sorting the Sentence Sorting
- Truncate Sentence Array
- Check if the Sentence Is Pangram Hash Table
- Maximum Nesting Depth of the Parentheses Stack
- Shuffle String Array
- Goal Parser Interpretation
- Jewels and Stones Hash Table
- Defanging an IP Address
- Longest Palindrome Hash Table, Greedy
- Repeated Substring Pattern String Matching
- Detect Capital
- Length of Last Word
- Ransom Note Hash Table, Counting
- First Unique Character in a String Hash Table, Queue, Counting
- Reverse Vowels of a String Two Pointers
- Reverse Words in a String III Two Pointers
- Automorphic Number Math
- Add Strings Math, Simulation
- Add Binary Math, Bit Manipulation, Simulation
- Roman to Integer Hash Table, Math
- Excel Sheet Column Title Math
- Excel Sheet Column Number Math
- Remove All Adjacent Duplicates In String Stack
- Backspace String Compare Two Pointers, Stack
- Valid Palindrome II Two Pointers, Greedy
- Is Subsequence Two Pointers, Dynamic Programming
- Find the Index of the First Occurrence in a String Two Pointers
- Word Pattern Hash Table
- Isomorphic Strings Hash Table
- Longest Common Prefix
- Valid Palindrome Two Pointers
- Valid Anagram Hash Table, Sorting, Counting
- Fizz Buzz Math, Simulation
- Valid Parentheses Stack
- Reverse String Two Pointers
Medium (117)
- Make Three Strings Equal
- Number of Ways to Divide a Long Corridor Math, Dynamic Programming
- Maximum Number of Non-overlapping Palindrome Substrings Dynamic Programming, Greedy
- Count Number of Texts Hash Table, Math, Dynamic Programming
- Delete Characters to Make Fancy String Greedy
- Count Number of Homogenous Substrings Math
- Construct String With Repeat Limit Hash Table, Greedy, Heap (Priority Queue)
- Minimum Number of Steps to Make Two Strings Anagram II Hash Table, Counting
- Count Vowel Substrings of a String Hash Table, Sliding Window
- Longest Substring of All Vowels in Order Sliding Window
- Minimum Deletions to Make String Balanced Dynamic Programming, Stack
- Minimum Length of String After Operations Hash Table, Counting
- Minimum Number of Changes to Make Binary String Beautiful Greedy
- Lexicographically Minimum String After Removing Stars Greedy, Heap (Priority Queue), Stack
- Count Substrings Starting and Ending with Given Character Math, Counting
- Sort Vowels in a String Sorting
- Find the Kth Largest Integer in the Array Array, Sorting, Heap (Priority Queue)
- Open the Lock Array, Breadth-First Search
- Longest Binary Subsequence Less Than or Equal to K Greedy, Dynamic Programming, Memoization
- Number of Ways to Select Buildings Dynamic Programming, Prefix Sum
- Longest Ideal Subsequence Hash Table, Dynamic Programming
- Minimum Additions to Make Valid String Greedy, Dynamic Programming, Stack
- Partition String Into Substrings With Values at Most K Greedy, Dynamic Programming
- Separate Black and White Balls Greedy, Two Pointers
- Count Collisions on a Road Stack, Simulation, Greedy
- Minimum Number of Operations to Move All Balls to Each Box Array, Prefix Sum, Greedy
- Maximum Number of Removable Characters Array, Binary Search, Two Pointers
- Swap For Longest Repeated Character Substring Sliding Window, Hash Table, Binary Search
- Replace the Substring for Balanced String Sliding Window, Two Pointers
- Get Equal Substrings Within Budget Sliding Window, Binary Search, Prefix Sum
- Maximize the Confusion of an Exam Sliding Window, Prefix Sum, Binary Search
- Number of Substrings Containing All Three Characters Hash Table, Sliding Window
- Longest Substring with At Most K Distinct Characters Hash Table, Sliding Window
- Longest Substring with At Most Two Distinct Characters Hash Table, Sliding Window
- Sentence Similarity III Two Pointers, Array
- Push Dominoes Two Pointers, Dynamic Programming, Simulation
- Number of Wonderful Substrings Bit Manipulation, Hash Table, Prefix Sum
- Find the Longest Substring Containing Vowels in Even Counts Bit Manipulation, Hash Table, Prefix Sum
- Flip String to Monotone Increasing Dynamic Programming, Bit Manipulation
- Optimal Partition of String Hash Table, Greedy
- Determine if Two Strings Are Close Hash Table, Counting, Sorting
- Smallest String With Swaps Union Find, Hash Table, Sorting
- Short Encoding of Words Trie, Hash Table
- Replace Words Trie, Hash Table
- Longest Word in Dictionary Trie, Hash Table
- Word Subsets Hash Table, Counting
- Number of Good Ways to Split a String Hash Table, Prefix Sum
- Minimum Length of String After Deleting Similar Ends Two Pointers
- Can Convert String in K Moves Hash Table, Greedy
- Longest Substring With At Least K Repeating Characters Sliding Window, Divide and Conquer
- Maximum Number of Occurrences of a Substring Sliding Window, Hash Table
- Number of Ways to Split a String Math, Counting
- Minimum Number of Frogs Croaking Greedy, Counting
- Check If a String Can Break Another String Greedy, Sorting
- Count Substrings That Differ by One Character Dynamic Programming
- Maximum Length of a Concatenated String With Unique Characters Bit Manipulation, Backtracking
- Count Words Obtained After Adding a Letter Bit Manipulation, Hash Table
- Longest Word in Dictionary Through Deleting Two Pointers, Sorting
- Number of Matching Subsequences Two Pointers, Binary Search
- Find and Replace Pattern Hash Table
- Minimum ASCII Delete Sum for Two Strings Dynamic Programming
- Longest Palindromic Subsequence Dynamic Programming
- Number of Steps to Reduce a Number in Binary Representation to One Bit Manipulation, Simulation
- Word Search Array, Backtracking, Depth-First Search
- Minimum Genetic Mutation Hash Table, Breadth-First Search
- Maximum Product of Word Lengths Array, Bit Manipulation
- Minimum Insertions to Balance a Parentheses String Stack, Greedy
- Remove All Adjacent Duplicates in String II Stack
- Maximum Score From Removing Substrings Stack, Greedy
- Minimum Number of Swaps to Make the String Balanced Stack, Greedy, Two Pointers
- Remove All Occurrences of a Substring Stack, Simulation
- Removing Stars From a String Stack, Simulation
- Dota2 Senate Greedy, Queue
- Reverse Substrings Between Each Pair of Parentheses Stack
- Basic Calculator II Math, Stack
- Next Greater Element III Math, Two Pointers
- Check If Word Is Valid After Substitutions Stack
- Score of Parentheses Stack
- Minimum Remove to Make Valid Parentheses Stack
- Minimum Add to Make Parentheses Valid Stack, Greedy
- Top K Frequent Words Array, Hash Table, Sorting
- Minimum Time to Make Rope Colorful Array, Dynamic Programming, Greedy
- Remove Duplicate Letters Stack, Greedy, Monotonic Stack
- Largest Number Array, Greedy, Sorting
- Minimum Number of Steps to Make Two Strings Anagram Hash Table, Counting
- Minimum Deletions to Make Character Frequencies Unique Hash Table, Greedy, Sorting
- Longest String Chain Array, Hash Table, Two Pointers
- Ones and Zeroes Array, Dynamic Programming
- Interleaving String Dynamic Programming
- Delete Operation for Two Strings Dynamic Programming
- Maximum Number of Vowels in a Substring of Given Length Sliding Window
- Custom Sort String Hash Table, Sorting
- Valid Parenthesis String Dynamic Programming, Stack, Greedy
- String Compression Two Pointers
- Count and Say Simulation
- Reverse Words in a String Two Pointers
- Integer to Roman Hash Table, Math, Greedy
- Generate Parentheses Backtracking, Dynamic Programming
- Letter Combinations of a Phone Number Backtracking, Hash Table
- Sort Characters By Frequency Heap, Hash Table, Bucket Sort
- Edit Distance Dynamic Programming
- Longest Common Subsequence Dynamic Programming
- Decode Ways Dynamic Programming
- Word Break Dynamic Programming, Hash Table, Trie
- Partition Labels Greedy, Hash Table, Two Pointers
- Decode String Stack, Recursion
- Simplify Path Stack
- Remove K Digits Stack, Greedy, Monotonic Stack
- Permutation in String Sliding Window, Hash Table
- Multiply Strings Math, Simulation
- Compare Version Numbers Two Pointers
- String to Integer (atoi) Math
- Find All Anagrams in a String Sliding Window, Hash Table
- Longest Repeating Character Replacement Sliding Window
- Palindromic Substrings Dynamic Programming
- Longest Palindromic Substring Dynamic Programming
- Longest Substring Without Repeating Characters Sliding Window, Hash Table
Hard (30)
- Number of Ways to Separate Numbers Dynamic Programming, Suffix Array, Prefix Sum
- Form Largest Integer With Digits That Add up to Target Array, Dynamic Programming
- Restore the Array Dynamic Programming
- Strange Printer Dynamic Programming
- Special Binary String Recursion, Divide and Conquer, Greedy
- Count Different Palindromic Subsequences Dynamic Programming
- Smallest K-Length Subsequence With Occurrences of a Letter Stack, Greedy, Monotonic Stack
- Valid Palindrome III Dynamic Programming
- Count Anagrams Hash Table, Math, Combinatorics
- String Compression II Dynamic Programming
- Minimum White Tiles After Covering With Carpets Dynamic Programming, Prefix Sum
- Scramble String Dynamic Programming
- Distinct Subsequences II Dynamic Programming
- Minimum Number of Flips to Make the Binary String Alternating Sliding Window, Dynamic Programming, Greedy
- Take K of Each Character From Left and Right Hash Table, Sliding Window, Prefix Sum
- Minimum Window Subsequence Two Pointers, Dynamic Programming, Sliding Window
- Minimum Number of Moves to Make Palindrome Two Pointers, Greedy
- Substring with Concatenation of All Words Hash Table, Sliding Window
- Longest Happy Prefix KMP, Rolling Hash
- Count Unique Characters of All Substrings of a Given String Hash Table, Counting
- Shortest Palindrome KMP, Rolling Hash
- Palindrome Partitioning II Dynamic Programming
- Wildcard Matching Dynamic Programming, Greedy
- Distinct Subsequences Dynamic Programming
- Minimum Insertion Steps to Make a String Palindrome Dynamic Programming
- Word Ladder Hash Table, Breadth-First Search
- Longest Valid Parentheses Dynamic Programming, Stack
- Regular Expression Matching Dynamic Programming, Recursion
- String Matching: All Occurrences Rolling Hash, String Matching
- Minimum Window Substring Sliding Window, Hash Table
Companies that ask strings problems
- Amazon 238 problems on strings
- Google 150 problems on strings
- Adobe 59 problems on strings
- Microsoft 44 problems on strings
- Meta 38 problems on strings
- TCS 34 problems on strings
- Infosys 26 problems on strings
- Bloomberg 15 problems on strings
- Cognizant 13 problems on strings
- Wipro 13 problems on strings
- Uber 12 problems on strings
- Zoho 10 problems on strings
Next topic: Hash Table