1
class Solution {
2
public boolean checkPalindromeFormation(String a, String b) {
3
// either way of split should give us a palindrome
4
return cut(a, b) || cut(b, a);
5
}
6

7
// method to match letters from both ends
8
private boolean cut(String a, String b) {
9
int i = 0, j = a.length() - 1;
10
// converge from both ends till we have same letters
11
while (i < j && a.charAt(i) == b.charAt(j)) {
12
i++;
13
j--;
14
}
15

16
// the case when we surpassed the mid point from both ends
17
if (i >= j) return true;
18
// the case when there is still a substring left in between
19
// or say we didn't reach the mid point
20
// we will check if that substring is a palindrome or not
21
return isPalindrome(a, i, j) || isPalindrome(b, i, j);
22
}
23

24
// method to check if a string is palindrome
25
private boolean isPalindrome(String s, int i, int j) {
26
while (i < j) {
27
if (s.charAt(i++) != s.charAt(j--)) {
28
return false;
29
}
30
}
31

32
return true;
33
}
34
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0