Skip to content

Commit fb5e238

Browse files
committed
1616. Split Two Strings to Make Palindrome: AC
1 parent b89c985 commit fb5e238

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1218,3 +1218,4 @@ mod s1610_maximum_number_of_visible_points;
12181218
mod s1611_minimum_one_bit_operations_to_make_integers_zero;
12191219
mod s1614_maximum_nesting_depth_of_the_parentheses;
12201220
mod s1615_maximal_network_rank;
1221+
mod s1616_split_two_strings_to_make_palindrome;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* [1616] Split Two Strings to Make Palindrome
3+
*
4+
* 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.
5+
* 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.
6+
* Return true if it is possible to form a palindrome string, otherwise return false.
7+
* Notice that x + y denotes the concatenation of strings x and y.
8+
*
9+
* Example 1:
10+
*
11+
* Input: a = "x", b = "y"
12+
* Output: true
13+
* Explaination: If either a or b are palindromes the answer is true since you can split in the following way:
14+
* aprefix = "", asuffix = "x"
15+
* bprefix = "", bsuffix = "y"
16+
* Then, aprefix + bsuffix = "" + "y" = "y", which is a palindrome.
17+
*
18+
* Example 2:
19+
*
20+
* Input: a = "xbdef", b = "xecab"
21+
* Output: false
22+
*
23+
* Example 3:
24+
*
25+
* Input: a = "ulacfd", b = "jizalu"
26+
* Output: true
27+
* Explaination: Split them at index 3:
28+
* aprefix = "ula", asuffix = "cfd"
29+
* bprefix = "jiz", bsuffix = "alu"
30+
* Then, aprefix + bsuffix = "ula" + "alu" = "ulaalu", which is a palindrome.
31+
*
32+
*
33+
* Constraints:
34+
*
35+
* 1 <= a.length, b.length <= 10^5
36+
* a.length == b.length
37+
* a and b consist of lowercase English letters
38+
*
39+
*/
40+
pub struct Solution {}
41+
42+
// problem: https://leetcode.com/problems/split-two-strings-to-make-palindrome/
43+
// discuss: https://leetcode.com/problems/split-two-strings-to-make-palindrome/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
impl Solution {
48+
pub fn check_palindrome_formation(a: String, b: String) -> bool {
49+
let (a, b) = (a.as_bytes(), b.as_bytes());
50+
51+
Self::check(a, b) || Self::check(b, a)
52+
}
53+
54+
fn is_palindrome(s: &[u8], i: usize, j: usize) -> bool {
55+
let (mut i, mut j) = (i, j);
56+
while i < j && s[i] == s[j] {
57+
i += 1;
58+
j -= 1;
59+
}
60+
i >= j
61+
}
62+
63+
fn check(a: &[u8], b: &[u8]) -> bool {
64+
let (mut i, mut j) = (0, a.len() - 1);
65+
66+
while i < j && a[i] == b[j] {
67+
i += 1;
68+
j -= 1;
69+
}
70+
71+
Self::is_palindrome(a, i, j) || Self::is_palindrome(b, i, j)
72+
}
73+
}
74+
75+
// submission codes end
76+
77+
#[cfg(test)]
78+
mod tests {
79+
use super::*;
80+
81+
#[test]
82+
fn test_1616_example_1() {
83+
let a = "x".to_string();
84+
let b = "y".to_string();
85+
86+
let result = true;
87+
88+
assert_eq!(Solution::check_palindrome_formation(a, b), result);
89+
}
90+
91+
#[test]
92+
fn test_1616_example_2() {
93+
let a = "xbdef".to_string();
94+
let b = "xecab".to_string();
95+
96+
let result = false;
97+
98+
assert_eq!(Solution::check_palindrome_formation(a, b), result);
99+
}
100+
101+
#[test]
102+
fn test_1616_example_3() {
103+
let a = "ulacfd".to_string();
104+
let b = "jizalu".to_string();
105+
106+
let result = true;
107+
108+
assert_eq!(Solution::check_palindrome_formation(a, b), result);
109+
}
110+
}

0 commit comments

Comments
 (0)