A valid number can be split up into these components (in order):
A decimal number can be split up into these components (in order):
An integer can be split up into these components (in order)
Valid | Invalid | |
—————— | ——– | 0 |
2 | e3 | |
0089 | 99e2.5 | |
-0.1 | 1a | |
+3.14 | 1e | |
+6e-1 | -.9 | |
2e10 | 95a54e53 | |
-90e3 | -+3 | |
3e+7 | –6 |
Rules -
- If the character is a digit, set seenDigit = true.
- If the character is a sign, check if it is either the first character of the input, or if the character before it is an exponent. If not, return false.
- If the character is an exponent, first check if we have already seen an exponent or if we have not yet seen a digit. If either is true, then return false. Otherwise, set seenExponent = true, and seenDigit = false. We need to reset seenDigit because after an exponent, we must construct a new integer.
- If the character is a dot, first check if we have already seen either a dot or an exponent. If so, return false. Otherwise, set seenDot = true.
- If the character is anything else, return false.
At the end, return seenDigit. This is one reason why we have to reset seenDigit after seeing an exponent - otherwise an input like “21e” would be incorrectly judged as valid.
Time Complexity - O(n) since we scanned only once through whole string | Space Complexity - O(1)
class Solution {
public boolean isNumber(String s) {
boolean seenDigit = false;
boolean seenExponent = false;
boolean seenDot = false;
for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i);
if (Character.isDigit(curr)) {
seenDigit = true;
} else if (curr == '+' || curr == '-') {
// if we see plus, minus,either it should be at first index or
// item before it should be 'e' or 'E', otherwise its invalid
if (i > 0 && s.charAt(i - 1) != 'e' && s.charAt(i - 1) != 'E') {
return false;
}
} else if (curr == 'e' || curr == 'E') {
// if previously exponent was already seen or
// if we are seeing it first time but have never seen a digit yet
// then its invalid
if (seenExponent || !seenDigit)
return false;
seenExponent = true;
// if we see a exponent, we have to reset the seenDigit flag
// because we need to see a new integer once we have seen exponent
seenDigit = false;
} else if (curr == '.') {
// if we already saw Dot or exponent, then we cannot again have a dot
// only integer is allowed after we saw exponent, not dot
if (seenDot || seenExponent)
return false;
seenDot = true;
} else {
return false;
}
}
return seenDigit;
}
}
| Complete Binary Tree | Given the root of a binary tree, determine if it’s a complete binary tree. (Complete if u go left to right and its filled) Complete Tree Not Complete Tree Notes If you ever encounter a NULL node, you must not encounter a non NULL node after that in a Level Order Traversal, this is the definition of a complete Binary Tree. Traverse by level...
read more
| Custom Sort String | You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously. Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string. Return any permutation...
read more