-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplitStringTwoHalves.java
56 lines (51 loc) · 1010 Bytes
/
SplitStringTwoHalves.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
public class SplitStringTwoHalves
{
public static void main(String[] args)
{
// String s = "x",b = "y";
// String s = "ulacfd"; b = "jizalu";
String s = "abdef", b = "fecab";
System.out.println(checkPalindromeFormation(s,b));
}
//
static boolean checkPalindromeFormation(String s, String b)
{
return check(s,b) || check(b,s);
}
//
static boolean check(String s, String b)
{
int start = 0;
int end = s.length()-1;
while (start<end)
{
if (s.charAt(start)==s.charAt(end))
{
start++;
end--;
}
}
if (start>end) return true;
return ispalindrome(s, start, end) || ispalindrome(b, start, end);
}
//
static boolean ispalindrome(String s,int start,int end)
{
while (start<=end)
{
if (s.charAt(start) != s.charAt(end))
{
return false;
}
start++;
end--;
}
return true;
}
}
/*static boolean checkPalindromeFormation(String a, String b)
{
if (a.length()==1) return true;
String ans = a.substring(0, a.length()/2) + b.substring(b.length()/2, b.length());
return ispalindrome(ans);
} */