Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f0558e

Browse files
committedApr 23, 2025
Add solution #1616
1 parent 058da14 commit 3f0558e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,422 LeetCode solutions in JavaScript
1+
# 1,423 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1246,6 +1246,7 @@
12461246
1611|[Minimum One Bit Operations to Make Integers Zero](./solutions/1611-minimum-one-bit-operations-to-make-integers-zero.js)|Hard|
12471247
1614|[Maximum Nesting Depth of the Parentheses](./solutions/1614-maximum-nesting-depth-of-the-parentheses.js)|Easy|
12481248
1615|[Maximal Network Rank](./solutions/1615-maximal-network-rank.js)|Medium|
1249+
1616|[Split Two Strings to Make Palindrome](./solutions/1616-split-two-strings-to-make-palindrome.js)|Medium|
12491250
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12501251
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12511252
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1616. Split Two Strings to Make Palindrome
3+
* https://leetcode.com/problems/split-two-strings-to-make-palindrome/
4+
* Difficulty: Medium
5+
*
6+
* You are given two strings a and b of the same length. Choose an index and split both strings at
7+
* the same index, splitting a into two strings: aprefix and asuffix where a = aprefix + asuffix,
8+
* and splitting b into two strings: bprefix and bsuffix where b = bprefix + bsuffix. Check if
9+
* aprefix + bsuffix or bprefix + asuffix forms a palindrome.
10+
*
11+
* When you split a string s into sprefix and ssuffix, either ssuffix or sprefix is allowed to be
12+
* empty. For example, if s = "abc", then "" + "abc", "a" + "bc", "ab" + "c" , and "abc" + "" are
13+
* valid splits.
14+
*
15+
* Return true if it is possible to form a palindrome string, otherwise return false.
16+
*
17+
* Notice that x + y denotes the concatenation of strings x and y.
18+
*/
19+
20+
/**
21+
* @param {string} a
22+
* @param {string} b
23+
* @return {boolean}
24+
*/
25+
var checkPalindromeFormation = function(a, b) {
26+
return check(a, b) || check(b, a);
27+
28+
function isPalindrome(str, left, right) {
29+
while (left < right) {
30+
if (str[left++] !== str[right--]) return false;
31+
}
32+
return true;
33+
}
34+
35+
function check(str1, str2) {
36+
let left = 0;
37+
let right = str1.length - 1;
38+
39+
while (left < right && str1[left] === str2[right]) {
40+
left++;
41+
right--;
42+
}
43+
44+
return isPalindrome(str1, left, right) || isPalindrome(str2, left, right);
45+
}
46+
};

0 commit comments

Comments
 (0)
Please sign in to comment.