Chapter 9: String Matching and Compression
Output Question · Brute Force
Question 1: How Many Comparisons?
What value does the lecture's bruteForce method return for the call bruteForce("abracacadabra", "cad"), and how many character comparisons does it make in total?
int bruteForce(String t, String p) { int n = t.length(), m = p.length(); for (int i = 0; i <= n - m; i++) { int j = 0; while (j < m && t.charAt(i + j) == p.charAt(j)) j++; if (j == m) return i; } return -1; }
Show answer
Returns: 6 · comparisons: 11
- s = 0, 1, 2, 3: the first character (a, b, r, a) ≠ c → 4 × 1 = 4
- s = 4: c = c, a = a, c ≠ d → 3
- s = 5: a ≠ c → 1
- s = 6: c, a, d match → 3; since j == m, 6 is returned
4 + 3 + 1 + 3 = 11. On a mismatch the information learned is thrown away; the pattern shifts by just 1 each time.
Multiple Choice · Brute Force
Question 2: Loop Bound Bug
If the condition i <= n - m in bruteForce is mistakenly written as i < n - m, which of the following calls returns a wrong result (−1)?
C: For n = 5, m = 2 the correct shifts are 0..3; the faulty condition tries only 0, 1, 2. "em" occurs only at s = 3 = n − m, so it is not found. "le" is still found because it is at s = 2.
Step-by-Step Trace · Brute Force
Question 3: bruteForce Trace
What value does bruteForce("AABAABAAC", "AAC") return, and how many character comparisons are made?
n = 9, m = 3: the valid shifts are i = 0..6. The pattern will be placed under the text at alignment i = 0.
i = 0: 2 characters matched, t[2] = 'B' ≠ p[2] = 'C' → the pattern shifts by 1.
i = 1: 1 character matched, t[2] = 'B' ≠ p[1] = 'A' → the pattern shifts by 1.
i = 2: t[2] = 'B' ≠ p[0] = 'A' → mismatch on the first character, the pattern shifts by 1.
i = 3: 2 characters matched, t[5] = 'B' ≠ p[2] = 'C' → the pattern shifts by 1.
i = 4: 1 character matched, t[5] = 'B' ≠ p[1] = 'A' → the pattern shifts by 1.
i = 5: t[5] = 'B' ≠ p[0] = 'A' → mismatch on the first character, the pattern shifts by 1.
i = 6: A, A, C all three matched; when j = 3 = m the while loop ends.
j == m → 6 is returned. 15 comparisons in total; the same A's were read again and again at different alignments.
int bruteForce(String t, String p) { int n = t.length(), m = p.length(); for (int i = 0; i <= n - m; i++) { int j = 0; while (j < m && t.charAt(i + j) == p.charAt(j)) j++; if (j == m) return i; } return -1; }
Multiple Choice · KMP
Question 4: LPS Definition
What array does computeLPS("aaaa") return?
A: lps[q] is the length of the longest proper prefix of P[0..q] (one not equal to the string itself) that is also a suffix. For "aaaa" this is "aaa" → 3. B also counts the string itself; D tries only the one-character prefix.
Step-by-Step Trace · KMP
Question 5: computeLPS Trace
What array does computeLPS("ababaca") return?
lps[0] = 0 always (a single character has no proper prefix). k: the length of the prefix matched so far.
p[0] = 'a' ≠ p[1] = 'b' → k stays 0, lps[1] = 0.
p[0] = 'a' = p[2] → k = 1, lps[2] = 1.
p[1] = 'b' = p[3] → k = 2, lps[3] = 2.
p[2] = 'a' = p[4] → k = 3, lps[4] = 3.
p[3] = 'b' ≠ p[5] = 'c' → fall back: k = lps[2] = 1.
p[1] = 'b' ≠ p[5] = 'c' → fall back: k = lps[0] = 0.
p[0] = 'a' ≠ p[5] = 'c' → k stays 0, lps[5] = 0.
p[0] = 'a' = p[6] → k = 1, lps[6] = 1.
Result: lps = [0, 0, 1, 2, 3, 0, 1]. The total fall-back cannot exceed the total increase → O(m).
int[] computeLPS(String p) { int m = p.length(), k = 0; int[] lps = new int[m]; for (int q = 1; q < m; q++) { while (k > 0 && p.charAt(k) != p.charAt(q)) k = lps[k - 1]; if (p.charAt(k) == p.charAt(q)) k++; lps[q] = k; } return lps; }
Multiple Choice · KMP
Question 6: Falling Back on a Mismatch
For the pattern "abcabd", lps = [0, 0, 0, 1, 2, 0]. In a KMP search, "abcab" has matched the text (q = 5) but the next text character is not d. What value does q take and how many positions has the pattern shifted?
D: q = lps[q − 1] = lps[4] = 2, because "ab" is both a prefix and a suffix of the matched "abcab"; the shift is 5 − 2 = 3. B is the mistake of using lps[q] (lps[5] = 0). The text pointer i does not go back; "ab" is not compared again.
Output Question · KMP
Question 7: Three Algorithms, One Text
Text "gel de geleyim", pattern "geleyim". How many character comparisons do brute force, KMP and Boyer-Moore (bad-character rule only) each make before finding the first match? At which position is the match?
Show answer
The match is at s = 7. Comparisons: brute force 17, KMP 15, Boyer-Moore 8.
- Brute force: at s = 0 "gel" matches, '␣' ≠ 'e' (4); s = 1..6 end at the first character (6); at s = 7, 7 → 17.
- KMP (lps is all 0): "gel" (3), '␣' first with p[3] then with p[0] (2), d, e, ␣ (3), geleyim (7) → 15. The text is never re-read.
- Boyer-Moore: at s = 0 the first comparison from the right is t[6] = '␣' ≠ 'm'; ␣ is not in the pattern → shift 7; at s = 7, 7 matches → 8.
Multiple Choice · KMP
Question 8: Why Is KMP Linear?
In a KMP search, why can the total number of fall-backs in the while loop (q = lps[q - 1]) not exceed n?
B: Amortized argument: q increases by at most 1 per step (total ≤ n), every fall-back decreases it by at least 1, and q stays ≥ 0; so the total number of fall-backs is ≤ n and the search is O(n). C is wrong: in KMP, i never goes back; A is wrong too: lps values can be as large as m − 1.
Multiple Choice · Boyer-Moore
Question 9: Why Is There a max(1, …)?
In Boyer-Moore, what happens if the shift s += Math.max(1, j - last[c]) is written as s += j - last[c]?
A: For example, in "abacab", last = 4 for c = a; if the mismatch occurs at j = 3 we get 3 − 4 = −1. max(1, …) guarantees an advance of at least 1; otherwise there would be an infinite loop or a move backwards. B is wrong: a shift of j + 1 is safe, because no alignment that contains c can match.
Step-by-Step Trace · Boyer-Moore
Question 10: boyerMoore Trace
What value does boyerMoore("KARTAL KALKAR", "KALKAR") return?
n = 13, m = 6. Last occurrences: K = 3, A = 4, L = 2, R = 5, others −1. Comparison will go from right to left.
s = 0: p[5] = 'R' ≠ t[5] = 'L'. Bad character: 'L'.
last['L'] = 2 → max(1, 5 − 2) = 3; s = 3.
s = 3: p[5] = 'R' ≠ t[8] = 'A'. Bad character: 'A'.
last['A'] = 4 → max(1, 5 − 4) = 1; s = 4.
s = 4: p[5] = 'R' ≠ t[9] = 'L'. Bad character: 'L'.
last['L'] = 2 → max(1, 5 − 2) = 3; s = 7.
s = 7: R, A, K, L, A, K matched from right to left, j = −1.
j < 0 → 7 is returned. 4 alignments, 9 comparisons in total (brute force tries 8 alignments).
int boyerMoore(String t, String p) { int n = t.length(), m = p.length(); int[] last = lastOccurrence(p); int s = 0; while (s <= n - m) { int j = m - 1; while (j >= 0 && p.charAt(j) == t.charAt(s + j)) j--; if (j < 0) return s; s += Math.max(1, j - last[t.charAt(s + j)]); } return -1; }
Output Question · Boyer-Moore
Question 11: Horspool Shift Table
Build the Horspool shift table for the pattern "BARBER" with the rule shift[c] = m − 1 − (rightmost position of c in P[0..m−2]), or m if c does not occur there. If the text character under the last character of the alignment is B, R and X, how far is the pattern shifted?
Show answer
m = 6; only P[0..4] = B A R B E take part in the table:
| c | B | A | R | E | other |
|---|---|---|---|---|---|
| shift[c] | 5 − 3 = 2 | 5 − 1 = 4 | 5 − 2 = 3 | 5 − 4 = 1 | 6 |
B → 2, R → 3, X → 6. The last character (R, index 5) is not part of the table; R still occurs at index 2, so it shifts by 3. The shift does not look at where the mismatch is; it is always based on the last character of the alignment.
Output Question · Rabin-Karp
Question 12: Rolling Hash
Base B = 10, Q = 11, the characters are digits; T = "26535", m = 3. h("265") = 265 mod 11 = 1 and Bm−1 mod Q = 100 mod 11 = 1. Using the sliding formula hs+1 = (B · (hs − t[s] · Bm−1) + t[s+m]) mod Q, find h("653") and h("535").
Show answer
- h("653") = (10 · (1 − 2 · 1) + 3) mod 11 = (−7) mod 11 = 4 ✓ (653 = 59 · 11 + 4)
- h("535") = (10 · (4 − 6 · 1) + 5) mod 11 = (−15) mod 11 = 7 ✓ (535 = 48 · 11 + 7)
Each new window takes O(1): the outgoing digit is subtracted, the rest shift one place left, and the incoming digit is added. The intermediate result can be negative; since % gives negative values in Java, if (th < 0) th += Q is needed.
Multiple Choice · Rabin-Karp
Question 13: Spurious Hit
Toy hash: the sum of the letter values (a = 1, b = 2, c = 3, d = 4). The pattern "bc" (hash 5) is searched for in the text "adcb". Which windows are spurious hits?
D: The windows are "ad" = 5, "dc" = 7, "cb" = 5. Two windows have the same hash as the pattern but different characters; verification rejects both and "bc" is not in the text. The sum hash is insensitive to order ("cb" also gives 5); that is why a position-weighted polynomial hash is used.
Multiple Choice · Rabin-Karp
Question 14: Many Patterns
500 different virus signatures of the same length are to be searched for in a file in a single pass. Which approach is most suitable?
B: The window hash is updated in O(1) and looked up in a HashSet in O(1) on average; if found, it is verified → expected O(n + k · m). A and D scan the file k times; in C, concatenating the signatures means no single signature is found on its own.
Multiple Choice · RLE
Question 15: When Does RLE Make Things Bigger?
For which of the following inputs does rleCompress make the output longer than the input?
D: There is no consecutive repeat; each character is written as "1" + character: 8 → 16 characters (twice as long). The others shrink: "5a5b" (10 → 4), "4a1b4a" (9 → 6), "11x1y" (12 → 5). RLE catches only consecutive repeats; LZW catches repeating strings like "abab…".
Step-by-Step Trace · LZW
Question 16: lzw Trace
With the initial dictionary A = 0, B = 1, what codes does lzw("BABAABAAA") produce?
The dictionary starts with single characters: A = 0, B = 1; new strings are numbered from 2. w = "".
"B" in dict (1) → w = "B"; look for a longer match.
"BA" not in dict → output code of w = "B": 1; add "BA" = 2; w = "A".
"AB" not in dict → output code of w = "A": 0; add "AB" = 3; w = "B".
"BA" in dict (2) → w = "BA"; look for a longer match.
"BAA" not in dict → output code of w = "BA": 2; add "BAA" = 4; w = "A".
"AB" in dict (3) → w = "AB"; look for a longer match.
"ABA" not in dict → output code of w = "AB": 3; add "ABA" = 5; w = "A".
"AA" not in dict → output code of w = "A": 0; add "AA" = 6; w = "A".
"AA" in dict (6) → w = "AA"; look for a longer match.
Loop finished; the code 6 of the remaining w = "AA" is added. Output: 1 0 2 3 0 6 (9 symbols → 6 codes).
List<Integer> lzw(String s) {
Map<String, Integer> dict = initDict(s);
String w = "";
List<Integer> codes = new ArrayList<>();
for (char c : s.toCharArray()) {
String wc = w + c;
if (dict.containsKey(wc)) {
w = wc;
} else {
codes.add(dict.get(w));
dict.put(wc, dict.size());
w = "" + c;
}
}
if (!w.isEmpty()) codes.add(dict.get(w));
return codes;
}Output Question · LZW
Question 17: LZW Decoding
Initial dictionary A = 0, B = 1, C = 2. Decode the LZW-compressed code sequence 0 1 3 2 4 7. At which code does the decoder meet the "code not yet defined" case?
Show answer
| code | written | added to dictionary |
|---|---|---|
| 0 | A | — |
| 1 | B | 3 = AB |
| 3 | AB | 4 = BA |
| 2 | C | 5 = ABC |
| 4 | BA | 6 = CB |
| 7 | BAB | 7 = BAB (special case) |
Result: ABABCBABAB. When 7 is read, the decoder's dictionary only goes up to 6: e = w + w[0] = "BA" + "B" = "BAB". Check: lzw("ABABCBABAB") again gives 0 1 3 2 4 7.
Multiple Choice · LZW
Question 18: LZ78 Output
Starting with an empty dictionary, which (index, character) pairs does LZ78 turn the string "abababa" into?
C: "a" → (0, a), dictionary 1 = a; "b" → (0, b), 2 = b; "ab": a exists (1), ab does not → (1, b), 3 = ab; "aba": ab exists (3), aba does not → (3, a), 4 = aba. The difference of LZW is that it starts the dictionary with all single characters; so there is no need to write a character in the output.
Classic Question · Comparison
Question 19: Choosing the Algorithm by Scenario
For each scenario choose one of the string matching algorithms from the lecture and give your reasoning:
(a) In data streaming in from the network that cannot be read back, a search with a bounded worst case is needed.
(b) A 12-letter word is searched for in a long English text in an editor.
(c) Thousands of sentences of the same length are searched for in a document (plagiarism check).
Show answer
- (a) KMP: the text pointer never moves backwards (suits a stream); even in the worst case O(n + m).
- (b) Boyer-Moore / Horspool: with a long pattern and a large alphabet the bad-character rule skips almost m characters; the fastest in practice, O(n/m) at best.
- (c) Rabin-Karp: pattern hashes go into a set, each window is updated in O(1) and looked up in the set; expected O(n + k · m).
Brute force needs no preprocessing and is enough for short patterns, but its worst case is O(n · m).