Skip to main content

Pattern · 51 problems

String Interview Problems

Most string problems are array problems wearing a coat. I name the pattern underneath, and the text stops being scary.

Start with Subtree of Another Tree

Free: write the code and run it, no sign-up. Read the problem here; code it on a laptop.

When I reach for it

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.
  • A window, two pointers or a count map hides inside.

Problems, easiest first

8 easy · 33 medium · 10 hard. Open one to read it, then write it in the free editor.

  1. Subtree of Another TreeeasyTreesBFS & DFS
  2. Valid AnagrameasyArrays & Hashing
  3. Valid PalindromeeasyTwo Pointers
  4. Valid ParentheseseasyStack
  5. Add BinaryeasyBit Manipulation
  6. Longest Palindrome LengtheasyGreedyArrays & Hashing
  7. Ransom NoteeasyArrays & Hashing
  8. Valid Palindrome IIeasyTwo Pointers
  9. Decode WaysmediumDynamic Programming
  10. Implement Trie (Prefix Tree)mediumTreesDesign
  11. Longest Common SubsequencemediumDynamic Programming
  12. Longest Palindromic SubstringmediumTwo PointersDynamic Programming
  13. Longest Repeating Character ReplacementmediumSliding WindowTwo Pointers
  14. Palindromic SubstringsmediumTwo PointersDynamic Programming
  15. Word SearchmediumBacktrackingBFS & DFS
  16. Design Add and Search Words Data StructuremediumTreesDesign
  17. Analytics: Longest Unique Session WindowmediumSliding Window
  18. Encode and Decode StringsmediumDesign
  19. Group AnagramsmediumArrays & Hashing
  20. Word BreakmediumTreesDynamic Programming
  21. Edit DistancemediumDynamic Programming
  22. Generate ParenthesesmediumBacktrackingDynamic Programming
  23. Interleaving StringmediumDynamic Programming
  24. Letter Combinations of a Phone NumbermediumBacktrackingArrays & Hashing
  25. Multiply Stringsmedium
Show the other 26 problems
  1. Palindrome PartitioningmediumBacktrackingDynamic Programming
  2. Partition LabelsmediumGreedyTwo Pointers
  3. Valid Parenthesis StringmediumStackGreedy
  4. Permutation in StringmediumSliding WindowTwo Pointers
  5. Accounts MergemediumGraphsBFS & DFS
  6. String to Integer Conversionmedium
  7. Find All Anagrams in a StringmediumSliding WindowArrays & Hashing
  8. Decode StringmediumStack
  9. Find All Possible Recipes from Given SuppliesmediumGraphsArrays & Hashing
  10. Minimum Remove to Make Valid ParenthesesmediumStackGreedy
  11. Reorganize StringmediumGreedyArrays & Hashing
  12. Replace WordsmediumArrays & Hashing
  13. Search Suggestions SystemmediumBinary SearchArrays & Hashing
  14. Top K Frequent WordsmediumHeapsArrays & Hashing
  15. Simplify PathmediumStack
  16. Web CrawlermediumDesignGraphs
  17. Word Search IIhardBacktrackingTrees
  18. Alien DictionaryhardGraphsBFS & DFS
  19. Minimum Window SubstringhardSliding WindowArrays & Hashing
  20. Distinct SubsequenceshardDynamic Programming
  21. Regular Expression MatchinghardBacktrackingDynamic Programming
  22. Word LadderhardGraphsBFS & DFS
  23. Basic CalculatorhardStack
  24. Encrypt and Decrypt StringshardDesignArrays & Hashing
  25. Word Break IIhardBacktrackingDynamic Programming
  26. Design Excel Sum FormulahardDesignGraphs

The idea

Ask which array pattern fits. A count map handles anagrams. Two pointers handle palindromes. A window handles the longest or shortest stretch.

Build results in a list and join once at the end. Strings are immutable in Python, Java and JavaScript, so adding to one in a loop copies it every time.

out = []
for ch in text:
    if keep(ch):
        out.append(transform(ch))   # build pieces in a list
return "".join(out)                 # join once at the end
Collect the pieces, then join once. The loop stays linear and the intent stays readable. Python shown; the idea carries to every language in the editor.

Where it goes wrong

  • Skipping the empty string and the one-character string.
  • Not asking whether case, spaces and punctuation count.
  • Slicing in a loop. Each slice copies, and the cost adds up.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

Where Coco comes in

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

Questions people ask

Should I turn the string into a list?
In Python, build a list of pieces and join once. In languages with mutable character arrays, edit in place instead.
What do interviewers watch for on string problems?
Edge cases and hidden cost. They want empty input handled and no quiet quadratic copying.

Meet these next

Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.