Skip to content

Commit 07f08aa

Browse files
authored
Merge pull request #2076 from PradnyaGaitonde/PradnyaGaitonde-patch-29
Create 0065-valid-number.md
2 parents d5d91c2 + 31bdc34 commit 07f08aa

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
id: valid-number
3+
title: Valid Number(LeetCode)
4+
sidebar_label: 0065-Valid Number
5+
tags:
6+
- String
7+
description: Given a string s, return whether s is a valid number.
8+
---
9+
10+
## Problem Statement
11+
12+
Given a string `s`, return whether `s` is a valid number.
13+
14+
For example, all the following are valid numbers: `"2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"`, while the following are not valid numbers: `"abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"`.
15+
16+
Formally, a valid number is defined using one of the following definitions:
17+
18+
1. An integer number followed by an optional exponent.
19+
2. A decimal number followed by an optional exponent.
20+
An integer number is defined with an optional sign `'-'` or `'+'` followed by digits.
21+
22+
A decimal number is defined with an optional sign `'-'` or `'+'` followed by one of the following definitions:
23+
24+
1. Digits followed by a dot `'.'`.
25+
2. Digits followed by a dot `'.'` followed by digits.
26+
3. A dot `'.'` followed by digits.
27+
An exponent is defined with an exponent notation `'e'` or `'E'` followed by an integer number.
28+
29+
The digits are defined as one or more digits.
30+
31+
### Examples
32+
33+
**Example 1:**
34+
35+
```plaintext
36+
Input: s = "0"
37+
Output: true
38+
```
39+
40+
**Example 2:**
41+
42+
```plaintext
43+
Input: s = "e"
44+
Output: false
45+
```
46+
47+
**Example 3:**
48+
49+
```plaintext
50+
Input: s = "."
51+
Output: false
52+
```
53+
54+
### Constraints
55+
56+
- `1 <= s.length <= 20`
57+
- `s` consists of only English letters (both uppercase and lowercase), digits (`0-9`), plus `'+'`, minus `'-'`, or dot `'.'`.
58+
59+
## Solution
60+
61+
### Approach
62+
63+
#### Algorithm
64+
65+
1. Trim the Input String: Remove leading and trailing whitespace using `s.trim()`.
66+
2. Initialize Flags:
67+
* `pointSeen`: Tracks if a decimal point has been encountered.
68+
* `eSeen`: Tracks if an exponent 'e' has been encountered.
69+
* `numberSeen`: Tracks if at least one digit has been encountered.
70+
* `numberAfterE`: Tracks if at least one digit exists after an 'e'.
71+
3. Iterate Over Each Character:
72+
* If the character is a digit (`0-9`), set `numberSeen` and `numberAfterE` to `true`.
73+
* If the character is a decimal point (`.`), ensure no 'e' or previous decimal point has been seen.
74+
* If the character is an exponent (`e`), ensure no previous 'e' has been seen and that at least one digit has been encountered before it. Set numberAfterE to false.
75+
* If the character is a sign (`+` or `-`), ensure it appears at the beginning or immediately after an 'e'.
76+
* If any other character is encountered, return `false`.
77+
4. Final Check: Ensure that at least one digit has been encountered (`numberSeen` is `true`) and if an 'e' was encountered, ensure there is at least one digit after it (`numberAfterE` is `true`).
78+
79+
#### Implementation
80+
81+
```Java
82+
public boolean isNumber(String s) {
83+
s = s.trim();
84+
85+
boolean pointSeen = false;
86+
boolean eSeen = false;
87+
boolean numberSeen = false;
88+
boolean numberAfterE = true;
89+
for(int i=0; i<s.length(); i++) {
90+
if('0' <= s.charAt(i) && s.charAt(i) <= '9') {
91+
numberSeen = true;
92+
numberAfterE = true;
93+
} else if(s.charAt(i) == '.') {
94+
if(eSeen || pointSeen) {
95+
return false;
96+
}
97+
pointSeen = true;
98+
} else if(s.charAt(i) == 'e') {
99+
if(eSeen || !numberSeen) {
100+
return false;
101+
}
102+
numberAfterE = false;
103+
eSeen = true;
104+
} else if(s.charAt(i) == '-' || s.charAt(i) == '+') {
105+
if(i != 0 && s.charAt(i-1) != 'e') {
106+
return false;
107+
}
108+
} else {
109+
return false;
110+
}
111+
}
112+
113+
return numberSeen && numberAfterE;
114+
}
115+
```
116+
117+
### Complexity Analysis
118+
119+
- **Time complexity**: $O(N)$
120+
- **Space complexity**: $O(1)$
121+
122+
### Conclusion
123+
124+
The solution effectively validates if a string represents a valid number by processing it in a single pass and using flags to ensure the correct format. The key checks include ensuring correct placement of decimal points, exponents, and signs, as well as ensuring the presence of digits both before and after an exponent if present. This approach is efficient with a linear time complexity and constant space complexity.

0 commit comments

Comments
 (0)