Skip to content

Commit db2beb9

Browse files
committed
+ problem 1616
1 parent ac0c364 commit db2beb9

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 1616. Split Two Strings to Make Palindrome
2+
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: <code>a<sub>prefix</sub></code> and <code>a<sub>suffix</sub></code> where <code>a = a<sub>prefix</sub> + a<sub>suffix</sub></code>, and splitting `b` into two strings: <code>b<sub>prefix</sub></code> and <code>b<sub>suffix</sub></code> where <code>b = b<sub>prefix</sub> + b<sub>suffix</sub></code>. Check if <code>a<sub>prefix</sub> + b<sub>suffix</sub></code> or <code>b<sub>prefix</sub> + a<sub>suffix</sub></code> forms a palindrome.
3+
4+
When you split a string `s` into <code>s<sub>prefix</sub></code> and <code>s<sub>suffix</sub></code>, either <code>s<sub>suffix</sub></code> or <code>s<sub>prefix</sub></code> is allowed to be empty. For example, if `s = "abc"`, then `"" + "abc"`, `"a" + "bc"`, `"ab" + "c"` , and `"abc" + ""` are valid splits.
5+
6+
Return `true` *if it is possible to form a palindrome string, otherwise return* `false`.
7+
8+
**Notice** that `x + y` denotes the concatenation of strings `x` and `y`.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> a = "x", b = "y"
13+
<strong>Output:</strong> true
14+
<strong>Explanation:</strong> If either a or b are palindromes the answer is true since you can split in the following way:
15+
aprefix = "", asuffix = "x"
16+
bprefix = "", bsuffix = "y"
17+
Then, aprefix + bsuffix = "" + "y" = "y", which is a palindrome.
18+
</pre>
19+
20+
#### Example 2:
21+
<pre>
22+
<strong>Input:</strong> a = "xbdef", b = "xecab"
23+
<strong>Output:</strong> false
24+
</pre>
25+
26+
#### Example 3:
27+
<pre>
28+
<strong>Input:</strong> a = "ulacfd", b = "jizalu"
29+
<strong>Output:</strong> true
30+
<strong>Explanation:</strong> Split them at index 3:
31+
aprefix = "ula", asuffix = "cfd"
32+
bprefix = "jiz", bsuffix = "alu"
33+
Then, aprefix + bsuffix = "ula" + "alu" = "ulaalu", which is a palindrome.
34+
</pre>
35+
36+
#### Constraints:
37+
* <code>1 <= a.length, b.length <= 10<sup>5</sup></code>
38+
* `a.length == b.length`
39+
* `a` and `b` consist of lowercase English letters
40+
41+
## Solutions (Rust)
42+
43+
### 1. Solution
44+
```Rust
45+
impl Solution {
46+
pub fn check_palindrome_formation(a: String, b: String) -> bool {
47+
let a = a.as_bytes();
48+
let b = b.as_bytes();
49+
let n = a.len();
50+
let mut prefix_i = -1;
51+
52+
for i in 0..n / 2 {
53+
if a[i] == b[n - 1 - i] {
54+
prefix_i = i as i32;
55+
} else {
56+
break;
57+
}
58+
}
59+
for i in 0..n / 2 {
60+
if b[i] == a[n - 1 - i] {
61+
prefix_i = prefix_i.max(i as i32);
62+
} else {
63+
break;
64+
}
65+
}
66+
67+
if prefix_i == n as i32 / 2 - 1 {
68+
return true;
69+
}
70+
71+
for i in (0..=n / 2).rev() {
72+
if a[i] == a[n - 1 - i] && i as i32 - 1 <= prefix_i {
73+
return true;
74+
} else if a[i] != a[n - 1 - i] {
75+
break;
76+
}
77+
}
78+
for i in (0..=n / 2).rev() {
79+
if b[i] == b[n - 1 - i] && i as i32 - 1 <= prefix_i {
80+
return true;
81+
} else if b[i] != b[n - 1 - i] {
82+
break;
83+
}
84+
}
85+
86+
false
87+
}
88+
}
89+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 1616. 分割两个字符串得到回文串
2+
给你两个字符串 `a``b` ,它们长度相同。请你选择一个下标,将两个字符串都在 **相同的下标** 分割开。由 `a` 可以得到两个字符串: <code>a<sub>prefix</sub></code> 和 <code>a<sub>suffix</sub></code> ,满足 <code>a = a<sub>prefix</sub> + a<sub>suffix</sub></code> ,同理,由 `b` 可以得到两个字符串 <code>b<sub>prefix</sub></code> 和 <code>b<sub>suffix</sub></code> ,满足 <code>b = b<sub>prefix</sub> + b<sub>suffix</sub></code> 。请你判断 <code>a<sub>prefix</sub> + b<sub>suffix</sub></code> 或者 <code>b<sub>prefix</sub> + a<sub>suffix</sub></code> 能否构成回文串。
3+
4+
当你将一个字符串 `s` 分割成 <code>s<sub>prefix</sub></code> 和 <code>s<sub>suffix</sub></code> 时, <code>s<sub>suffix</sub></code> 或者 <code>s<sub>prefix</sub></code> 可以为空。比方说, `s = "abc"` 那么 `"" + "abc"``"a" + "bc"``"ab" + "c"``"abc" + ""` 都是合法分割。
5+
6+
如果 **能构成回文字符串** ,那么请返回 `true`,否则返回 `false`
7+
8+
**注意**`x + y` 表示连接字符串 `x``y`
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> a = "x", b = "y"
13+
<strong>输出:</strong> true
14+
<strong>解释:</strong> 如果 a 或者 b 是回文串,那么答案一定为 true ,因为你可以如下分割:
15+
aprefix = "", asuffix = "x"
16+
bprefix = "", bsuffix = "y"
17+
那么 aprefix + bsuffix = "" + "y" = "y" 是回文串。
18+
</pre>
19+
20+
#### 示例 2:
21+
<pre>
22+
<strong>输入:</strong> a = "xbdef", b = "xecab"
23+
<strong>输出:</strong> false
24+
</pre>
25+
26+
#### 示例 3:
27+
<pre>
28+
<strong>输入:</strong> a = "ulacfd", b = "jizalu"
29+
<strong>输出:</strong> true
30+
<strong>解释:</strong> 在下标为 3 处分割:
31+
aprefix = "ula", asuffix = "cfd"
32+
bprefix = "jiz", bsuffix = "alu"
33+
那么 aprefix + bsuffix = "ula" + "alu" = "ulaalu" 是回文串。
34+
</pre>
35+
36+
#### 提示:
37+
* <code>1 <= a.length, b.length <= 10<sup>5</sup></code>
38+
* `a.length == b.length`
39+
* `a``b` 都只包含小写英文字母
40+
41+
## 题解 (Rust)
42+
43+
### 1. 题解
44+
```Rust
45+
impl Solution {
46+
pub fn check_palindrome_formation(a: String, b: String) -> bool {
47+
let a = a.as_bytes();
48+
let b = b.as_bytes();
49+
let n = a.len();
50+
let mut prefix_i = -1;
51+
52+
for i in 0..n / 2 {
53+
if a[i] == b[n - 1 - i] {
54+
prefix_i = i as i32;
55+
} else {
56+
break;
57+
}
58+
}
59+
for i in 0..n / 2 {
60+
if b[i] == a[n - 1 - i] {
61+
prefix_i = prefix_i.max(i as i32);
62+
} else {
63+
break;
64+
}
65+
}
66+
67+
if prefix_i == n as i32 / 2 - 1 {
68+
return true;
69+
}
70+
71+
for i in (0..=n / 2).rev() {
72+
if a[i] == a[n - 1 - i] && i as i32 - 1 <= prefix_i {
73+
return true;
74+
} else if a[i] != a[n - 1 - i] {
75+
break;
76+
}
77+
}
78+
for i in (0..=n / 2).rev() {
79+
if b[i] == b[n - 1 - i] && i as i32 - 1 <= prefix_i {
80+
return true;
81+
} else if b[i] != b[n - 1 - i] {
82+
break;
83+
}
84+
}
85+
86+
false
87+
}
88+
}
89+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
impl Solution {
2+
pub fn check_palindrome_formation(a: String, b: String) -> bool {
3+
let a = a.as_bytes();
4+
let b = b.as_bytes();
5+
let n = a.len();
6+
let mut prefix_i = -1;
7+
8+
for i in 0..n / 2 {
9+
if a[i] == b[n - 1 - i] {
10+
prefix_i = i as i32;
11+
} else {
12+
break;
13+
}
14+
}
15+
for i in 0..n / 2 {
16+
if b[i] == a[n - 1 - i] {
17+
prefix_i = prefix_i.max(i as i32);
18+
} else {
19+
break;
20+
}
21+
}
22+
23+
if prefix_i == n as i32 / 2 - 1 {
24+
return true;
25+
}
26+
27+
for i in (0..=n / 2).rev() {
28+
if a[i] == a[n - 1 - i] && i as i32 - 1 <= prefix_i {
29+
return true;
30+
} else if a[i] != a[n - 1 - i] {
31+
break;
32+
}
33+
}
34+
for i in (0..=n / 2).rev() {
35+
if b[i] == b[n - 1 - i] && i as i32 - 1 <= prefix_i {
36+
return true;
37+
} else if b[i] != b[n - 1 - i] {
38+
break;
39+
}
40+
}
41+
42+
false
43+
}
44+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@
10101010
[1610][1610l]|[Maximum Number of Visible Points][1610] |![rs]
10111011
[1614][1614l]|[Maximum Nesting Depth of the Parentheses][1614] |![rb]&nbsp;&nbsp;![rs]
10121012
[1615][1615l]|[Maximal Network Rank][1615] |![rs]
1013+
[1616][1616l]|[Split Two Strings to Make Palindrome][1616] |![rs]
10131014
[1619][1619l]|[Mean of Array After Removing Some Elements][1619] |![rb]&nbsp;&nbsp;![rs]
10141015
[1620][1620l]|[Coordinate With Maximum Network Quality][1620] |![rs]
10151016
[1621][1621l]|[Number of Sets of K Non-Overlapping Line Segments][1621] |![py]
@@ -2565,6 +2566,7 @@
25652566
[1610]:Problemset/1610-Maximum%20Number%20of%20Visible%20Points/README.md#1610-maximum-number-of-visible-points
25662567
[1614]:Problemset/1614-Maximum%20Nesting%20Depth%20of%20the%20Parentheses/README.md#1614-maximum-nesting-depth-of-the-parentheses
25672568
[1615]:Problemset/1615-Maximal%20Network%20Rank/README.md#1615-maximal-network-rank
2569+
[1616]:Problemset/1616-Split%20Two%20Strings%20to%20Make%20Palindrome/README.md#1616-split-two-strings-to-make-palindrome
25682570
[1619]:Problemset/1619-Mean%20of%20Array%20After%20Removing%20Some%20Elements/README.md#1619-mean-of-array-after-removing-some-elements
25692571
[1620]:Problemset/1620-Coordinate%20With%20Maximum%20Network%20Quality/README.md#1620-coordinate-with-maximum-network-quality
25702572
[1621]:Problemset/1621-Number%20of%20Sets%20of%20K%20Non-Overlapping%20Line%20Segments/README.md#1621-number-of-sets-of-k-non-overlapping-line-segments
@@ -4119,6 +4121,7 @@
41194121
[1610l]:https://leetcode.com/problems/maximum-number-of-visible-points/
41204122
[1614l]:https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/
41214123
[1615l]:https://leetcode.com/problems/maximal-network-rank/
4124+
[1616l]:https://leetcode.com/problems/split-two-strings-to-make-palindrome/
41224125
[1619l]:https://leetcode.com/problems/mean-of-array-after-removing-some-elements/
41234126
[1620l]:https://leetcode.com/problems/coordinate-with-maximum-network-quality/
41244127
[1621l]:https://leetcode.com/problems/number-of-sets-of-k-non-overlapping-line-segments/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@
10101010
[1610][1610l]|[可见点的最大数目][1610] |![rs]
10111011
[1614][1614l]|[括号的最大嵌套深度][1614] |![rb]&nbsp;&nbsp;![rs]
10121012
[1615][1615l]|[最大网络秩][1615] |![rs]
1013+
[1616][1616l]|[分割两个字符串得到回文串][1616] |![rs]
10131014
[1619][1619l]|[删除某些元素后的数组均值][1619] |![rb]&nbsp;&nbsp;![rs]
10141015
[1620][1620l]|[网络信号最好的坐标][1620] |![rs]
10151016
[1621][1621l]|[大小为 K 的不重叠线段的数目][1621] |![py]
@@ -2565,6 +2566,7 @@
25652566
[1610]:Problemset/1610-Maximum%20Number%20of%20Visible%20Points/README_CN.md#1610-可见点的最大数目
25662567
[1614]:Problemset/1614-Maximum%20Nesting%20Depth%20of%20the%20Parentheses/README_CN.md#1614-括号的最大嵌套深度
25672568
[1615]:Problemset/1615-Maximal%20Network%20Rank/README_CN.md#1615-最大网络秩
2569+
[1616]:Problemset/1616-Split%20Two%20Strings%20to%20Make%20Palindrome/README_CN.md#1616-分割两个字符串得到回文串
25682570
[1619]:Problemset/1619-Mean%20of%20Array%20After%20Removing%20Some%20Elements/README_CN.md#1619-删除某些元素后的数组均值
25692571
[1620]:Problemset/1620-Coordinate%20With%20Maximum%20Network%20Quality/README_CN.md#1620-网络信号最好的坐标
25702572
[1621]:Problemset/1621-Number%20of%20Sets%20of%20K%20Non-Overlapping%20Line%20Segments/README_CN.md#1621-大小为-k-的不重叠线段的数目
@@ -4119,6 +4121,7 @@
41194121
[1610l]:https://leetcode.cn/problems/maximum-number-of-visible-points/
41204122
[1614l]:https://leetcode.cn/problems/maximum-nesting-depth-of-the-parentheses/
41214123
[1615l]:https://leetcode.cn/problems/maximal-network-rank/
4124+
[1616l]:https://leetcode.cn/problems/split-two-strings-to-make-palindrome/
41224125
[1619l]:https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/
41234126
[1620l]:https://leetcode.cn/problems/coordinate-with-maximum-network-quality/
41244127
[1621l]:https://leetcode.cn/problems/number-of-sets-of-k-non-overlapping-line-segments/

0 commit comments

Comments
 (0)