# Question

Formatted question description: https://leetcode.ca/all/65.html

A valid number can be split up into these components (in order):

1. A decimal number or an integer.
2. (Optional) An 'e' or 'E', followed by an integer.

A decimal number can be split up into these components (in order):

1. (Optional) A sign character (either '+' or '-').
2. One of the following formats:
1. One or more digits, followed by a dot '.'.
2. One or more digits, followed by a dot '.', followed by one or more digits.
3. A dot '.', followed by one or more digits.

An integer can be split up into these components (in order):

1. (Optional) A sign character (either '+' or '-').
2. One or more digits.

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"].

Given a string s, return true if s is a valid number.

Example 1:

Input: s = "0"
Output: true


Example 2:

Input: s = "e"
Output: false


Example 3:

Input: s = "."
Output: false


Constraints:

• 1 <= s.length <= 20
• s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-', or dot '.'.

# Algorithm

To solve the minimum path sum problem, we maintain a two-dimensional dp array, where dp[i][j] represents the minimum path sum to the current position (i, j).

Next, we look for the state transition formula. Since there are only two ways to reach the current position (i, j), either from the top (i-1, j) or from the left (i, j-1), we choose the dp value of the smaller path. We compare dp[i-1][j] with dp[i][j-1], and add the smaller value to the current number grid[i][j], which is the current position dp value.

However, there are some special cases that need to be handled in advance. For example, the starting point position, which is directly assigned to grid[0][0], and the first row and column. The position of the first row can only come from the left, and the position of the first column can only come from above. Therefore, we initialize these two lines in advance and then update from the position (1, 1) to the lower right corner.

# Code

• 
public class Valid_Number {

public static void main(String[] args) {

String s = "3";

boolean yes = s.matches("^\\s*[+-]?(\\d+\\.?\\d*|\\d*\\.\\d+)(e[+-]?\\d+)?\\s*$"); // boolean yes = s.matches("\\de\\d+"); System.out.print(yes); } public class Solution { public boolean isNumber(String s) { // @note: matches() // return s.matchAll(); // s.matches("^\\s*[+-]?(\\d+\\.?\\d*|\\d*\\.\\d+)(e[+-]?\\d+)?\\s*"); return s.matches("^\\s*[+-]?(\\d+\\.?\\d*|\\d*\\.\\d+)(e[+-]?\\d+)?\\s*$");

/*
Input:	"."
Output:	true
Expected:	false
*/
// make it 2 parts, with "e" and without "e"
// return s.matches("^\\s*[+-]?d*\\.?\\d*\\s*$") || s.matches("^\\s*[+-]?\\d+e[+-]?\\d+\\s*$");
}
}

}

############

class Solution {
public boolean isNumber(String s) {
int n = s.length();
int i = 0;
if (s.charAt(i) == '+' || s.charAt(i) == '-') {
++i;
}
if (i == n) {
return false;
}
if (s.charAt(i) == '.'
&& (i + 1 == n || s.charAt(i + 1) == 'e' || s.charAt(i + 1) == 'E')) {
return false;
}
int dot = 0, e = 0;
for (int j = i; j < n; ++j) {
if (s.charAt(j) == '.') {
if (e > 0 || dot > 0) {
return false;
}
++dot;
} else if (s.charAt(j) == 'e' || s.charAt(j) == 'E') {
if (e > 0 || j == i || j == n - 1) {
return false;
}
++e;
if (s.charAt(j + 1) == '+' || s.charAt(j + 1) == '-') {
if (++j == n - 1) {
return false;
}
}
} else if (s.charAt(j) < '0' || s.charAt(j) > '9') {
return false;
}
}
return true;
}
}

• // OJ: https://leetcode.com/problems/valid-number/
// Time: O(N)
// Space: O(N)
class Solution {
bool isInteger(string s) {
if (s.empty()) return false;
int i = 0, N = s.size();
if (s[i] == '+' || s[i] == '-') ++i;
if (i == N) return false;
while (i < N && isdigit(s[i])) ++i;
return i == N;
}
bool isDecimal(string s) {
if (s.empty()) return false;
int i = 0, N = s.size();
if (s[i] == '+' || s[i] == '-') ++i;
if (i == N) return false;
bool hasInteger = isdigit(s[i]);
while (i < N && isdigit(s[i])) ++i;
if (i == N || s[i] != '.') return false;
++i;
if (i == N) return hasInteger;
while (i < N && isdigit(s[i])) ++i;
return i == N;
}
public:
bool isNumber(string s) {
auto eIndex = s.find_first_of("eE");
if (eIndex == string::npos) return isDecimal(s) || isInteger(s);
auto first = s.substr(0, eIndex), second = s.substr(eIndex + 1);
return (isDecimal(first) || isInteger(first)) && isInteger(second);
}
};

• class Solution:
def isNumber(self, s: str) -> bool:
n = len(s)
i = 0
if s[i] in '+-':
i += 1
if i == n:
return False
if s[i] == '.' and (i + 1 == n or s[i + 1] in 'eE'):
return False
dot = e = 0
j = i
while j < n:
if s[j] == '.':
if e or dot:
return False
dot += 1
elif s[j] in 'eE':
if e or j == i or j == n - 1:
return False
e += 1
if s[j + 1] in '+-':
j += 1
if j == n - 1:
return False
elif not s[j].isnumeric():
return False
j += 1
return True

############

class States(object):
def __init__(self):
self.init = 0
self.decimal = 1
self.decpoint = 2
self.afterdp = 3
self.e = 4
self.aftere = 5
self.sign = 6
self.nullpoint = 7
self.esign = 8
self.afteresign = 9

class Solution(object):
def isNumber(self, s):
"""
:type s: str
:rtype: bool
"""
s = s.strip()
states = States()
state = states.init
decimals = "01234567890"

for c in s:
if state == states.init:
if c == ".":
state = states.nullpoint
elif c in decimals:
state = states.decimal
elif c in ["+", "-"]:
state = states.sign
else:
return False
elif state == states.sign:
if c in decimals:
state = states.decimal
elif c == ".":
state = states.nullpoint
else:
return False
elif state == states.esign:
if c not in decimals:
return False
state = states.afteresign
elif state == states.afteresign:
if c not in decimals:
return False
elif state == states.nullpoint:
if c not in decimals:
return False
state = states.decpoint
elif state == states.decimal:
if c in decimals:
continue
elif c == "e":
state = states.e
elif c == ".":
state = states.decpoint
else:
return False
elif state == states.decpoint:
if c in decimals:
state = states.afterdp
elif c == "e":
state = states.e
else:
return False
elif state == states.afterdp:
if c in decimals:
continue
elif c == "e":
state = states.e
else:
return False
elif state == states.e:
if c in decimals:
state = states.aftere
elif c in ["+", "-"]:
state = states.esign
else:
return False
elif state == states.aftere:
if c not in decimals:
return False
else:
return False
return state not in [states.init, states.e, states.nullpoint, states.sign, states.esign]


• func isNumber(s string) bool {
i, n := 0, len(s)
if s[i] == '+' || s[i] == '-' {
i++
}
if i == n {
return false
}
if s[i] == '.' && (i+1 == n || s[i+1] == 'e' || s[i+1] == 'E') {
return false
}
var dot, e int
for j := i; j < n; j++ {
if s[j] == '.' {
if e > 0 || dot > 0 {
return false
}
dot++
} else if s[j] == 'e' || s[j] == 'E' {
if e > 0 || j == i || j == n-1 {
return false
}
e++
if s[j+1] == '+' || s[j+1] == '-' {
j++
if j == n-1 {
return false
}
}
} else if s[j] < '0' || s[j] > '9' {
return false
}
}
return true
}

• ﻿using System.Text.RegularExpressions;

public class Solution {
private readonly Regex _isNumber_Regex = new Regex(@"^\s*[+-]?(\d+(\.\d*)?|\.\d+)([Ee][+-]?\d+)?\s*\$");

public bool IsNumber(string s) {
return _isNumber_Regex.IsMatch(s);
}
}