Skip to content

Commit 7306937

Browse files
authored
Update 1616-split-two-strings-to-make-palindrome.js
1 parent d00c46a commit 7306937

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

1616-split-two-strings-to-make-palindrome.js

+11-20
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,20 @@
33
* @param {string} b
44
* @return {boolean}
55
*/
6-
const checkPalindromeFormation = function(a, b) {
7-
return helper(a, b) || helper(b, a)
8-
};
6+
const checkPalindromeFormation = function (a, b) {
7+
return check(a, b) || check(b, a)
8+
}
99

10-
function helper(A, B) {
11-
const str_len = A.length
12-
let idx = 0
13-
while(A[idx] === B[str_len - idx - 1]) {
14-
idx += 1
10+
function isPalindrome(s, i, j) {
11+
for (; i < j; ++i, --j) {
12+
if (s[i] != s[j]) return false
1513
}
16-
console.log(idx)
17-
if (idx > Math.floor(str_len / 2) ) return true
18-
else if (chk(A.slice(idx + 1, str_len - idx - 2 + 1))) return true
19-
else if (chk(B.slice(idx + 1, str_len - idx - 2 + 1))) return true
20-
else return false
21-
}
14+
return true
15+
}
2216

23-
function chk(s) {
24-
let l = 0, r = s.length - 1
25-
while(l < r) {
26-
if(s[l] !== s[r]) return false
27-
l++
28-
r--
17+
function check(a, b) {
18+
for (let i = 0, j = a.length - 1; i < j; ++i, --j) {
19+
if (a[i] !== b[j]) return isPalindrome(a, i, j) || isPalindrome(b, i, j)
2920
}
3021
return true
3122
}

0 commit comments

Comments
 (0)