A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Concept Basics

Basic String

Easy

Strings are arrays of characters wearing a friendlier interface — and in Python and Java they are immutable, which changes how you build and modify them. This lesson covers the string mental model, the core operations and their real costs, and the three patterns behind this module's string problems: two-pointer palindrome checking, frequency-based anagram logic, and prefix matching.

The mental model: an immutable character array

Indexing, length and iteration behave exactly like arrays: s[i] in O(1), len(s), for ch in s. The difference is immutability in Python/Java (and const-correctness habits in C++): you cannot assign s[0] = 'A'. Every 'modification' builds a new string — which means building a string with += inside a loop copies the whole string each iteration, an accidental O(n^2). The fixes are idiomatic per language: collect pieces in a list and ''.join them (Python), use StringBuilder (Java), or mutate a char array / std::string directly (C++).

Slicing creates copies too — s[::-1] is a beautifully short reversal that costs O(n) memory, fine unless the problem demands in-place work.

Core operations and their honest costs

Concatenation of two strings costs O(total length). Searching for a substring (in / find / indexOf) is O(n × m) worst case in naive implementations — fast in practice thanks to optimised library code, but worth knowing. split and join are O(n). Comparing two strings is O(n) in the worst case (they may differ only at the last character), which is why sorting strings costs O(n log n × L), not just O(n log n).

Character arithmetic is the other essential: ord('a') = 97, chr(ord('a') + 3) = 'd'. Case conversion, digit checks (c.isdigit(), Character.isDigit) and alphabet indexing (ord(c) - ord('a') for the 26-slot frequency array) all build on it.

Pattern 1: two pointers for palindromes and reversals

Place one pointer at each end and walk inward comparing characters: a palindrome survives every comparison until the pointers meet. O(n) time, O(1) extra space, no reversed copy needed. The same skeleton reverses (swap instead of compare) and powers later classics like valid-palindrome-with-skips. Decide the fine print before coding: are comparisons case-sensitive? Do spaces and punctuation count? Problem statements differ, and this decision is where most wrong answers come from.

def is_palindrome(s): l, r = 0, len(s) - 1 while l < r: if s[l] != s[r]: return False l += 1 r -= 1 return True print(is_palindrome('racecar')) # True print(is_palindrome('hello')) # False

Patterns 2 and 3: frequencies and prefixes

Frequency logic from the Basic Hashing lesson applies verbatim to characters: anagrams compare count tables, 'sort characters by frequency' sorts the table, 'largest odd number in a string' scans digits with positional awareness. If a string problem mentions counts, your first move is a frequency table.

Prefix problems (longest common prefix and friends) compare position by position across strings: take the first string's character at index i, check every other string has the same character at i, stop at the first mismatch or the end of the shortest string. The shortest string bounds the answer — finding it first is the small optimisation that also prevents index errors.

Common mistakes

The += in a loop O(n^2) trap. Comparing strings with == in Java (reference comparison) instead of .equals. Index errors at the boundaries — empty string and single character are the test cases that expose them. Forgetting that sorting-based anagram checks cost O(n log n) when a counting check is O(n). And treating Unicode as single bytes: in most modern languages an index steps over characters/ code points, not raw bytes — know what your language counts.

Key points

  • Strings are immutable character arrays in Python/Java — build via list + join or StringBuilder, never += in a loop.
  • Know real costs: concatenation, slicing and comparison are O(n); searching is not free either.
  • Character arithmetic (ord/chr) unlocks alphabet indexing and case/digit tricks.
  • Palindromes and reversals are two-pointer problems: compare or swap while walking inward, O(1) extra space.
  • Counts in a string problem → frequency table first (anagrams, sort-by-frequency).
  • Prefix problems are bounded by the shortest string — find it first to dodge index errors.

Suggested questions

Reverse a String II ↗ Reversal three ways: slicing, two pointers on a char list, and the library call.
Palindrome Check ↗ The two-pointer skeleton from this lesson; decide case rules from the statement first.
Longest Common Prefix ↗ Position-by-position comparison bounded by the shortest string.
Valid Anagram ↗ Frequency equality on characters — the hashing pattern in string clothing.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.