Similar Problems
Similar Problems not available
Valid Number - Leetcode Solution
Companies:
LeetCode: Valid Number Leetcode Solution
Difficulty: Hard
Topics: string
The problem Valid Number on LeetCode is a coding challenge that asks you to determine whether a given string represents a valid number. Here's a detailed solution to the problem:
Approach:
As a first step, we should identify what constitutes a valid number. According to the problem statement, a valid number comprises the following components:
- An optional sign (+/-)
- A sequence of digits
- An optional decimal point (.)
- Another sequence of digits after the decimal point
- An optional exponent (e/E) followed by an optional sign and a sequence of digits
If a string satisfies all the above conditions, it can be considered a valid number. However, there are some additional constraints that we need to consider:
- The input string should contain only digits, signs, decimal points, and exponent indicators. Any other character is considered invalid.
- The sign can appear only at the beginning of the number or right after the exponent indicator.
- The decimal point cannot appear after the exponent indicator.
- The exponent indicator cannot appear multiple times or at the beginning or end of the number.
- The decimal point and exponent indicator cannot appear together.
Taking into account all the above factors, we can come up with the following algorithm to solve the problem:
Algorithm:
- Remove all leading and trailing white spaces from the input string.
- Initialize the following flags to false:
- decimalSeen - set to true if a decimal point is encountered
- exponentSeen - set to true if an exponent indicator is encountered
- numSeen - set to false initially. Set to true as soon as we encounter a digit
- Traverse the input string character by character and do the following:
- If the current character is a digit, set the numSeen flag to true.
- If the current character is a sign (+/-), it can appear only at the beginning of the string or right after the exponent indicator. Check if this condition is satisfied, and move to the next character.
- If the current character is a decimal point, it can appear only once and not after the exponent indicator. Check if this condition is satisfied, set the decimalSeen flag to true, and move on to the next character.
- If the current character is an exponent indicator, it can appear only once and not at the beginning or end of the string. Check if this condition is satisfied, set the exponentSeen flag to true, and move to the next character.
- If the current character is not a valid digit, sign, decimal point, or exponent indicator, return false.
- If the numSeen flag is false, return false as well (we need to have at least one digit in the number). If the exponentSeen flag is true, check that there is at least one digit after the exponent indicator. If not, return false.
Code:
Here's the Python code that implements the above algorithm:
class Solution:
def isNumber(self, s: str) -> bool:
s = s.strip()
n = len(s)
numSeen = False
decimalSeen = False
exponentSeen = False
for i in range(n):
c = s[i]
if c.isdigit():
numSeen = True
elif c in ['+', '-']:
if i > 0 and s[i-1] not in ['e', 'E']:
return False
elif c == '.':
if decimalSeen or exponentSeen:
return False
decimalSeen = True
elif c in ['e', 'E']:
if exponentSeen or not numSeen or i == n-1:
return False
exponentSeen = True
else:
return False
return numSeen
The above code has O(n) time complexity, where n is the length of the input string, and O(1) space complexity, as we are using only a few flags to keep track of the different components of the number.
Valid Number Solution Code
1