2
2
Solution By: Reniz Shah
3
3
Topic: Deterministic Finite Automaton (DFA)
4
4
Given a string s, return whether s is a valid number or not
5
- Input: s = -90E3
6
- Output: True
7
5
Leetcode link: https://leetcode.com/problems/valid-number/description/
8
6
"""
9
7
@@ -53,100 +51,6 @@ class State(Enum):
53
51
State .EXP_NUMBER : {CharType .NUMERIC : State .EXP_NUMBER },
54
52
}
55
53
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
-
150
54
151
55
def classify_char (char : str ) -> CharType | None :
152
56
"""
@@ -179,9 +83,6 @@ def classify_char(char: str) -> CharType | None:
179
83
>>> classify_char('0')
180
84
<CharType.NUMERIC: 'NUMERIC'>
181
85
>>> classify_char('01')
182
- 'decimal'
183
- >>> classify_char('r')
184
-
185
86
"""
186
87
if len (char ) != 1 :
187
88
return None
@@ -199,15 +100,12 @@ def classify_char(char: str) -> CharType | None:
199
100
def is_valid_number (number_string : str ) -> bool :
200
101
"""
201
102
This function checks if the input string represents a valid number.
202
-
203
103
It uses a finite state machine to parse the input string,
204
104
transitioning between states based on the character type.
205
105
The function returns True if the input string represents a valid number,
206
106
and False otherwise.
207
-
208
107
A valid number is defined as a string that can be parsed into an
209
108
integer, decimal, or exponent.
210
-
211
109
>>> is_valid_number("2")
212
110
True
213
111
>>> is_valid_number("0089")
0 commit comments