deepseek/deepseek-chat-v3.1

Exercise 6: Irregularity inspector

Let n∈N>0n\in\mathbb{N}_{>0}, Σ:={a,b}\Sigma:=\{a,b\}, A,B⊆ΣnA,B\subseteq\Sigma^n. We write ∣x∣A:=∣{u:x=uvw,v∈A}∣|x|_A:=|\{u:x=uvw,v\in A\}|, denoting the number of occurrences of a word in AA in xx. For example, ∣abaaba∣{ab,ba}=4|abaaba|_{\{ab,ba\}}=4.

We want to find out whether the language L′:={w∈Σ∗:∣w∣A=∣w∣B}L':=\{w\in\Sigma^*:|w|_A=|w|_B\} is regular. For this purpose, we define λ(wc):=∣w∣A−∣w∣B\lambda(wc):=|w|_A-|w|_B for w∈Σ∗,c∈Σw\in\Sigma^*,c\in\Sigma. Note that λ\lambda ignores the last character of its argument; in particular, λ(wtz)=λ(wt)+λ(tz)\lambda(wtz)=\lambda(wt)+\lambda(tz) holds for w,z∈Σ∗w,z\in\Sigma^* and t∈Σnt\in\Sigma^n. Obviously, L′L' is regular if and only if L:=λ−1(0)L:=\lambda^{-1}(0) is regular.

Our goal is to show that LL is regular if and only if there do not exist u∈Au\in A, v∈Bv\in B, and x,y∈Σ∗x,y\in\Sigma^* such that λ(uxu)>0>λ(vyv)\lambda(uxu)>0>\lambda(vyv).

Subproblem a (–/5.0P)

Show the “⇒\Rightarrow” direction.
Assume that LL is regular. Suppose for contradiction that there exist u∈Au \in A, v∈Bv \in B, and x,y∈Σ∗x, y \in \Sigma^* such that λ(uxu)>0\lambda(uxu) > 0 and λ(vyv)<0\lambda(vyv) < 0. Let p=λ(uxu)>0p = \lambda(uxu) > 0 and q=λ(vyv)<0q = \lambda(vyv) < 0.

Define the strings sk=(ux)kus_k = (ux)^k u for k∈Nk \in \mathbb{N}. Since sks_k ends with u∈Σnu \in \Sigma^n, for any z∈Σ∗z \in \Sigma^*, λ(skz)=λ(sk)+λ(uz)\lambda(s_k z) = \lambda(s_k) + \lambda(u z). A similar argument shows that λ(sk)=kp\lambda(s_k) = k p for all kk. Thus, λ(sk)=kp\lambda(s_k) = k p, which is distinct for each kk since p>0p > 0.

For sks_k and sk′s_{k'} with k≠k′k \neq k', if they were equivalent under the right congruence of LL, then for all zz, skz∈Ls_k z \in L iff sk′z∈Ls_{k'} z \in L. However, skz∈Ls_k z \in L iff λ(skz)=0\lambda(s_k z) = 0 iff kp+λ(uz)=0k p + \lambda(u z) = 0, and similarly for sk′zs_{k'} z. Thus, for all zz, kp+λ(uz)=0k p + \lambda(u z) = 0 iff k′p+λ(uz)=0k' p + \lambda(u z) = 0. This implies that for all zz, if λ(uz)=−kp\lambda(u z) = -k p then λ(uz)=−k′p\lambda(u z) = -k' p, which requires k=k′k = k', a contradiction.

To show that such a zz exists for each kk, note that λ(u)=0\lambda(u) = 0 and by adding characters or strings, λ(uz)\lambda(u z) can achieve any integer value. Specifically, since λ(vyv)=q<0\lambda(vyv) = q < 0, by adding multiple copies of vyvvyv or by character-by-character addition (which changes λ\lambda by at most 1 each step), for any integer mm, there exists zz such that λ(uz)=m\lambda(u z) = m. In particular, for m=−kpm = -k p, there exists zz such that λ(uz)=−kp\lambda(u z) = -k p, so skz∈Ls_k z \in L but sk′z∉Ls_{k'} z \notin L for k≠k′k \neq k'.

Therefore, the strings sks_k for k∈Nk \in \mathbb{N} are all pairwise inequivalent under the right congruence of LL, implying infinitely many equivalence classes. Thus, LL is not regular, contradicting the assumption. Hence, no such u,v,x,yu, v, x, y exist.

Subproblem b (–/5.0P)

Show the “⇐\Leftarrow” direction.
Assume that there do not exist u∈Au \in A, v∈Bv \in B, and x,y∈Σ∗x, y \in \Sigma^* such that λ(uxu)>0\lambda(uxu) > 0 and λ(vyv)<0\lambda(vyv) < 0. We will show that L=λ−1(0)L = \lambda^{-1}(0) is regular by proving that λ\lambda is bounded on Σ∗\Sigma^*.

Consider the automaton that computes λ(w)\lambda(w) while keeping track of the last n−1n-1 characters of ww. Formally, define states as pairs (s,d)(s, d) where s∈Σn−1s \in \Sigma^{n-1} (with the initial state corresponding to the empty string handled separately) and d∈Zd \in \mathbb{Z}. The transition function is such that reading a string zz from state ss results in a state s′s' and an increment Δ(s,z)\Delta(s,z) so that if λ(w)=d\lambda(w) = d and the last n−1n-1 characters of ww are ss, then for any string zz, λ(wz)=d+Δ(s,z)\lambda(wz) = d + \Delta(s,z), and the new state is determined by the last n−1n-1 characters of wzwz.

Suppose for contradiction that λ\lambda is unbounded. Then there exists a state s∈Σn−1s \in \Sigma^{n-1} and a sequence of strings wiw_i ending with state ss such that ∣λ(wi)∣→∞|\lambda(w_i)| \to \infty. By the pigeonhole principle, there must be cycles from state ss to itself with nonzero effect. Without loss of generality, assume there is a cycle with positive effect: i.e., there exists a string zz such that from state ss, reading zz returns to state ss and Δ(s,z)=δ>0\Delta(s,z) = \delta > 0.

Now, since A⊆ΣnA \subseteq \Sigma^n is nonempty (if AA is empty, then λ(w)≤0\lambda(w) \leq 0 for all ww, and similarly for BB, and boundedness follows easily), pick any u0∈Au_0 \in A. Let s0s_0 be the last n−1n-1 characters of u0u_0. Since the state space is finite and connected (by appending letters, any state can be reached from any other), there exist strings rr and r′r' such that:
- From state ss, reading r′r' goes to state s0s_0 with effect Δ(s,r′)=E′\Delta(s, r') = E'.
- From state s0s_0, reading rr goes to state ss with effect Δ(s0,r)=E\Delta(s_0, r) = E.

Now consider the string x=r′zNrx = r' z^N r for a large integer NN. When read from state s0s_0, this string yields:
Δ(s0,x)=Δ(s0,r′)+N⋅δ+Δ(s,r)=E′+Nδ+E.
\Delta(s_0, x) = \Delta(s_0, r') + N \cdot \delta + \Delta(s, r) = E' + N\delta + E.

For sufficiently large NN, Δ(s0,x)>0\Delta(s_0, x) > 0. Also, reading xx from state s0s_0 returns to state s0s_0 because:
- After r′r', state becomes ss.
- After zNz^N, state remains ss.
- After rr, state becomes s0s_0.

Now consider the string u0xu0u_0 x u_0. We have:
λ(u0xu0)=λ(u0)+Δ(s0,x)+Δ(s0,u0).
\lambda(u_0 x u_0) = \lambda(u_0) + \Delta(s_0, x) + \Delta(s_0, u_0).

Since u0∈Au_0 \in A and reading u0u_0 from state s0s_0 returns to state s0s_0 (because the last n−1n-1 characters of u0u_0 is s0s_0), Δ(s0,u0)\Delta(s_0, u_0) is some finite number. Thus, for sufficiently large NN, λ(u0xu0)>0\lambda(u_0 x u_0) > 0. This contradicts the assumption that no such u∈Au \in A and xx exist with λ(uxu)>0\lambda(uxu) > 0.

A symmetric argument shows that there cannot be any cycle with negative effect. Therefore, all cycles have zero effect. It follows that the value of λ(w)\lambda(w) is determined only by the path from the initial state to the current state, and since the state space is finite, λ\lambda is bounded. Hence, the automaton recognizing LL has finitely many states, and LL is regular.

google/gemini-2.5-pro

Exercise 6: Irregularity inspector

Let n∈N>0n\in\mathbb{N}_{>0}, Σ:={a,b}\Sigma:=\{a,b\}, A,B⊆ΣnA,B\subseteq\Sigma^n. We write ∣x∣A:=∣{u:x=uvw,v∈A}∣|x|_A:=|\{u:x=uvw,v\in A\}|, denoting the number of occurrences of a word in AA in xx. For example, ∣abaaba∣{ab,ba}=4|abaaba|_{\{ab,ba\}}=4.

We want to find out whether the language L′:={w∈Σ∗:∣w∣A=∣w∣B}L':=\{w\in\Sigma^*:|w|_A=|w|_B\} is regular. For this purpose, we define λ(wc):=∣w∣A−∣w∣B\lambda(wc):=|w|_A-|w|_B for w∈Σ∗,c∈Σw\in\Sigma^*,c\in\Sigma. Note that λ\lambda ignores the last character of its argument; in particular, λ(wtz)=λ(wt)+λ(tz)\lambda(wtz)=\lambda(wt)+\lambda(tz) holds for w,z∈Σ∗w,z\in\Sigma^* and t∈Σnt\in\Sigma^n. Obviously, L′L' is regular if and only if L:=λ−1(0)L:=\lambda^{-1}(0) is regular.

Our goal is to show that LL is regular if and only if there do not exist u∈Au\in A, v∈Bv\in B, and x,y∈Σ∗x,y\in\Sigma^* such that λ(uxu)>0>λ(vyv)\lambda(uxu)>0>\lambda(vyv).

Subproblem a (–/5.0P)

Show the “⇒\Rightarrow” direction.
We prove the contrapositive of the contrapositive, which is the original statement. We assume that L={w∈Σ∗∣λ(w)=0}L=\{w\in \Sigma^* \mid \lambda(w)=0\} is regular and show by contradiction that there are no u∈Au\in A, v∈Bv\in B and x,y∈Σ∗x,y\in \Sigma^* with λ(uxu)>0>λ(vyv)\lambda(uxu)>0>\lambda(vyv).

Assume LL is regular. Let's suppose for the sake of contradiction that there exist u∈A,v∈Bu\in A, v\in B and x,y∈Σ∗x,y\in \Sigma^* such that λ(uxu)=c>0\lambda(uxu)=c>0 and λ(vyv)=d<0\lambda(vyv)=d<0.

A key property of λ\lambda is that for any w′∈Σ≥nw' \in \Sigma^{\ge n} and z∈Σ∗z \in \Sigma^*, we have λ(w′z)=λ(w′)+λ(sufn(w′)z)\lambda(w'z) = \lambda(w') + \lambda(suf_n(w')z), where sufn(w′)suf_n(w') is the suffix of w′w' of length nn. This follows from the given identity λ(wtz)=λ(wt)+λ(tz)\lambda(wtz)=\lambda(wt)+\lambda(tz) by setting w′w' as wtwt and noting that t=sufn(wt)t=suf_n(wt), and from the fact that λ(t)=0\lambda(t)=0 for any t∈Σnt\in\Sigma^n.

Let's construct a family of words Wa,b=(ux)au(yv)bvW_{a,b} = (ux)^a u (yv)^b v for integers a,b≥1a, b \ge 1.
The word Wa,bW_{a,b} can be parsed as w′tz′w'tz' where w′=(ux)aw'=(ux)^a, t=ut=u, and z′=(yv)bvz'=(yv)^b v.
Using the given property, we can calculate λ(Wa,b)\lambda(W_{a,b}):
λ(Wa,b)=λ((ux)au)+λ(u(yv)bv). \lambda(W_{a,b}) = \lambda((ux)^a u) + \lambda(u (yv)^b v). 
Let's analyze each term.
1. Let Aa=λ((ux)au)A_a = \lambda((ux)^a u). For a≥2a \ge 2, we can parse (ux)au(ux)^a u as (ux)a−1 u xu(ux)^{a-1} \ u \ xu. This gives the recurrence Aa=λ((ux)a−1u)+λ(uxu)=Aa−1+cA_a = \lambda((ux)^{a-1}u) + \lambda(uxu) = A_{a-1} + c. With A1=λ(uxu)=cA_1=\lambda(uxu)=c, we get Aa=acA_a = ac for a≥1a \ge 1.

2. Let Bb=λ(u(yv)bv)B_b = \lambda(u (yv)^b v). We can parse this as u(yv)b v ϵu (yv)^b \ v \ \epsilon. This gives Bb=λ(u(yv)b)+λ(v)=λ(u(yv)b)B_b = \lambda(u(yv)^b) + \lambda(v) = \lambda(u(yv)^b), since λ(v)=0\lambda(v)=0. To evaluate λ(u(yv)b)\lambda(u(yv)^b), we parse it as u y v (yv)b−1u \ y \ v \ (yv)^{b-1}. This yields λ(u(yv)b)=λ(uyv)+λ(v(yv)b−1)\lambda(u(yv)^b) = \lambda(uyv) + \lambda(v(yv)^{b-1}). Similarly to AaA_a, we can show that λ(v(yv)b−1)=(b−1)d\lambda(v(yv)^{b-1})=(b-1)d. So, Bb=λ(uyv)+(b−1)dB_b = \lambda(uyv) + (b-1)d.

Combining these, λ(Wa,b)=ac+λ(uyv)+(b−1)d\lambda(W_{a,b}) = ac + \lambda(uyv) + (b-1)d. Let K=λ(uyv)−dK = \lambda(uyv)-d. Then λ(Wa,b)=ac+bd+K\lambda(W_{a,b}) = ac + bd + K.
Since c>0,d<0c>0, d<0, the set of values {λ(Wa,b)∣a,b≥1}\{\lambda(W_{a,b}) \mid a,b \ge 1\} is infinite.

Since LL is regular, its Myhill-Nerode equivalence relation ∼L\sim_L has a finite number of classes, say NN.
All words Wa,1=(ux)auyvvW_{a,1} = (ux)^a u yv v (a≥1a \ge 1) have the same suffix of length nn, which is vv.
The set of values {λ(Wa,1)=ac+d+K∣a≥1}\{\lambda(W_{a,1}) = ac+d+K \mid a \ge 1\} is an infinite arithmetic progression.
Consider the N+1N+1 words pa=Wa,1p_a = W_{a,1} for a=1,…,N+1a=1, \dots, N+1. They all have the same suffix vv, but their λ\lambda-values are all distinct. By the Pigeonhole Principle, there must be a1≠a2a_1 \neq a_2 such that pa1∼Lpa2p_{a_1} \sim_L p_{a_2}.
This implies that for any z∈Σ∗z \in \Sigma^*, pa1z∈L⇔pa2z∈Lp_{a_1}z \in L \Leftrightarrow p_{a_2}z \in L, which is equivalent to λ(pa1z)=0⇔λ(pa2z)=0\lambda(p_{a_1}z) = 0 \Leftrightarrow \lambda(p_{a_2}z)=0.
Using the property λ(w′z)=λ(w′)+λ(sufn(w′)z)\lambda(w'z) = \lambda(w') + \lambda(suf_n(w')z), we get:
λ(pa1z)=λ(pa1)+λ(vz)\lambda(p_{a_1}z) = \lambda(p_{a_1}) + \lambda(vz)
λ(pa2z)=λ(pa2)+λ(vz)\lambda(p_{a_2}z) = \lambda(p_{a_2}) + \lambda(vz)

The set of values {λ(vz)∣z∈Σ∗}\{\lambda(vz) \mid z \in \Sigma^*\} is an infinite arithmetic progression (e.g., using z=(yv)bvz=(yv)^b v), so it is unbounded. We can therefore find a z0z_0 such that λ(vz0)=−λ(pa1)\lambda(vz_0) = -\lambda(p_{a_1}).
For this z0z_0:
λ(pa1z0)=λ(pa1)−λ(pa1)=0\lambda(p_{a_1}z_0) = \lambda(p_{a_1}) - \lambda(p_{a_1}) = 0. So pa1z0∈Lp_{a_1}z_0 \in L.
But then:
λ(pa2z0)=λ(pa2)−λ(pa1)=(a2c+d+K)−(a1c+d+K)=(a2−a1)c\lambda(p_{a_2}z_0) = \lambda(p_{a_2}) - \lambda(p_{a_1}) = (a_2c+d+K) - (a_1c+d+K) = (a_2-a_1)c.
Since a1≠a2a_1 \neq a_2 and c≠0c \neq 0, (a2−a1)c≠0(a_2-a_1)c \neq 0. Thus pa2z0∉Lp_{a_2}z_0 \notin L.
This contradicts pa1∼Lpa2p_{a_1} \sim_L p_{a_2}.
The assumption that such u,v,x,yu,v,x,y exist must be false.

Subproblem b (–/5.0P)

Show the “⇐\Leftarrow” direction.
We want to show that if there are no u∈A,v∈B,x,y∈Σ∗u\in A, v\in B, x,y\in\Sigma^* with λ(uxu)>0>λ(vyv)\lambda(uxu)>0>\lambda(vyv), then L=λ−1(0)L=\lambda^{-1}(0) is regular.

The given condition is equivalent to the disjunction of two statements:
1.  For all u∈Au \in A and x∈Σ∗x \in \Sigma^*, λ(uxu)≤0\lambda(uxu) \le 0.
2.  For all v∈Bv \in B and y∈Σ∗y \in \Sigma^*, λ(vyv)≥0\lambda(vyv) \ge 0.

We will prove that if either of these conditions holds, LL is regular. The proof is symmetric for both cases, so we assume the first one holds without loss of generality.

Assumption: For all u∈Au \in A and x∈Σ∗x \in \Sigma^*, we have λ(uxu)≤0\lambda(uxu) \le 0.

Let f(w):=∣w∣A−∣w∣Bf(w) := |w|_A - |w|_B. By definition, λ(wc)=f(w)\lambda(wc) = f(w). The language LL is {wc∣c∈Σ,f(w)=0}\{wc \mid c \in \Sigma, f(w)=0\}. LL is regular if and only if the language L′:={w∈Σ∗∣f(w)=0}L' := \{w \in \Sigma^* \mid f(w)=0\} is regular, since regular languages are closed under concatenation. We will show that L′L' is regular by demonstrating that it can be recognized by a finite automaton.

A DFA for L′L' would need to keep track of the value of f(w)f(w) after reading a prefix ww. The value of f(w)f(w) can be updated upon reading a character cc based on the suffix of ww of length n−1n-1. Let p=Sn−1(w)p=S_{n-1}(w). The update rule is f(wc)=f(w)+δ(p,c)f(wc) = f(w) + \delta(p,c), where δ(p,c)=1\delta(p,c) = 1 if pc∈A∖Bpc \in A \setminus B, −1-1 if pc∈B∖Apc \in B\setminus A, and 00 otherwise.
The state of a DFA could be (Sn−1(w),f(w))(S_{n-1}(w), f(w)). The number of such states is finite if and only if the set of reachable values for f(w)f(w) is finite. While this is not always true, we can show that we only need to distinguish a finite number of value ranges for f(w)f(w).

Our assumption ∀u∈A,x∈Σ∗,λ(uxu)≤0\forall u \in A, x \in \Sigma^*, \lambda(uxu) \le 0 implies that the values of f(w)f(w) are bounded from above. Let's denote this upper bound by MM. A detailed proof of this bound's existence is non-trivial but relies on the fact that any construction that repeatedly introduces words from AA is "damped" by the condition.

Let's assume the set of values {f(w)∣w∈Σ∗}\{f(w) \mid w \in \Sigma^*\} is bounded above by MM. It may or may not be bounded below.
Let Vw={f(z)∣Sn−1(z)=Sn−1(w)}V_w = \{f(z) \mid S_{n-1}(z) = S_{n-1}(w)\}. Let Cp=sup⁡{f(pz′)−f(p)=f(pz′)∣p∈Σn−1,z′∈Σ∗}C_p = \sup \{ f(pz')-f(p)=f(pz') \mid p\in\Sigma^{n-1}, z'\in\Sigma^*\}. The set of all such possible increases is bounded.

Consider the change f(wz)−f(w)f(wz) - f(w). This value depends on zz and on the suffix of ww of length n−1n-1. Let Δ(p,z)=f(pz)−f(p)=f(pz)\Delta(p,z) = f(pz)-f(p) = f(pz) for p∈Σn−1p\in\Sigma^{n-1}. For any word ww ending in pp, f(wz)−f(w)=f(xz)f(wz)-f(w) = f(xz) where w=xpw=xp. From this, we can show that the set of possible increases {f(wz)−f(w)∣z∈Σ∗}\{f(wz)-f(w) \mid z\in\Sigma^*\} is bounded above, say by a constant MΔM_{\Delta}.

Let K>MΔK > M_{\Delta} be an integer. We can construct a DFA for L′L' with states representing (p,k)(p,k) where pp is a suffix of length at most n−1n-1 and kk is the current value of ff.
The set of values for kk that we need to distinguish is finite:
*   k=0k=0: This is the target value for acceptance.
*   Values between 00 and MM: Values can potentially increase or decrease.
*   Values above MM: Our assumption implies there are no such values reachable.
*   Values below −K-K: If f(w)<−Kf(w) < -K, then for any z∈Σ∗z \in \Sigma^*, the new value is f(wz)=f(w)+(f(wz)−f(w))<−K+MΔ<0f(wz) = f(w) + (f(wz)-f(w)) < -K + M_{\Delta} < 0. So f(wz)f(wz) can never be 00. Thus, all values below −K-K can be mapped to a single non-accepting sink state.

So, we define a DFA with states (p,k)(p, k) where p∈Σ≤n−1p \in \Sigma^{\le n-1} and k∈{−K,−K+1,…,⌈M⌉+1}k \in \{-K, -K+1, \dots, \lceil M \rceil+1 \}, plus a sink state q<−Kq_{<-K}.
*   States QQ: (Σ≤n−1×({−K,…,⌈M⌉+1}))∪{q<−K}(\Sigma^{\le n-1} \times (\{-K, \dots, \lceil M \rceil+1\})) \cup \{q_{<-K}\}
*   Initial state: (ε,0)(\varepsilon, 0).
*   Accepting states FF: {(p,0)∣p∈Σ≤n−1}\{(p, 0) \mid p \in \Sigma^{\le n-1}\}.
*   Transition function τ\tau: For a state (p,k)∈Q(p,k) \in Q and symbol c∈Σc \in \Sigma:
    *   Let p′p' be the new suffix (i.e. pcpc if ∣p∣<n−1|p|<n-1, or (pc)2..n(pc)_{2..n} if ∣p∣=n−1|p|=n-1).
    *   Let k′=k+δ(p,c)k' = k + \delta(p,c).
    *   If k′<−Kk' < -K, τ((p,k),c)=q<−K\tau((p,k), c) = q_{<-K}.
    *   Otherwise, τ((p,k),c)=(p′,k′)\tau((p,k), c) = (p', k').
This defines a finite automaton. As justified, any word ww for which f(w)<−Kf(w) < -K will lead to the sink state q<−Kq_{<-K}, and no extension of ww can be in L′L'. Any word ww with f(w)>Mf(w) > M is unreachable. Words with f(w)∈[−K,M]f(w) \in [-K, M] are tracked precisely. The automaton thus correctly recognizes L′L'.

Since L′L' is regular, L=L′ΣL=L'\Sigma is also regular. The same logic applies symmetrically if we start with Case 2, where f(w)f(w) would be bounded from below. As one of the two cases must hold, LL is always regular under the problem's condition.

openai/gpt-5

Exercise 6: Irregularity inspector

Let n∈N>0n\in\mathbb{N}_{>0}, Σ:={a,b}\Sigma:=\{a,b\}, A,B⊆ΣnA,B\subseteq\Sigma^n. We write ∣x∣A:=∣{u:x=uvw,v∈A}∣|x|_A:=|\{u:x=uvw,v\in A\}|, denoting the number of occurrences of a word in AA in xx. For example, ∣abaaba∣{ab,ba}=4|abaaba|_{\{ab,ba\}}=4.

We want to find out whether the language L′:={w∈Σ∗:∣w∣A=∣w∣B}L':=\{w\in\Sigma^*:|w|_A=|w|_B\} is regular. For this purpose, we define λ(wc):=∣w∣A−∣w∣B\lambda(wc):=|w|_A-|w|_B for w∈Σ∗,c∈Σw\in\Sigma^*,c\in\Sigma. Note that λ\lambda ignores the last character of its argument; in particular, λ(wtz)=λ(wt)+λ(tz)\lambda(wtz)=\lambda(wt)+\lambda(tz) holds for w,z∈Σ∗w,z\in\Sigma^* and t∈Σnt\in\Sigma^n. Obviously, L′L' is regular if and only if L:=λ−1(0)L:=\lambda^{-1}(0) is regular.

Our goal is to show that LL is regular if and only if there do not exist u∈Au\in A, v∈Bv\in B, and x,y∈Σ∗x,y\in\Sigma^* such that λ(uxu)>0>λ(vyv)\lambda(uxu)>0>\lambda(vyv).

Subproblem a (–/5.0P)

Show the “⇒\Rightarrow” direction.
Assume L is regular. Suppose, for a contradiction, that there exist u∈A, v∈B and x,y∈Σ* with α:=λ(uxu)>0 and β:=-λ(vyv)>0.

For k≥1 define p_k:=(ux)^k u and n_k:=(vy)^k v. By the additivity identity λ(wtz)=λ(wt)+λ(tz) applied at the occurrences of u (resp. v), we get
- λ(p_k)=k·α,
- λ(n_k)=−k·β.

Fix k. Consider suffixes of the form
z = (xu)^r v (yv)^s,
with r,s≥0 to be chosen. Using the identity at u, then at v, one computes
λ(p_k z) = kα + λ(u (xu)^r v (yv)^s) = kα + rα + λ(uv) − sβ.
Thus
λ(p_k z)=0 ⇔ (k+r)α − sβ = −λ(uv).

Let d:=gcd(α,β). Since α,β>0, the linear Diophantine equation (k+r)α − sβ = −λ(uv) has integer solutions (r,s) and, by shifting along the solution lattice by multiples of (β/d, α/d), we can ensure r,s≥0. Choose such r,s, and set z_k:=(xu)^r v (yv)^s. Then λ(p_k z_k)=0, i.e., p_k z_k∈L. But
λ(p_{k+1} z_k) = (k+1)α + rα + λ(uv) − sβ = α ≠ 0,
so p_{k+1} z_k∉L. Hence for every k the two prefixes p_k and p_{k+1} are distinguished by the same suffix z_k. Therefore Σ* has infinitely many Myhill–Nerode equivalence classes w.r.t. L, contradicting the regularity of L.

Thus, if L is regular, no such u∈A, v∈B, x,y∈Σ* with λ(uxu)>0>λ(vyv) can exist.

Subproblem b (–/5.0P)

Show the “⇐\Leftarrow” direction.
Assume there do not exist u∈A, v∈B and x,y∈Σ* with λ(uxu)>0>λ(vyv). We prove that L=λ^{-1}(0) is regular.

View the evolution of λ along the de Bruijn graph on Σ^n:
- Vertices are t∈Σ^n.
- From t to t′ there is an edge labeled by c∈Σ if the last n−1 symbols of t′ equal the last n−1 of t followed by c.
- Assign weight g(t):=1_A(t)−1_B(t)∈{−1,0,1} to every outgoing edge from t. Then for any word w and next letter c, λ(wc)−λ(w)=g(t), where t is the length-n suffix of w (if |w|<n, the increment is 0).

A closed walk starting and ending in t has total weight exactly λ(t z t) for the corresponding label z. Any positive-weight closed walk visits some vertex in A, and any negative-weight closed walk visits some vertex in B; by cyclic rotation we may base such a walk at a vertex of A or B respectively. Hence the hypothesis implies that not both signs occur: either every directed cycle has weight ≥0, or every directed cycle has weight ≤0. By symmetry it suffices to treat the case “all cycles have weight ≥0”.

In this case, along any walk the minimal prefix-sum (relative to its start) is attained before the first repetition of a vertex. Since the de Bruijn graph has N:=|Σ|^n vertices and each step decreases by at most 1, the total possible drop from any starting vertex is bounded by D:=N−1:
for every vertex t and every path z, λ(tz)−λ(t) ≥ −D.
Consequently, for any word w and any continuation z we have
λ(wz) ≥ λ(w) − D.
In particular, if λ(w) > D then for all z, λ(wz) > 0, so w cannot be extended to reach 0 (and w∉L already).

We now build a DFA recognizing L. Its states store:
- the current suffix of length <n (to determine when increments start and what the next g(·) will be), and
- a clipped counter r∈{−D,−D+1,…,D} recording λ exactly as long as |λ|≤D, with a single rejecting sink state ⊤ representing “λ>D”.

Transitions update the suffix in the usual de Bruijn manner; when the suffix has length n and equals t, the counter is updated by r←r+g(t), except that any attempt to make r>D moves to ⊤; ⊤ is absorbing. A state is accepting iff its counter component equals 0.

Correctness: By induction on the input length, as long as the run is not in ⊤, the counter equals the true λ of the input read so far. If the run ever enters ⊤, then the true λ>D and, by the bound above, no continuation can bring λ back to 0; rejecting forever is thus correct. Therefore this finite automaton recognizes L.

The case “all cycles have weight ≤0” is symmetric (use a lower absorbing state for λ<−D). Hence, under the given hypothesis, L is regular.