Skip to content

Commit cee0771

Browse files
Create: 1849-splitting-a-string-into-descending-consecutive-values.java
Signed-off-by: Aliaksei <a1exandddrovich@gmail.com>
1 parent 08c263e commit cee0771

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.math.BigInteger;
2+
class Solution {
3+
public boolean splitString(String s) {
4+
for (int i = 0; i < s.length() - 1; i++) {
5+
BigInteger curValue = new BigInteger(s.substring(0, i + 1));
6+
if (backtrack(i + 1, s, curValue)) {
7+
return true;
8+
}
9+
}
10+
11+
return false;
12+
}
13+
14+
private boolean backtrack(int startIndex,
15+
String s,
16+
BigInteger lastValue) {
17+
if (startIndex >= s.length()) {
18+
return true;
19+
}
20+
21+
for (int i = startIndex; i < s.length(); i++) {
22+
BigInteger curValue = new BigInteger(s.substring(startIndex, i + 1));
23+
if (lastValue.subtract(curValue).compareTo(BigInteger.ONE) == 0 &&
24+
backtrack(i + 1, s, curValue)) {
25+
return true;
26+
}
27+
}
28+
29+
return false;
30+
}
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy