Computer Science/Coding Test
LeetCode: 65. Valid Number
focalpoint
2022. 1. 20. 14:20
First solution is using regular expression.
Below is some useful patterns.
Integer: ^[+-]?\d+$
Flaot: ^[+-]?((\d+(\.\d*)?)|(\.\d+))$
Sicnetific notation: ^[+-]?((\d+(\.\d*)?)|(\.\d+))[eE][+-]?\d+$
class Solution:
def isNumber(self, s: str) -> bool:
import re
pat = re.compile('(^[+-]?((\d+(\.\d*)?)|(\.\d+))$)|(^[+-]?((\d+(\.\d*)?)|(\.\d+))[eE][+-]?\d+$)')
return re.match(pat, s)
Second solution is Deterministic Finite Automation (DFA).
DFA is very similar to finite state machine.
Below is the graph to solve this problem.
class Solution:
def isNumber(self, s: str) -> bool:
dfa = [
{"sign": 2, "dot": 3, "digit": 1},
{"digit": 1, "expo": 5, "dot": 4},
{"digit": 1, "dot":3},
{"digit": 4},
{"digit": 4, "expo": 5},
{"sign": 6, "digit": 7},
{"digit": 7},
{"digit": 7},
]
current_state =0
for char in s:
typ = ''
if char.isdigit():
typ = 'digit'
elif char == '.':
typ = 'dot'
elif char in ['e', 'E']:
typ = 'expo'
elif char in ['+', '-']:
typ = 'sign'
else:
return False
if typ not in dfa[current_state]:
return False
current_state = dfa[current_state][typ]
return current_state in [1, 4, 7]