65. Valid Number
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to validate if a given string s represents a valid number according to a specific, detailed set of rules. These rules define what constitutes an integer, a decimal number, and an exponent. A valid number can be an integer or a decimal, optionally followed by an exponent. This is a classic string parsing problem where the main challenge lies in correctly handling all the combinations and edge cases defined by the rules.
Solution
Valid Number in Python
class Solution:
def isNumber(self, s: str) -> bool:
# DFA transition table
# Rows are states 0-8. Columns are input types:
# 0: sign (+/-), 1: digit (0-9), 2: dot (.), 3: exponent (e/E)
dfa = [
# state sign digit dot exp
# 0 Start
{"sign": 1, "digit": 2, "dot": 3, "exp": -1},
# 1 Sign
{"sign": -1, "digit": 2, "dot": 3, "exp": -1},
# 2 Integer
{"sign": -1, "digit": 2, "dot": 4, "exp": 6},
# 3 DotNoLd
{"sign": -1, "digit": 5, "dot": -1, "exp": -1},
# 4 DotLd
{"sign": -1, "digit": 5, "dot": -1, "exp": 6},
# 5 Frac
{"sign": -1, "digit": 5, "dot": -1, "exp": 6},
# 6 Exp
{"sign": 7, "digit": 8, "dot": -1, "exp": -1},
# 7 ExpSign
{"sign": -1, "digit": 8, "dot": -1, "exp": -1},
# 8 ExpNum
{"sign": -1, "digit": 8, "dot": -1, "exp": -1},
]
# Set of accepting states
accepting_states = {2, 4, 5, 8}
current_state = 0
for char in s:
char_type = ''
if char in '+-':
char_type = 'sign'
elif char.isdigit():
char_type = 'digit'
elif char == '.':
char_type = 'dot'
elif char.lower() == 'e':
char_type = 'exp'
else:
return False # Invalid character
# Transition to the next state
if current_state == -1 or dfa[current_state][char_type] == -1:
return False # Invalid transition
current_state = dfa[current_state][char_type]
# After iterating through the string, check if the final state is an accepting one
return current_state in accepting_states
Valid Number in Java
class Solution {
public boolean isNumber(String s) {
// DFA transition table
// Rows are states 0-8. Columns are input types:
// 0: sign (+/-), 1: digit (0-9), 2: dot (.), 3: exponent (e/E)
int[][] dfa = {
// state sign digit dot exp
/* 0 Start */ { 1, 2, 3, -1 },
/* 1 Sign */ { -1, 2, 3, -1 },
/* 2 Integer*/{ -1, 2, 4, 6 },
/* 3 DotNoLd*/{ -1, 5, -1, -1 },
/* 4 DotLd */{ -1, 5, -1, 6 },
/* 5 Frac */{ -1, 5, -1, 6 },
/* 6 Exp */ { 7, 8, -1, -1 },
/* 7 ExpSign*/{ -1, 8, -1, -1 },
/* 8 ExpNum */{ -1, 8, -1, -1 }
};
// Set of accepting states
// States 2, 4, 5, 8 are accepting states
boolean[] acceptingStates = {false, false, true, false, true, true, false, false, true};
int currentState = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int charType;
if (c == '+' || c == '-') {
charType = 0; // sign
} else if (Character.isDigit(c)) {
charType = 1; // digit
} else if (c == '.') {
charType = 2; // dot
} else if (c == 'e' || c == 'E') {
charType = 3; // exp
} else {
return false; // Invalid character
}
// Transition to the next state
if (currentState == -1 || dfa[currentState][charType] == -1) {
return false; // Invalid transition
}
currentState = dfa[currentState][charType];
}
// After iterating through the string, check if the final state is an accepting one
return currentState != -1 && acceptingStates[currentState];
}
}
Valid Number in C++
class Solution {
public:
bool isNumber(string s) {
// DFA transition table
// Rows are states 0-8. Columns are input types:
// 0: sign (+/-), 1: digit (0-9), 2: dot (.), 3: exponent (e/E)
vector<vector<int>> dfa = {
// state sign digit dot exp
/* 0 Start */ { 1, 2, 3, -1 },
/* 1 Sign */ { -1, 2, 3, -1 },
/* 2 Integer*/{ -1, 2, 4, 6 },
/* 3 DotNoLd*/{ -1, 5, -1, -1 },
/* 4 DotLd */{ -1, 5, -1, 6 },
/* 5 Frac */{ -1, 5, -1, 6 },
/* 6 Exp */ { 7, 8, -1, -1 },
/* 7 ExpSign*/{ -1, 8, -1, -1 },
/* 8 ExpNum */{ -1, 8, -1, -1 }
};
// Set of accepting states
// States 2, 4, 5, 8 are accepting states
vector<bool> accepting_states = {false, false, true, false, true, true, false, false, true};
int current_state = 0;
for (char c : s) {
int char_type;
if (c == '+' || c == '-') {
char_type = 0; // sign
} else if (isdigit(c)) {
char_type = 1; // digit
} else if (c == '.') {
char_type = 2; // dot
} else if (c == 'e' || c == 'E') {
char_type = 3; // exp
} else {
return false; // Invalid character
}
// Transition to the next state
if (current_state == -1 || dfa[current_state][char_type] == -1) {
return false; // Invalid transition
}
current_state = dfa[current_state][char_type];
}
// After iterating through the string, check if the final state is an accepting one
return current_state != -1 && accepting_states[current_state];
}
};
Valid Number in TypeScript
function isNumber(s: string): boolean {
// DFA transition table
// Rows are states 0-8. Columns are input types:
// 0: sign (+/-), 1: digit (0-9), 2: dot (.), 3: exponent (e/E)
const dfa: number[][] = [
// state sign digit dot exp
/* 0 Start */ [ 1, 2, 3, -1 ],
/* 1 Sign */ [ -1, 2, 3, -1 ],
/* 2 Integer*/[ -1, 2, 4, 6 ],
/* 3 DotNoLd*/[ -1, 5, -1, -1 ],
/* 4 DotLd */[ -1, 5, -1, 6 ],
/* 5 Frac */[ -1, 5, -1, 6 ],
/* 6 Exp */ [ 7, 8, -1, -1 ],
/* 7 ExpSign*/[ -1, 8, -1, -1 ],
/* 8 ExpNum */[ -1, 8, -1, -1 ]
];
// Set of accepting states
const acceptingStates: Set<number> = new Set([2, 4, 5, 8]);
let currentState = 0;
for (const char of s) {
let charType: number;
if (char === '+' || char === '-') {
charType = 0; // sign
} else if (char >= '0' && char <= '9') {
charType = 1; // digit
} else if (char === '.') {
charType = 2; // dot
} else if (char === 'e' || char === 'E') {
charType = 3; // exp
} else {
return false; // Invalid character
}
// Transition to the next state
if (currentState === -1 || dfa[currentState][charType] === -1) {
return false; // Invalid transition
}
currentState = dfa[currentState][charType];
}
// After iterating through the string, check if the final state is an accepting one
return acceptingStates.has(currentState);
}
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.