Re: Count strings

One other solution.. just thinking to define several states that we can go.

There are 7 states - a, c, cc, b(transition state), ba, bc, bcc.

And there are rules we can define for moving the states.

From 'a' we can go 'a', 'c', or 'b'.

From 'c' we can go 'a', 'cc', or 'b'

From 'cc' we can go 'a', or 'b'

From 'b' we can go 'ba' or 'bc'

From 'ba' we can go 'ba' or 'bc'

From 'bc' we can go 'ba' or 'bcc'

From 'bcc' we can go 'ba'

The point here is just setting b as a transition state so once we get 'b', you cannot go back to the states for 'a', or 'c', but new state 'ba' or 'bc'. Also, I use 'c' and 'cc' so that 'cc' can only go to the 'a', so no way to make 'ccc'.

Maybe you can make a smaller code than this but the idea is defining states to move. I didn't test my code for bigger input, so let me know if you find some bugs here.

```
int countString(int n) {
if (n <= 0) {
return 0;
}
int a = 1;
int c = 1;
int cc = 0;
int b = 1;
int ba = 0;
int bc = 0;
int bcc = 0;
while (--n > 0) {
int _a = a + c + cc;
int _c = a;
int _cc = c;
int _b = a + c + cc;
int _ba = b + ba + bcc + bc;
int _bc = b + ba;
int _bcc = bc;
a = _a;
b = _b;
c = _c;
cc = _cc;
ba = _ba;
bc = _bc;
bcc = _bcc;
}
return a + c + cc + b + ba + bc + bcc;
}
```