Valid Number
This challenge involves validating if a given string represents a valid numerical value. This is a common task in data parsing and input validation, ensuring that only well-formed numbers are processed, preventing errors and unexpected behavior in applications.
Problem Description
You are tasked with creating a function that takes a string as input and returns true if the string represents a valid number, and false otherwise. A valid number can be an integer, a decimal, or a number in scientific notation.
Key Requirements:
- Integers: Can be positive or negative whole numbers (e.g., "0", "123", "-45").
- Decimals: Can have a decimal point, with optional digits before or after it (e.g., "1.23", ".5", "7.").
- Scientific Notation: Can include an exponent part, denoted by 'e' or 'E', followed by an optional sign and an integer (e.g., "2e10", "-90E3", "3e+7", "6e-1").
- Signs: A '+' or '-' sign can appear at the beginning of the number or immediately after 'e'/'E'.
- Valid Combinations: Numbers can be combinations of the above (e.g., "123.45e+6").
Invalid Cases to Consider:
- Empty string.
- Strings with multiple decimal points.
- Strings with multiple 'e'/'E' characters.
- 'e'/'E' without a preceding number.
- 'e'/'E' without a following integer.
- Signs in incorrect positions (e.g., "1e-", "1-e10").
- Strings containing non-numeric characters other than '.', 'e', 'E', '+', '-'.
- Strings that are just a sign or a decimal point.
Expected Behavior:
The function should return true for valid numbers and false for invalid ones.
Examples
Example 1:
Input: "0"
Output: true
Explanation: A simple integer.
Example 2:
Input: "e"
Output: false
Explanation: 'e' alone is not a valid number.
Example 3:
Input: "."
Output: false
Explanation: A single decimal point is not a valid number.
Example 4:
Input: ".1"
Output: true
Explanation: A decimal number with digits after the point.
Example 5:
Input: "2e10"
Output: true
Explanation: A number in scientific notation.
Example 6:
Input: "-90E3"
Output: true
Explanation: A negative number in scientific notation.
Example 7:
Input: "1e"
Output: false
Explanation: 'e' requires an integer exponent.
Example 8:
Input: "e3"
Output: false
Explanation: Scientific notation requires a base number.
Example 9:
Input: "6e-1"
Output: true
Explanation: Scientific notation with a negative exponent.
Example 10:
Input: "123.45e+6"
Output: true
Explanation: A valid combination of decimal and scientific notation.
Example 11:
Input: "123.45.6"
Output: false
Explanation: Multiple decimal points.
Example 12:
Input: "abc"
Output: false
Explanation: Contains non-numeric characters.
Example 13:
Input: ""
Output: false
Explanation: Empty string.
Example 14:
Input: "+"
Output: false
Explanation: Just a sign.
Example 15:
Input: "+.8"
Output: true
Explanation: Valid decimal with a leading sign.
Constraints
- The input will be a single string.
- The string can contain digits ('0'-'9'), '.', '+', '-', 'e', 'E'.
- The length of the input string will be between 0 and 100 characters, inclusive.
- The solution should aim for an efficient time complexity, ideally linear with respect to the length of the input string.
Notes
Consider using a state machine or regular expressions (if allowed by language constraints) to parse the string. Pay close attention to the order and placement of characters like signs, decimal points, and the exponent indicator. A systematic approach to checking for these patterns will be crucial.