ABC words: Difference between revisions
Content added Content deleted
(Added C++ solution) |
m (Simplified C++ code) |
||
Line 159: | Line 159: | ||
bool is_abc_word(const std::string& word) { |
bool is_abc_word(const std::string& word) { |
||
bool a = false; |
|||
bool b = false; |
|||
for ( |
for (char ch : word) { |
||
switch ( |
switch (ch) { |
||
case 'a': |
case 'a': |
||
if (!a) |
|||
a = true; |
|||
a = pos; |
|||
break; |
break; |
||
case 'b': |
case 'b': |
||
if (b |
if (!b) { |
||
// fail if we haven't seen 'a' yet |
// fail if we haven't seen 'a' yet |
||
if (a |
if (!a) |
||
return false; |
return false; |
||
b = true; |
|||
b = pos; |
|||
} |
} |
||
break; |
break; |
||
case 'c': |
case 'c': |
||
// succeed iff we've seen 'b' already |
// succeed iff we've seen 'b' already |
||
return b |
return b; |
||
} |
} |
||
} |
} |