#include #include class Solution { private: int i = 0; std::string s; std::vector digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; bool atEnd() { return this->i == s.length(); } bool test(char c) { if (atEnd()) { return false; } return s.at(i) == c; } bool test(std::vector chars) { for (char c : chars) { if (test(c)) { return true; } } return false; } bool match(char c) { if (test(c)) { i++; return true; } return false; } bool match(std::vector chars) { if (test(chars)) { i++; return true; } return false; } bool many(std::vector chars) { bool matched = false; while (match(chars)) { matched = true; } return matched; } bool digitString() { return many(digits); } bool integer() { match({'+', '-'}); return digitString(); } bool decimal() { if (!integer()) { return match('.') && digitString(); } else { match('.'); digitString(); return true; } } bool exponent() { if (match({'e', 'E'})) { return integer(); } return true; } bool number() { bool success = decimal(); return success && exponent(); } public: bool isNumber(std::string s) { this->s = s; return number() && this->atEnd(); } };