-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path1616-split-two-strings-to-make-palindrome.js
46 lines (41 loc) · 1.42 KB
/
1616-split-two-strings-to-make-palindrome.js
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
/**
* 1616. Split Two Strings to Make Palindrome
* https://leetcode.com/problems/split-two-strings-to-make-palindrome/
* Difficulty: Medium
*
* You are given two strings a and b of the same length. Choose an index and split both strings at
* the same index, splitting a into two strings: aprefix and asuffix where a = aprefix + asuffix,
* and splitting b into two strings: bprefix and bsuffix where b = bprefix + bsuffix. Check if
* aprefix + bsuffix or bprefix + asuffix forms a palindrome.
*
* When you split a string s into sprefix and ssuffix, either ssuffix or sprefix is allowed to be
* empty. For example, if s = "abc", then "" + "abc", "a" + "bc", "ab" + "c" , and "abc" + "" are
* valid splits.
*
* Return true if it is possible to form a palindrome string, otherwise return false.
*
* Notice that x + y denotes the concatenation of strings x and y.
*/
/**
* @param {string} a
* @param {string} b
* @return {boolean}
*/
var checkPalindromeFormation = function(a, b) {
return check(a, b) || check(b, a);
function isPalindrome(str, left, right) {
while (left < right) {
if (str[left++] !== str[right--]) return false;
}
return true;
}
function check(str1, str2) {
let left = 0;
let right = str1.length - 1;
while (left < right && str1[left] === str2[right]) {
left++;
right--;
}
return isPalindrome(str1, left, right) || isPalindrome(str2, left, right);
}
};