-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankPsAlgorithms0080.java
More file actions
57 lines (42 loc) · 1.23 KB
/
HackerrankPsAlgorithms0080.java
File metadata and controls
57 lines (42 loc) · 1.23 KB
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
import java.io.*;
import java.util.stream.*;
public class HackerrankPsAlgorithms0080 {
// Separate the Numbers
// https://www.hackerrank.com/challenges/separate-the-numbers/problem?isFullScreen=true
static class Result {
public static void separateNumbers(String s) {
int n = s.length();
boolean found = false;
for (int i = 1; i <= n / 2; i++) {
String prefix = s.substring(0, i);
long firstNum = Long.parseLong(prefix);
StringBuilder generatedString = new StringBuilder(prefix);
while (generatedString.length() < n) {
firstNum++;
generatedString.append(firstNum);
}
if (generatedString.toString().equals(s)) {
System.out.println("YES " + prefix);
found = true;
break;
}
}
if (!found) {
System.out.println("NO");
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int q = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, q).forEach(qItr -> {
try {
String s = bufferedReader.readLine();
Result.separateNumbers(s);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
}
}