Is $\{w \in \Sigma ^* : |w|_2 mod 4 = 2\}$ a regular language?

189 Views Asked by At

Given alphabet $\Sigma = \{1,2,3\}$, is $\{w \in \Sigma^* : |w|_2 \bmod 4 = 2\}$ a regular language?

I tried so hard on finding a regular expression but couldn't...

2

There are 2 best solutions below

1
On BEST ANSWER

/([13]*2[13]*2[13]*2[13]*2)*[13]*2[13]*2[13]*/

0
On

As you may know the set of regular languages is precisely the set of languages recognized by some DFA. In your case the DFA is simple to construct, having four states $S_0, S_1, S_2$ $S_3$ where being in $S_q$ represents the fact that the letter $2$ has appeared $n\equiv q\bmod 4$ times so far. The main transitions go $$S_0 \rightarrow_2 S_1 \rightarrow_2 S_2 \rightarrow_2 S_3 \rightarrow_2 S_0.$$ The state $S_2$ is the sole accepting state and $S_0$ is the start state. There are loops for the symbols $1$ and $3$ that don't affect $|w|_2$ e.g. $$ S_0 \rightarrow_{1,3} S_0.$$