-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergedStringChecker.java
53 lines (45 loc) · 1.46 KB
/
MergedStringChecker.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
package com.smlnskgmail.jaman.codewarsjava.kyu5;
// https://www.codewars.com/kata/54c9fcad28ec4c6e680011aa
public class MergedStringChecker {
private final String string;
private final String firstPart;
private final String secondPart;
public MergedStringChecker(
String string,
String firstPart,
String secondPart
) {
this.string = string;
this.firstPart = firstPart;
this.secondPart = secondPart;
}
public boolean solution() {
return isMerge(string, firstPart, secondPart);
}
private boolean isMerge(
String string,
String firstPart,
String secondPart
) {
if (string.equals("")) {
return firstPart.equals("") && secondPart.equals("");
}
boolean isMergeFromPart1 = false;
boolean isMergeFromPart2 = false;
if (firstPart.length() > 0 && string.charAt(0) == firstPart.charAt(0)) {
isMergeFromPart1 = isMerge(
string.substring(1),
firstPart.substring(1),
secondPart
);
}
if (secondPart.length() > 0 && string.charAt(0) == secondPart.charAt(0)) {
isMergeFromPart2 = isMerge(
string.substring(1),
firstPart,
secondPart.substring(1)
);
}
return isMergeFromPart1 || isMergeFromPart2;
}
}