Chapter 10: Advanced String Algorithms

Chapter 10: Advanced String Algorithms · Study Questions
Lexicographic orderRadix sortRegular expressionsFinite state machinesLevenshtein distanceSuffix array and BWT
Sercan KÜLCÜ · Giresun University · Computer Engineering

Output Question · Lexicographic Order

Question 1: Output of compare

What do these three calls return with the lecture's compare method? compare("car", "cart"), compare("Zebra", "apple"), compare("kale", "kaba")

int compare(String a, String b) {
  int n = Math.min(a.length(), b.length());
  for (int k = 0; k < n; k++) {
    char x = a.charAt(k), y = b.charAt(k);
    if (x != y)
      return x - y;   // first difference
  }
  return a.length() - b.length();
}
Show answer
-1
-7
10
  • "car" · "cart": the first 3 characters are equal and the shorter one ran out → 3 − 4 = −1 (a prefix comes first).
  • "Zebra" · "apple": 'Z' (90) − 'a' (97) = −7: uppercase letters come before lowercase ones.
  • "kale" · "kaba": 3rd character 'l' (108) − 'b' (98) = 10 > 0 → "kaba" comes first.

Multiple Choice · Lexicographic Order

Question 2: Character Code Order

What is the result when the array {"b2", "B10", "ab", "Ab", "a"} is sorted with Arrays.sort (that is, compareTo)?

D: compareTo compares character codes: all uppercase letters (A = 65, B = 66) come before lowercase letters (a = 97). "a" comes first because it is a prefix of "ab". A and C treat uppercase and lowercase as equal, like a human alphabet (A also puts b2 before B10, ordering numbers by value); that needs a Collator or compareToIgnoreCase.

Step-by-Step Trace · Radix Sort

Question 3: countingSort Trace

For a = {"cab", "bad", "ace", "dab", "bed", "add"}, what does a become after the call countingSort(a, 1)?

acabbadacedabbedaddaux012345

d = 1 (2nd character). Keys: a, a, c, a, e, d. Counting: cnt[a] = 3, cnt[c] = cnt[d] = cnt[e] = 1.

acabbadacedabbedaddaux012345

Prefix sum: cnt[c] is now the position where bucket c ends: a → 3, b → 3, c → 4, d → 5, e → 6.

acabbadacedabbedaddauxadd012345

i = 5: "add", key 'd' → cnt[d] decreases by one, aux[4] = "add".

acabbadacedabbedaddauxaddbed012345

i = 4: "bed", key 'e' → cnt[e] decreases by one, aux[5] = "bed".

acabbadacedabbedaddauxdabaddbed012345

i = 3: "dab", key 'a' → cnt[a] decreases by one, aux[2] = "dab".

acabbadacedabbedaddauxdabaceaddbed012345

i = 2: "ace", key 'c' → cnt[c] decreases by one, aux[3] = "ace".

acabbadacedabbedaddauxbaddabaceaddbed012345

i = 1: "bad", key 'a' → cnt[a] decreases by one, aux[1] = "bad".

acabbadacedabbedaddauxcabbaddabaceaddbed012345

i = 0: "cab", key 'a' → cnt[a] decreases by one, aux[0] = "cab".

acabbadacedabbedaddauxcabbaddabaceaddbed012345

Result: cab, bad, dab, ace, add, bed. cab, bad, dab, whose key is a, kept their input order (stable); placing from back to front guarantees this.

void countingSort(String[] a, int d) {
  int[] cnt = new int[256];          // ASCII
  for (String s : a) cnt[ch(s, d)]++;
  for (int r = 1; r < 256; r++)
    cnt[r] += cnt[r - 1];
  String[] aux = new String[a.length];
  for (int i = a.length - 1; i >= 0; i--)
    aux[--cnt[ch(a[i], d)]] = a[i];  // stable
  System.arraycopy(aux, 0, a, 0, a.length);
}
int ch(String s, int d) {    // short string → 0
  return d < s.length() ? s.charAt(d) : 0;
}

Multiple Choice · Radix Sort

Question 4: Reverse-Order Placement

If the placement loop in countingSort is written from front to back (for (int i = 0; i < a.length; i++)) instead of back to front, what happens to LSD radix sort?

A: cnt[c] points to the end of the bucket; the first element coming from the front is written at the very end of the bucket, so equal items end up reversed (unstable). The correctness of LSD relies on items that are equal in the last pass keeping the order set by earlier passes; so the work of the first passes is destroyed.

Multiple Choice · Regular Expressions

Question 5: The ab*c Pattern

For which of the following strings does Pattern.compile("ab*c").matcher(s).find() return false?

D: * applies only to the b before it: a, zero or more b's, then c. "ac" (zero b's), "abbc" and "xabcx" contain such a piece (find looks for a piece of the string). In "adc" there is a d between a and c, so there is no match.

Output Question · Regular Expressions

Question 6: matches and find

What does the following code print?

Pattern d = Pattern.compile("\\d+");
System.out.println("abc123".matches("\\d+"));
System.out.println(d.matcher("abc123").find());
System.out.println("2024".matches("\\d{4}"));
System.out.println("20245".matches("\\d{4}"));
String r = "[A-Z]{2}\\d{3}";
System.out.println("AB123".matches(r));
System.out.println("ab123".matches(r));
Show answer
false
true
true
false
true
false
  • matches tries to match the whole string (implicit ^…$): "abc123" does not start with a digit → false.
  • find looks for a piece: "123" is found → true.
  • \d{4} is exactly four digits: "2024" true, "20245" false (the fifth digit is left over).
  • [A-Z] is uppercase letters only: "ab123" false.

Multiple Choice · Regular Expressions

Question 7: Lookahead

How many times does the pattern a(?=b) match in the string "abacab" with find(), and what text is each match?

C: lookahead tests a position and consumes no characters: the a's followed by b are at positions 0 and 4; after the a at position 2 comes c. The match is just "a"; b does not take part in the match (so A is wrong).

Multiple Choice · Regular Expressions

Question 8: Greedy and Lazy

In the text "<b>x</b>", what are the first find() results (group()) of the patterns <.+> and <.+?>, respectively?

A: .+ is greedy; it takes the longest possible piece and runs to the last >. +? is lazy and stops at the first >. A match always starts at the leftmost suitable position (0); so the </b> in C cannot be the first result.

Step-by-Step Trace · Finite State Machines

Question 9: Running a DFA

For the DFA given in the lecture (A start, D accepting), which states does the call accepts(delta, accept, "1011010") pass through, and what is the result?

01→AEBBBCCBDD*DAEEFFFA1011010state: Apath: A

Start state A (0). Transition table: a single target for each (state, symbol); only D is an accepting state.

01→AEBBBCCBDD*DAEEFFFA1011010move: A → Bpath: A B

Symbol 1 is 1: δ(A, 1) = B.

01→AEBBBCCBDD*DAEEFFFA1011010move: B → Bpath: A B B

Symbol 2 is 0: δ(B, 0) = B.

01→AEBBBCCBDD*DAEEFFFA1011010move: B → Cpath: A B B C

Symbol 3 is 1: δ(B, 1) = C.

01→AEBBBCCBDD*DAEEFFFA1011010move: C → Dpath: A B B C D

Symbol 4 is 1: δ(C, 1) = D. D accepts, but input remains.

01→AEBBBCCBDD*DAEEFFFA1011010move: D → Dpath: A B B C D D

Symbol 5 is 0: δ(D, 0) = D. D accepts, but input remains.

01→AEBBBCCBDD*DAEEFFFA1011010move: D → Apath: A B B C D D A

Symbol 6 is 1: δ(D, 1) = A.

01→AEBBBCCBDD*DAEEFFFA1011010move: A → Epath: A B B C D D A E

Symbol 7 is 0: δ(A, 0) = E.

01→AEBBBCCBDD*DAEEFFFA1011010state: Epath: A

Input finished; the final state E is not an accepting state → false (reject). Passing through D along the way is not enough; only the final state matters.

boolean accepts(int[][] delta,
        boolean[] accept, String s) {
  int state = 0;               // start state
  for (char c : s.toCharArray())
    state = delta[state][c - '0'];
  return accept[state];         // accepting?
}

Output Question · Finite State Machines

Question 10: FSM That Parses Numbers

What does the lecture's findNumbers method print for the call findNumbers("v1.2.3 and 7")?

void findNumbers(String s) {
  State d = State.START;
  StringBuilder buf = new StringBuilder();
  for (char c : s.toCharArray()) {
    boolean digit = Character.isDigit(c);
    if (digit) {
      if (d == State.START) d = State.DIGIT;
      buf.append(c);
    } else if (c == '.' && d != State.DECIMAL) {
      d = State.DECIMAL;
      buf.append(c);
    } else if (d != State.START) {
      System.out.println(buf);
      buf.setLength(0);
      d = State.START;
    }
  }
  if (d != State.START)
    System.out.println(buf);
}
Show answer
1.2
3
7
  • "1" → DIGIT, "." → DECIMAL, "2" → buffer "1.2".
  • When the second "." arrives, d is already DECIMAL: the second condition fails and the third branch runs → "1.2" is printed, back to START; the dot is dropped.
  • "3" is a new number; it is printed at the space. "7" is printed at the end of the input by the last if.

The machine splits a version number like "1.2.3" into two numbers; this is a consequence of the state design.

Multiple Choice · Finite State Machines

Question 11: Pattern Automaton

In an automaton searching for the pattern "abab", state q = the length of the longest prefix of the pattern that matches the end of the text. Starting from state 0, which state is the automaton in after reading the text "abaa"?

B: States: a → 1, b → 2, a → 3; the fourth character is a, not the expected b. The longest suffix of "abaa" that is a prefix of "abab" is "a" → q = 1 (it does not drop to 0). This fallback is the automaton form of the prefix function in KMP.

Step-by-Step Trace · Edit Distance

Question 12: levenshtein Trace

How is the table of levenshtein("ELMA", "ALEM") filled row by row, and what is the result?

εALEMε01234E1L2M3A4

5 × 5 table. First column D[i][0] = i (i deletions), first row D[0][j] = j (j insertions). The other cells are still 0.

εALEMε01234E11223LMA

Row 1 (E): 1, 2, 2, 3. In the cell where E = E (j = 3) no cost is added: D[1][3] = D[0][2] = 2 (green).

εALEMε01234E11223L22123MA

Row 2 (L): 2, 1, 2, 3. In the cell where L = L (j = 2) no cost is added: D[2][2] = D[1][1] = 1 (green).

εALEMε01234E11223L22123M33222A

Row 3 (M): 3, 2, 2, 2. In the cell where M = M (j = 4) no cost is added: D[3][4] = D[2][3] = 2 (green).

εALEMε01234E11223L22123M33222A43333

Row 4 (A): 3, 3, 3, 3. In the cell where A = A (j = 1) no cost is added: D[4][1] = D[3][0] = 3 (green).

εALEMε01234E11223L22123M33222A43333

D[4][4] = 3. One solution: three substitutions E→A, M→E, A→M (ELMA → ALMA → ALEA → ALEM). Time O(m · n).

int levenshtein(String a, String b) {
  int m = a.length(), n = b.length();
  int[][] D = new int[m + 1][n + 1];
  for (int i = 0; i <= m; i++) D[i][0] = i;
  for (int j = 0; j <= n; j++) D[0][j] = j;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      if (a.charAt(i - 1) == b.charAt(j - 1))
        D[i][j] = D[i - 1][j - 1];      // equal
      else
        D[i][j] = 1 + Math.min(D[i - 1][j - 1],
                  Math.min(D[i][j - 1],
                           D[i - 1][j]));
  return D[m][n];
}

Output Question · Edit Distance

Question 13: Distance Values

What does the following code print with the lecture's levenshtein method? Give an edit sequence for each.

String[][] c = {{"algoritma", "logaritma"},
  {"pazar", "bazaar"}, {"abc", ""},
  {"kitap", "kitap"}};
for (String[] p : c) {
  int d = levenshtein(p[0], p[1]);
  System.out.println(d);
}
Show answer
3
2
3
0
  • algoritma → logaritma: the leading a is deleted (lgoritma), an o is inserted after l (logoritma), the second o is replaced by a (logaritma) → 3.
  • pazar → bazaar: p is replaced by b, an a is inserted → 2.
  • abc → "": 3 deletions (first column D[3][0] = 3).
  • Identical strings: 0 (metric property d(a, a) = 0).

Multiple Choice · Edit Distance

Question 14: Is a Swap One Operation?

What is levenshtein("saat", "asat")?

B: Swapping two adjacent letters is not a single operation in Levenshtein: s→a and a→s need two substitutions, or one deletion and one insertion → 2. The measure that counts an adjacent swap as one operation is the Damerau-Levenshtein distance.

Output Question · Suffix Array and BWT

Question 15: Suffix Array

Find the suffix array (SA) of the string "kazak". Then explain how you would find the positions where the pattern "ak" occurs in the text by binary search on the SA.

Show answer
rankSAsuffix
03ak
11azak
24k
30kazak
42zak

SA = [3, 1, 4, 0, 2]. The suffixes starting with "ak" form a contiguous range in the sorted list: only rank 0 ("ak") → position 3. Those starting with "a" are ranks 0–1 (positions 3 and 1). The two ends of the range are found by binary search in O(m log n).

Output Question · Suffix Array and BWT

Question 16: BWT Computation

Find the Burrows–Wheeler transform (the L column) of the string "ARABA" and the row index of the original string in the sorted list.

Show answer 0AARAB1ABAAR2ARABA3BAARA4RABAA

Rotations: ARABA, RABAA, ABAAR, BAARA, AARAB → sorted: AARAB, ABAAR, ARABA, BAARA, RABAA.

L = last column = BRAAA, row index 2 (from 0). The three A's were gathered side by side; these repeats then compress well with Move-to-Front and RLE. The F column (AAABR) is the sorted version of L.

Step-by-Step Trace · Suffix Array and BWT

Question 17: Inverse BWT Trace

What text does inverseBWT("kyka$a") reconstruct using the LF mapping?

FLrank0$k01ay02ak13ka04k$05ya1read: —reversed: —

L = "kyka$a" (T = "kayak$"). rank[i]: which occurrence (from 0) of the character L[i] it is in L.

FLrank0$k01ay02ak13ka04k$05ya1read: —reversed: —

F = sort(L) = "$aakky". C[c]: the first row of c in F: $ → 0, a → 1, k → 3, y → 5.

FLrank0$k01ay02ak13ka04k$05ya1read: kreversed: k

k = 0: L[0] = 'k' is read; LF(0) = C['k'] + rank[0] = 3 + 0 = 3.

FLrank0$k01ay02ak13ka04k$05ya1read: kareversed: ak

k = 1: L[3] = 'a' is read; LF(3) = C['a'] + rank[3] = 1 + 0 = 1.

FLrank0$k01ay02ak13ka04k$05ya1read: kayreversed: yak

k = 2: L[1] = 'y' is read; LF(1) = C['y'] + rank[1] = 5 + 0 = 5.

FLrank0$k01ay02ak13ka04k$05ya1read: kayareversed: ayak

k = 3: L[5] = 'a' is read; LF(5) = C['a'] + rank[5] = 1 + 1 = 2.

FLrank0$k01ay02ak13ka04k$05ya1read: kayakreversed: kayak

k = 4: L[2] = 'k' is read; LF(2) = C['k'] + rank[2] = 3 + 1 = 4.

FLrank0$k01ay02ak13ka04k$05ya1read: kayakreversed: kayak

The characters read give the text from back to front: "kayak" reversed → "kayak". O(n) with the rank values.

String inverseBWT(String L) {   // $ is smallest
  int n = L.length();
  int[] cnt = new int[256], rank = new int[n];
  for (int i = 0; i < n; i++)
    rank[i] = cnt[L.charAt(i)]++;
  int[] C = new int[256];  // first row in F
  for (int c = 1; c < 256; c++)
    C[c] = C[c - 1] + cnt[c - 1];
  StringBuilder s = new StringBuilder();
  int i = 0;                 // $ row
  for (int k = 0; k < n - 1; k++) {
    char c = L.charAt(i);
    s.append(c);
    i = C[c] + rank[i];    // LF(i)
  }
  return s.reverse().toString();
}

Classic Question · Radix Sort

Question 18: Why Does LSD Start from the Right?

Why does LSD radix sort start from the rightmost character, and why must every pass be stable? For strings of 3 characters, what property does the array have after k passes?

Show answer
  • Invariant: after k passes the array is sorted by the last k characters (e.g. "dab" and "cab" are together by their last letter after pass 1).
  • The last pass is on the most significant character; the order of items equal in this character was decided by earlier passes. Stability preserves this decision.
  • An unstable inner sort (e.g. quicksort) shuffles equal items; "bed" and "bee" may swap places in the last pass.
  • W passes · O(N + R) → O(W · (N + R)); no comparisons are made.

MSD, on the other hand, starts from the left and sorts each bucket recursively on its own; since it can stop when the distinguishing prefix ends, it suits strings of different lengths.

The End

Chapter 10: Advanced String Algorithms: Study Questions
Lecture slides: Chapter 10: Advanced String Algorithms · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.