An answer has already been given, but let me additionally give you a meta-answer: the constraints you describe define a rational language on the alphabet $\{0,1\}$, namely a set of words (=finite strings) on this alphabet which can be described by a regular expression or finite automaton. In your case, a regular expression is easily described:
$$
00^*(111^*000^*)^*111^*00^* + 00^*(111^*000^*)^*11^*
+ 11^*(000^*111^*)^*00^* + 11^*(000^*111^*)^*000^*11^*
+ 0^* + 1^*
$$
where "$^*$" means "any number (at least zero) of", and "$+$" (sometimes written "$|$") means "or" (the cases in the above sum are, moreover, disjoint: the first four describe the four cases where your string begins with $0$ and ends with $0$, begins with $0$ and ends with $1$, etc., and the last two make a special case of the sequences with just zeros or just ones — you might wish to add $\varepsilon$ to the expression if you consider the empty string to match your condition).
Now there are well known algorithms which will
take a regular expression such as above and turn it into a finite automaton recognizing the language,
turn this finite automaton into a deterministic one,
compute the generating function of the language generated by a deterministic finite automaton.
Any book on rational languages or finite automata (e.g., the one by Sakarovitch¹) should discuss at least the first two and probably all three; the third is also discussed, e.g., here.
Alternatively, you can go through the "unambiguous regular expression" path, as discussed here: I don't know which is algorithmically more efficient in general, but in the case of your particular question, this works very well, as the above regular expression is unambiguous, and the procedure described here immediately produces the rational function $2\frac{x+x^2}{1-x-x^2}$.
My point is, this answers not only your particular question, but all questions about counting (or at least, producing a generating function for) the number of words described by any rational expression. Furthermore, these algorithms are actually implemented in the Vaucanson/Vaucanson2/Vaucanson-R/Wali/VCSN programs¹ (none of which are terribly usable at the present, unfortunately).
- Full disclosure: Sakarovitch is my office neighbour.