22Solution By: Reniz Shah
33Topic: Deterministic Finite Automaton (DFA)
44Given a string s, return whether s is a valid number or not
5- Input: s = -90E3
6- Output: True
75Leetcode link: https://leetcode.com/problems/valid-number/description/
86"""
97
@@ -53,100 +51,6 @@ class State(Enum):
5351 State .EXP_NUMBER : {CharType .NUMERIC : State .EXP_NUMBER },
5452}
5553
56- from enum import Enum
57- from typing import Dict
58-
59-
60- class CharType (Enum ):
61- NUMERIC = "NUMERIC"
62- SIGN = "SIGN"
63- EXPONENT = "EXPONENT"
64- DECIMAL = "DECIMAL"
65-
66-
67- class State (Enum ):
68- INITIAL = "INITIAL"
69- SIGNED = "SIGNED"
70- WHOLE = "WHOLE"
71- FRACTIONAL = "FRACTIONAL"
72- FRACTION = "FRACTION"
73- EXPONENTIAL = "EXPONENTIAL"
74- EXP_SIGN = "EXP_SIGN"
75- EXP_NUMBER = "EXP_NUMBER"
76-
77-
78- state_machine : Dict [State , Dict [CharType , State ]] = {
79- State .INITIAL : {
80- CharType .NUMERIC : State .WHOLE ,
81- CharType .SIGN : State .SIGNED ,
82- CharType .DECIMAL : State .FRACTIONAL ,
83- },
84- State .SIGNED : {CharType .NUMERIC : State .WHOLE , CharType .DECIMAL : State .FRACTIONAL },
85- State .WHOLE : {
86- CharType .NUMERIC : State .WHOLE ,
87- CharType .DECIMAL : State .FRACTION ,
88- CharType .EXPONENT : State .EXPONENTIAL ,
89- },
90- State .FRACTIONAL : {CharType .NUMERIC : State .FRACTION },
91- State .FRACTION : {
92- CharType .NUMERIC : State .FRACTION ,
93- CharType .EXPONENT : State .EXPONENTIAL ,
94- },
95- State .EXPONENTIAL : {
96- CharType .NUMERIC : State .EXP_NUMBER ,
97- CharType .SIGN : State .EXP_SIGN ,
98- },
99- State .EXP_SIGN : {CharType .NUMERIC : State .EXP_NUMBER },
100- State .EXP_NUMBER : {CharType .NUMERIC : State .EXP_NUMBER },
101- }
102-
103- from enum import Enum
104- from typing import Dict
105-
106-
107- class CharType (Enum ):
108- NUMERIC = "NUMERIC"
109- SIGN = "SIGN"
110- EXPONENT = "EXPONENT"
111- DECIMAL = "DECIMAL"
112-
113-
114- class State (Enum ):
115- INITIAL = "INITIAL"
116- SIGNED = "SIGNED"
117- WHOLE = "WHOLE"
118- FRACTIONAL = "FRACTIONAL"
119- FRACTION = "FRACTION"
120- EXPONENTIAL = "EXPONENTIAL"
121- EXP_SIGN = "EXP_SIGN"
122- EXP_NUMBER = "EXP_NUMBER"
123-
124-
125- state_machine : Dict [State , Dict [CharType , State ]] = {
126- State .INITIAL : {
127- CharType .NUMERIC : State .WHOLE ,
128- CharType .SIGN : State .SIGNED ,
129- CharType .DECIMAL : State .FRACTIONAL ,
130- },
131- State .SIGNED : {CharType .NUMERIC : State .WHOLE , CharType .DECIMAL : State .FRACTIONAL },
132- State .WHOLE : {
133- CharType .NUMERIC : State .WHOLE ,
134- CharType .DECIMAL : State .FRACTION ,
135- CharType .EXPONENT : State .EXPONENTIAL ,
136- },
137- State .FRACTIONAL : {CharType .NUMERIC : State .FRACTION },
138- State .FRACTION : {
139- CharType .NUMERIC : State .FRACTION ,
140- CharType .EXPONENT : State .EXPONENTIAL ,
141- },
142- State .EXPONENTIAL : {
143- CharType .NUMERIC : State .EXP_NUMBER ,
144- CharType .SIGN : State .EXP_SIGN ,
145- },
146- State .EXP_SIGN : {CharType .NUMERIC : State .EXP_NUMBER },
147- State .EXP_NUMBER : {CharType .NUMERIC : State .EXP_NUMBER },
148- }
149-
15054
15155def classify_char (char : str ) -> CharType | None :
15256 """
@@ -179,9 +83,6 @@ def classify_char(char: str) -> CharType | None:
17983 >>> classify_char('0')
18084 <CharType.NUMERIC: 'NUMERIC'>
18185 >>> classify_char('01')
182- 'decimal'
183- >>> classify_char('r')
184-
18586 """
18687 if len (char ) != 1 :
18788 return None
@@ -199,15 +100,12 @@ def classify_char(char: str) -> CharType | None:
199100def is_valid_number (number_string : str ) -> bool :
200101 """
201102 This function checks if the input string represents a valid number.
202-
203103 It uses a finite state machine to parse the input string,
204104 transitioning between states based on the character type.
205105 The function returns True if the input string represents a valid number,
206106 and False otherwise.
207-
208107 A valid number is defined as a string that can be parsed into an
209108 integer, decimal, or exponent.
210-
211109 >>> is_valid_number("2")
212110 True
213111 >>> is_valid_number("0089")
0 commit comments