GATE Computer ScienceVery hardGATE CS

Let #ab(w) and #ba(w) be the numbers of occurrences of the substrings ab and ba in w ∈ {a, b}*. Which one of the following languages is regular?

  1. A. { w : n_a(w) = n_b(w) }
  2. B. { w : #ab(w) = #ba(w) }
  3. C. { w : #aa(w) = #bb(w) }
  4. D. { w : #ab(w) ≥ #aa(w) }

Correct answer: B. { w : #ab(w) = #ba(w) }

The occurrences of ab and ba alternate along the string, so #ab − #ba ∈ {0, ±1}, and it is 0 exactly when w is empty or its first and last symbols are equal: a regular condition. For #aa = #bb, intersect with a⁺b⁺ to get n − 1 = m − 1, i.e. aⁿbⁿ, which is not regular.

More Theory of Computation questions

Exams that ask this topic