Skip to content

Instantly share code, notes, and snippets.

@chrislzm
Created May 11, 2017 21:18
Show Gist options
  • Save chrislzm/63b8e5690eaf93de2e0f757d8828147c to your computer and use it in GitHub Desktop.
Save chrislzm/63b8e5690eaf93de2e0f757d8828147c to your computer and use it in GitHub Desktop.
Coding Problem (Strings)
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++) {
String s = in.next();
boolean valid = false;
long firstx = -1;
// Try each possible starting number
for (int i=1; i<=s.length()/2; ++i) {
long x = Long.parseLong(s.substring(0,i));
firstx = x;
// Build up sequence starting with this number
String test = Long.toString(x);
while (test.length() < s.length()) {
test += Long.toString(++x);
}
// Compare to original
if (test.equals(s)) {
valid = true;
break;
}
}
System.out.println(valid ? "YES " + firstx : "NO");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment