LeetCode - Valid Number

2 minute read

Problem description

description

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.

Analysis

The solution for this is to use DFA to find different state and test them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public boolean isNumber(String s) {
    s = s.trim();

    State start = new State("");
    State flag = new State("+-");
    State numberBeforeE = new State("0123456789");
    State point = new State(".");
    State numberAfterPoint = new State("0123456789");
    State e = new State("e");
    State flagAfterE = new State("+-");
    State numberAfterE = new State("0123456789");
    State end = new State("");

    start.next.add(flag);
    start.next.add(numberBeforeE);
    start.next.add(point); // really?

    flag.next.add(numberBeforeE);
    flag.next.add(point); // really?

    numberBeforeE.next.add(point);
    numberBeforeE.next.add(numberBeforeE);
    numberBeforeE.next.add(e);
    numberBeforeE.next.add(end);

    point.next.add(numberAfterPoint);
    point.next.add(end); // really?
    point.next.add(e); // really?

    numberAfterPoint.next.add(numberAfterPoint);
    numberAfterPoint.next.add(e);
    numberAfterPoint.next.add(end);

    e.next.add(numberAfterE);
    e.next.add(flagAfterE);

    flagAfterE.next.add(numberAfterE);

    numberAfterE.next.add(numberAfterE);
    numberAfterE.next.add(end);

    State s1 = start;
    boolean hasNumberBeforeE = false;
    boolean hasNumber = false;
    for (char c : s.toCharArray()) {
      boolean findNext = false;
      for (State state : s1.next) {
        if (state.context.indexOf(c) != -1) {
          s1 = state;
          findNext = true;

          if (s1 == numberAfterE || s1 == numberAfterPoint || s1 == numberBeforeE) {
            hasNumber = true;
          }

          if (s1 == numberBeforeE || s1 == numberAfterPoint) {
            hasNumberBeforeE = true;
          }

          if (s1 == e && hasNumberBeforeE == false) {
            return false;
          }
          break;
        }
      }

      if (!findNext) {
        return false;
      }
    }

    return s1.next.contains(end) && hasNumber;
  }

  class State {
    String context;

    HashSet<State> next = new HashSet<>();

    public State(String s1) {
      context = s1;
    }
  }

What to improve

  • there are some edge cases which makes it difficult to solve.

“.1” => true “3.” => true “.” => false “+.8” => true “46.e3” => true “.e1” => false “.2e81” => true