-2

I am trying to solve one of the sample test problems in which I have to write an expression/pattern for the following language:

$\{w \in \{0, 1, 2\}^*: {\#}_{0}(w) ∗ {\#}_{1}(w) \text{ is even}\}$

I was thinking about the approach of getting all strings with even number of 0s and 1s first and then make a complement of that expression, but now I'm not so sure if my solution would also include all variations, that is:

  • even number of 0s and odd number of 1s
  • odd number of 0s and even number of 1s
  • even number of 0s and even number of 1s

My solution: $\overline{2^{*}(002^{*}|112^{*})^{*} 012^{*}}$

Mostly I'm not so sure about points 1. and 2. and would gladly accept any help in dispelling my doubts and solving this issue.

crystalsky
  • 31
  • 4
  • https://cs.stackexchange.com/q/45570/755 – D.W. Jun 13 '22 at 20:34
  • 1
    Please edit the question to include your solution as text (or, better, text with MathJax formatting). Images should not be used unless there is no alternative, because they cannot be edited, copied, or quoted, and are of no use to people who use screen-readers. – rici Jun 13 '22 at 20:49

1 Answers1

1

I don't know whether writing the complement of a regular expression will satisfy the requirements of your test, but anyway it's not correct. $2^∗(002^∗|112^∗)^∗012^∗$ must start with $0$, $11$, or $2$, so neither $100$ nor $101$ are included, and so both of them will show up in the complement. But they both satisfy the criterion, since the product of population counts in both cases is 2.

All the same, you're actually very close to solving this problem. You've already enumerated three of the four possible categories of inputs (the three which satisfy the criterion). It's only necessary to add the fourth possibility and show the transitions between them with each new input, in order to construct a finite automaton with four states. From that it's straight-forward (although a bit messy) to produce a regular expression, or trivial to produce a regular grammar.

rici
  • 12,020
  • 21
  • 38