Valid Number

This article explains about Leetcode’s Valid Number Explanation and Solution.

Validate if a given string can be interpreted as a decimal number.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

  • Numbers 0-9
  • Exponent – “e”
  • Positive/negative sign – “+”/”-“
  • Decimal point – “.”

Of course, the context of these characters also matters in the input.

Explanation – How to approach this problem?

Important things to note:

Corner Case:

  1. Checking the corner cases, what happen if string is null or string is empty, in that case we have to return false.

Reduce unwanted empty spaces from both the ends

  1. Use trim function to remove spaces,
     s = s.trim();

Use flags to track the string easily

   int signCount = 0; 
   boolean eSeen = false;
   boolean numSeen = false;
   boolean dotSeen = false; 

Check Valid or Invalid Character

While enumerating the string through for loop, we need to check it initially, whether it is valid or invalid character.

When a character doesn’t satisfy this condition, we need to return false.

 return c == '.' || c == '+' || c == '-' || c == 'e' || c == 'E'  || c >= '0' && c <= '9';

e or E conditions

  1. e should not be the last one
  2. e should not appear twice and digits must be present in front of it

Decimal conditions

  • . should not appear twice and it should not appear after e
  • if . is the last one, digits should be in front of it, e.g. “7.”

Sign Conditions

  1. Only two signs are allowed, more than two signs, we need to return false;
  2. Sign should not be the last one
  3. Sign can appear in the middle only when e appears in front

Java Program to understand better

public class Solution {
    public boolean isNumber(String s) {

        if (s == null || s.length() == 0)
         return false;
        
        s = s.trim();
        int n = s.length();
        
        // Initialize flags
        int signCount = 0;
        boolean eSeen = false;
        boolean numSeen = false;
        boolean dotSeen = false;
        
	//Enumerating the string values
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            
            // Condition to check valid or invalid character
            if (!isValid(c)) 
            return false;
            
            // For Digit case, no problem
            if (c >= '0' && c <= '9') 
            numSeen= true;
            
            // When we get, e or E
            if (c == 'e' || c == 'E') {
                // e should not appear twice and digits must be in front of it
                if (eSeen || !numSeen) 
                return false;

                // e could not be the last one
                if (i == n - 1) return false;
                
                eSeen= true;
            }
            
            // Decimal Place Conditions
            if (c == '.') {
                // . should not appear twice and it count not appear after e
                if (dotSeen || eSeen) 
                return false;

                // if . is the last one, digits should be in front of it, e.g. "3."
                if (i == n - 1 && !numSeen) 
                return false;
                
                dotSeen= true;
            }
            
            // Treating with signs
            if (c == '+' || c == '-') {
                // no more than 2 signs
                if (signCount == 2) 
                return false;

                // sign cannot be the last one
                if (i == n - 1) 
                return false;

                // sign can appear in the middle only when e appears in front
                if (i > 0 && !eSeen)
                return false;
                
                signCount++;
            }
        }
        
        return true;
    }
    
    boolean isValid(char c) {
        return c == '.' || c == '+' || c == '-' || c == 'e' || c == 'E' || c >= '0' && c <= '9';
    }
}
If you like this article, please subscribe my blog to support me. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s