Search code examples
regexregular-languageformal-languages

find Reg. Expr. over {0,1,2} so last symbol of string is the sum of the symbols so far on the string mod 3.


I'm learning by myself formal languages (Aho's,Hopcroft) but I'm having a hard time with regular expressions.

I've been able to tackle simple tasks but this one has posed a challenge, at least for me. How to solve this if you can't count so far, I'm not used to this type of computation.
There must be some property or something that let me generalize the answer that much that i can put it as a regular expresion.

So far I've devised that is possible that there may be at least 2 o 3 cases:

  • sums mod3=0 if sum=3k
  • sums mod3=1 if sum=3k+1
  • sums mod3=2 if sum=3k+2.

But I've come to realize that there may be many combinations for a sum to happen so can't find the pattern the regular expression must follow.

The string for ex. {122211}0 (braces are for easy read sake) has the zero at the end as it holds that {sum=3k}0, if the sum is "10" from a string for ex. {1222111}1 the case may be {sum=3k+1} so the one has to be at the end, and so on.

This may or not be the right track to tackle the problem but I'm open to any suggestions please, any help is very appreciated.


Solution

  • Here's a hint: think of what distinct final states you can possibly be in. You certainly have at least 3 states, since the number of values can be three different things mod three. Also, you need to have a distinct start state, since the empty string cannot be accepted. Do you need more states?

    Hint2: I think you can easily do this with a DFA using a start state and nine other states, of which exactly three will be accepting.

    EDIT: Once you have a DFA, you can use Kleene's Theorem to construct an equivalent regular expression. If you'd rather go straight for a regular expression, here's another hint: if you're looking at any string of length 3k, you can append: 0; any string of length 1, followed by 1; any string of length 2, followed by 2. So if you can write regular expressions for strings of lengths 3k, 1, and 2, you're practically done.