## The challenge

You are given a string of numbers in sequence and your task will be to return the missing number. If there is no number missing or there is an error in the sequence, return -1.

For example:

 missing("123567") = 4 missing("899091939495") = 92 missing("9899101102") = 100 missing("599600601602") = -1 -- no number missing missing("8990919395") = -1 -- error in sequence. Both 92 and 94 missing.

The sequence will always be in ascending order.

## The solution in Java code

Option 1:

 public class Solution { public static int missing(String s) { int len = s.length(); if (len == 0 || s.charAt(0) == '0') return -1; outer: for (int k = 1, maxK = Math.min(9, len / 2); k <= maxK; k++) { int n = Integer.parseInt(s.substring(0, k)); int x = 0; int i = k; while (i < len) { String ns = Integer.toString(++n); if (s.startsWith(ns, i)) i += ns.length(); else if (x > 0) continue outer; else x = n; } if (x > 0 && i == len) return x; } return -1; } }

Option 2:

 class Solution{ public static int missing(String s){ int res = 0, limit = Math.min(8,s.length()/2); for (int c = 1; c < limit; ++c) { int temp = 1; int m = Integer.parseInt(s.substring(0, c)); int hold = c; while (hold < s.length()) { m++; String n = String.valueOf(m); if (!n.equals(s.substring(hold, hold+n.length()))) { res = m; temp--; if (temp < 0) break; } else hold += n.length(); } if (temp == 0 && hold == s.length()) return (int)res; } return -1; } }

## Test cases to validate our solution

 import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest{ @Test public void basicTests(){ assertEquals(4,Solution.missing("123567")); assertEquals(92,Solution.missing("899091939495")); assertEquals(100,Solution.missing("9899101102")); assertEquals(-1,Solution.missing("599600601602")); assertEquals(-1,Solution.missing("8990919395")); assertEquals(1002,Solution.missing("998999100010011003")); assertEquals(10000,Solution.missing("99991000110002")); assertEquals(-1,Solution.missing("979899100101102")); assertEquals(900003,Solution.missing("900001900002900004900005900006")); } }