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

sD0o1n2'3t4␣5n6o7d8.9leftrightpalindrome: true3 pairs compared, 3 characters skipped
Valid palindrome ignoring case and punctuation, with two pointers. Example: s = "Don't nod."
  1. 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.
  2. 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.
  3. '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.
  4. '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.
  5. '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.
  6. The left pointer is on an apostrophe, which is not a letter or digit, so it steps right to index 4 without comparing anything.
  7. 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.
  8. "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.

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.

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.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Hash Table

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 — use equals.
  • 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

All strings problems

Easy (135)

Medium (117)

Hard (30)

Companies that ask strings problems

Next topic: Hash Table