forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
60 lines (46 loc) · 1.25 KB
/
Solution.java
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
58
59
60
import java.math.BigInteger;
import java.util.Scanner;
public class Solution {
static boolean check(String s, BigInteger prev) {
if (s.length() == 0) {
return true;
}
if (s.charAt(0) == '0') {
return false;
}
int i = 0;
while (i < s.length()) {
BigInteger current = new BigInteger(s.substring(0, i + 1));
BigInteger diff = current.add(prev.negate());
boolean result = diff.equals(BigInteger.ONE);
if (result) {
return check(s.substring(i + 1, s.length()), current);
}
i++;
}
return false;
}
// Complete the separateNumbers function below.
static String separateNumbers(String s) {
boolean[] res = new boolean[s.length()];
for (int i = 0; i < s.length() - 1; i++) {
res[i] = check(s.substring(i+1, s.length()), new BigInteger(s.substring(0, i+1)));
}
for (int i = 0; i < s.length(); i++) {
if (res[i]) {
return "YES " + s.substring(0, i + 1);
}
}
return "NO";
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String s = scanner.nextLine();
System.out.println(separateNumbers(s));
}
scanner.close();
}
}