Created
May 11, 2017 21:18
-
-
Save chrislzm/63b8e5690eaf93de2e0f757d8828147c to your computer and use it in GitHub Desktop.
Coding Problem (Strings)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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