@@ -58,22 +58,152 @@ b<sub>prefix</sub> = "jiz", b<sub>suffix</sub> = "alu"
58
58
59
59
<!-- 这里可写通用的实现逻辑 -->
60
60
61
+ ** 方法一:双指针**
62
+
63
+ - 首先,一个指针从 ` a ` 的头开始,另一个指针从 ` b ` 的尾开始,如果字符相等,两个指针就往中间移动,遇到不同的字符或双指针交叉后停止
64
+ - 双指针交叉说明 ` prefix ` 和 ` suffix ` 已经可以得到回文串
65
+ - 否则还需要判断 ` a[i:j+1] ` 或 ` b[i:j+1] ` 是否是回文(通过 ` prefix + palindrome + suffix ` 得到回文串)
66
+ - 如果还得不到回文串,尝试交换 ` a ` , ` b ` 重复同样的判断
67
+
61
68
<!-- tabs:start -->
62
69
63
70
### ** Python3**
64
71
65
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
73
67
74
``` python
68
-
75
+ class Solution :
76
+ def checkPalindromeFormation (self , a : str , b : str ) -> bool :
77
+ def check1 (a : str , b : str ) -> bool :
78
+ i, j = 0 , len (b) - 1
79
+ while i < j and a[i] == b[j]:
80
+ i += 1
81
+ j -= 1
82
+ return i >= j or check2(a, i, j) or check2(b, i, j)
83
+
84
+ def check2 (a : str , i : int , j : int ) -> bool :
85
+ while i < j and a[i] == a[j]:
86
+ i += 1
87
+ j -= 1
88
+ return i >= j
89
+
90
+ return check1(a, b) or check1(b, a)
69
91
```
70
92
71
93
### ** Java**
72
94
73
95
<!-- 这里可写当前语言的特殊实现逻辑 -->
74
96
75
97
``` java
98
+ class Solution {
99
+ public boolean checkPalindromeFormation (String a , String b ) {
100
+ return check1(a, b) || check1(b, a);
101
+ }
102
+
103
+ private boolean check1 (String a , String b ) {
104
+ int i = 0 ;
105
+ int j = b. length() - 1 ;
106
+ while (i < j && a. charAt(i) == b. charAt(j)) {
107
+ i++ ;
108
+ j-- ;
109
+ }
110
+ return i >= j || check2(a, i, j) || check2(b, i, j);
111
+ }
112
+
113
+ private boolean check2 (String a , int i , int j ) {
114
+ while (i < j && a. charAt(i) == a. charAt(j)) {
115
+ i++ ;
116
+ j-- ;
117
+ }
118
+ return i >= j;
119
+ }
120
+ }
121
+ ```
122
+
123
+ ### ** C++**
124
+
125
+ ``` cpp
126
+ class Solution {
127
+ public:
128
+ bool checkPalindromeFormation(string a, string b) {
129
+ return check1(a, b) || check1(b, a);
130
+ }
131
+
132
+ private:
133
+ bool check1(string &a, string &b) {
134
+ int i = 0, j = b.size() - 1;
135
+ while (i < j && a[ i] == b[ j] ) {
136
+ ++i;
137
+ --j;
138
+ }
139
+ return i >= j || check2(a, i, j) || check2(b, i, j);
140
+ }
141
+
142
+ bool check2(string &a, int i, int j) {
143
+ while (i <= j && a[i] == a[j]) {
144
+ ++i;
145
+ --j;
146
+ }
147
+ return i >= j;
148
+ }
149
+ };
150
+ ```
151
+
152
+ ### ** Go**
153
+
154
+ ``` go
155
+ func checkPalindromeFormation (a string , b string ) bool {
156
+ return check1 (a, b) || check1 (b, a)
157
+ }
158
+
159
+ func check1 (a , b string ) bool {
160
+ i , j := 0 , len (b)-1
161
+ for i < j && a[i] == b[j] {
162
+ i++
163
+ j--
164
+ }
165
+ return i >= j || check2 (a, i, j) || check2 (b, i, j)
166
+ }
167
+
168
+ func check2 (a string , i , j int ) bool {
169
+ for i < j && a[i] == a[j] {
170
+ i++
171
+ j--
172
+ }
173
+ return i >= j
174
+ }
175
+ ```
76
176
177
+ ### ** Rust**
178
+
179
+ ``` rust
180
+ impl Solution {
181
+ pub fn check_palindrome_formation (a : String , b : String ) -> bool {
182
+ fn check1 (a : & [u8 ], b : & [u8 ]) -> bool {
183
+ let (mut i , mut j ) = (0 , b . len () - 1 );
184
+ while i < j && a [i ] == b [j ] {
185
+ i += 1 ;
186
+ j -= 1 ;
187
+ }
188
+ if i >= j {
189
+ return true ;
190
+ }
191
+ check2 (a , i , j ) || check2 (b , i , j )
192
+ }
193
+
194
+ fn check2 (a : & [u8 ], mut i : usize , mut j : usize ) -> bool {
195
+ while i < j && a [i ] == a [j ] {
196
+ i += 1 ;
197
+ j -= 1 ;
198
+ }
199
+ i >= j
200
+ }
201
+
202
+ let a = a . as_bytes ();
203
+ let b = b . as_bytes ();
204
+ check1 (a , b ) || check1 (b , a )
205
+ }
206
+ }
77
207
```
78
208
79
209
### ** ...**
0 commit comments