1
/*
2
Max Possible combination of characters in the string has followig parts(stages)
3
:
4
+/- number . number e/E +/- number
5
stages: 0 1 2 3 4 5 6 7
6

7
Now check each characters at there correct stages or not and increament the
8
stage as per the character found at ith position.
9

10
*/
11

12
class Solution {
13
public:
14
bool isNumber(string s) {
15
char stage = 0;
16
for (int i = 0; i < s.size(); ++i) {
17
if ((s[i] == '+' || s[i] == '-') && (stage == 0 || stage == 5)) {
18
stage++;
19
} else if ((s[i] == 'e' || s[i] == 'E') && stage > 1 && stage < 5) {
20
stage = 5;
21
} else if (s[i] == '.' && stage < 3) {
22
// both side of '.' do not have any digit then return false
23
if (stage <= 1 && (i + 1 >= s.size() || !(s[i + 1] >= '0' && s[i + 1] <= '9')))
24
return false;
25
stage = 3;
26
} else if (s[i] >= '0' && s[i] <= '9') {
27
if (!(stage == 2 || stage == 4 || stage == 7)) stage++;
28
if (stage == 1 || stage == 6) stage++;
29
} else
30
return false;
31
}
32
if (stage <= 1 || stage == 5 || stage == 6) return false;
33
return true;
34
}
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0