Find a missing number in a string -- Amazon - James Chen's Java Home
http://latest.careercup.appspot.com/question?id=6110019401547776
http://geekyjumps.blogspot.in/2013/08/find-missing-number-in-given-string.html
Read full article from Find a missing number in a string -- Amazon - James Chen's Java Home
Str="4142434546" Findout missing no 44.Add it to str;
Output:"414243444546".
public class FindMissingNumInString {
public static boolean ExpectedNextNum(String str, int num, int start) {
if (start + Integer.toString(num).length() > str.length()) {
return false;
}
int expected = Integer.parseInt(
str.substring(start, start + Integer.toString(num).length()));
return (num == expected);
}
public static int FindMissingNum(String str) {
int len = str.length();
int i = 1;
int missingNum = Integer.MIN_VALUE;
while (i <= len / 2) {
int start = i;
int curr = Integer.parseInt(str.substring(0, i));;
boolean result = true;
while (result) {
if (result = ExpectedNextNum(str, curr + 1, start)) {
start += Integer.toString(curr + 1).length();
curr = curr + 1;
} else if (result = ExpectedNextNum(str, curr + 2, start)) {
start += Integer.toString(curr + 2).length();
missingNum = curr + 1;
curr = curr + 2;
}
}
if (start == len) {
return missingNum;
}
i++;
}
return missingNum;
}
http://geekyjumps.blogspot.in/2013/08/find-missing-number-in-given-string.html
Read full article from Find a missing number in a string -- Amazon - James Chen's Java Home