|
| 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 | +``` |
0 commit comments