Monday, May 12, 2014

LeetCode: Valid Number

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.


Referred to the discussion in the forum, this problem can be solved via finite automata.

C++ Code:

/*
 * func: valid_number
 * goal: to check if the current string is a valid number
 * @param s: input string
 * return: true or false
 */
bool valid_number(const char *s){
    enum InputType{
        INVALID,   //0
        SPACE,     //1
        SIGN,      //2
        DIGIT,     //3
        DOT,       //4
        EXPONENT,  //5
        NUM_INPUTS //6
    };
    int transition_table[][NUM_INPUTS] = {
      -1,  0,  3,  1,  2, -1,
      -1,  8, -1,  1,  4,  5,
      -1, -1, -1,  4, -1, -1,
      -1, -1, -1,  1,  2, -1,
      -1,  8, -1,  4, -1,  5,
      -1, -1,  6,  7, -1, -1,
      -1, -1, -1,  7, -1, -1,
      -1,  8, -1,  7, -1, -1,
      -1,  8, -1, -1, -1, -1
    };
    
    int state = 0;
    while(*s != '\0'){
        InputType input_type = INVALID;
        if(isspace(*s)){
            input_type = SPACE;
        }else if(*s == '+' || *s == '-'){
            input_type = SIGN;
        }else if(isdigit(*s)){
            input_type = DIGIT;
        }else if(*s == '.'){
            input_type = DOT;
        }else if(*s == 'e' || *s == 'E'){
            input_type = EXPONENT;
        }
        
        state = transition_table[state][input_type];
        
        if(state == -1){
            return false;
        }
        ++s;
    }
    
    return state == 1 || state == 4 || state == 7 || state == 8;
}

Python Code:

# func: to test the input string is in valid number format
# @param s: input string
# @return: True or False
def valid_number(s):
    # 0 ==> INVALID
    # 1 ==> SPACE
    # 2 ==> SIGN
    # 3 ==> DIGIT
    # 4 ==> DOT
    # 5 ==> EXPONENT
    transition_table = [[-1, 0, 3, 1, 2, -1],
                        [-1, 8, -1, 1, 4, 5],
                        [-1, -1, -1, 4, -1, -1],
                        [-1, -1, -1, 1, 2, -1],
                        [-1, 8, -1, 4, -1, 5],
                        [-1, -1, 6, 7, -1, -1],
                        [-1, -1, -1, 7, -1, -1],
                        [-1, 8, -1, 7, -1, -1],
                        [-1, 8, -1, -1, -1, -1]]
    state = 0
    index = 0
    while index < len(s):
        input_type = 0
        if s[index] == ' ':
            input_type = 1
        elif s[index] == '+' or s[index] == '-':
            input_type = 2
        elif s[index] in '0123456789':
            input_type = 3
        elif s[index] == '.':
            input_type = 4
        elif s[index] == 'e' or s[index] == 'E':
            input_type = 5

        state = transition_table[state][input_type]

        if state == -1:
            return False
        else:
            index += 1

    return state == 1 or state == 4 or state == 7 or state == 8

Reference Link:

1. [closed] Valid Number - LeetCode http://discuss.leetcode.com/questions/241/valid-number

No comments:

Post a Comment