Valid Number
The problem of determining if a given string represents a valid number involves various rules and conditions. We’ll build a solution that checks all these conditions one by one.
Here’s a Python method to determine if a given string is a valid number:


This code defines two helper functions, isInteger
and isDecimal
, which check if a given string is a valid integer or decimal respectively. Then, the main code splits the given string around ’e’ or ‘E’ and checks if the parts adhere to the rules for a valid number.
The given examples will produce the expected output:


Identifying Problem Isomorphism
“String to Integer (atoi)” shares some similarities with the “Valid Number” problem.
In “Valid Number”, you’re given a string s, and the task is to return true if s is a valid number. A string could be considered as a valid number if it is in a format that could be converted to a real number.
“String to Integer (atoi)” presents a similar problem. Here, the function first discards as many whitespace characters as necessary until the first nonwhitespace character is found. Then, starting from this character, it takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The similarities is in the need to parse the string and determine if it can be converted to a number (integer in one case and real number in the other). Both problems require a good understanding of state machines or regular expressions to solve efficiently.
“Valid Number” is more complex because of the various conditions that make a string a valid real number, such as handling decimal points, ’e’ or ‘E’ for scientific notation, and plus/minus signs. “String to Integer (atoi)” is simpler as it mostly deals with integers and fewer special cases.
10 Prerequisite LeetCode Problems
The “65. Valid Number” problem is a string parsing problem that tests your ability to implement a finite state machine or regular expression to parse and validate numeric input. Here are 10 problems that involve similar concepts:
“8. String to Integer (atoi)”: This problem also involves parsing a string and converting it to a number, dealing with potential overflow and invalid inputs.
“20. Valid Parentheses”: This problem involves using a stack to track the state of the input string, similar to how you might track the state of a finite state machine.
“125. Valid Palindrome”: This problem also involves validating string input and ignores nonalphanumeric characters.
“28. Implement strStr()”: This problem is about finding a substring within a larger string, which can help you practice parsing strings.
“67. Add Binary”: In this problem, you need to convert binary number strings to integers, add them together, and then convert back to a binary string. This gives practice in parsing number strings and dealing with potential overflow.
“680. Valid Palindrome II”: This problem extends the “Valid Palindrome” problem to allow for the removal of one character, which can help you practice dealing with more complex validation logic.
“443. String Compression”: This problem involves manipulating a string inplace, which can help you practice dealing with string parsing and manipulation.
“58. Length of Last Word”: A simpler string parsing problem that involves finding the last word in a string.
“387. First Unique Character in a String”: This problem involves parsing a string and keeping track of the count of each character, which can help you practice using data structures like hashmaps while parsing strings.
“14. Longest Common Prefix”: This problem involves comparing multiple strings, which can help you practice dealing with multiple inputs and edge cases.
While none of these problems are exactly the same as “Valid Number”, they should help you improve your string parsing skills and prepare you for the type of logic and edge cases that you might encounter in the “Valid Number” problem.


Problem Classification
The problem asks to validate if a given string can be interpreted as a real number. This involves understanding and validating against rules of number syntax, hence Syntax or Rule Validation.
Language Agnostic Coding Drills
String Manipulation: Manipulating strings is a common operation in almost all programming languages. Try writing a function or a method that takes in a string and removes leading and trailing whitespaces.
Regular Expressions: Regular expressions are a standard feature in most programming languages. Your first exercise is to create a regular expression that matches a single word in a given string.
Advanced Regular Expressions: Regular expressions can become quite complex. For your next exercise, try creating a regular expression that matches email addresses in a given string.
Using Regular Expressions for Complex Pattern Matching: As your final exercise, create a regular expression that can match both positive and negative integers, floatingpoint numbers, and numbers in scientific notation (e.g. 1e3, 3.14e10). Then write a function or method that uses this regular expression to determine whether a given string is a valid representation of a number.
Targeted Drills in Python
This code makes use of a few different concepts including regular expressions (regex), string manipulation, and Python’s re
module. Let’s break it down into digestible drills:
String manipulation in Python: Python provides several builtin methods for manipulating strings, like the
strip()
function, which is used to remove leading and trailing characters from a string.1 2
s = " Hello, World! " print(s.strip()) # Should print "Hello, World!"
Basic Regular Expressions (regex): Regular expressions are sequences of characters that define a search pattern. Here is a simple exercise to match a word using regex.
1 2 3 4 5
import re pattern = re.compile(r"\bword\b") match = pattern.match("word") print(bool(match)) # Should print True
Advanced Regular Expressions (regex): Regular expressions can become quite complex. Let’s practice with a more advanced pattern. In this example, we’re matching a simple email address pattern.
1 2 3 4 5
import re pattern = re.compile(r"[azAZ09_.+]+@[azAZ09]+\.[azAZ09.]+") match = pattern.match("test.email+alex@leetcode.com") print(bool(match)) # Should print True
Creating a regex pattern for number matching: In the problem, a regex pattern is created to check if a string represents a valid number.
1 2 3 4 5 6
import re # This pattern matches positive/negative integers, floats, and scientific notation pattern = re.compile(r"^[+]?((\d+\.?\d*)(\d*\.?\d+))([eE][+]?\d+)?$") match = pattern.match("123.45e6") print(bool(match)) # Should print True
These drills should give you a solid foundation of the necessary concepts to understand the problem’s solution. This problem is solved by defining a regex pattern that matches all valid number representations. The strip()
function is used to remove any leading and trailing spaces from the input string, and then the regex match()
function is used to check if the input string is a valid number representation.