File tree Expand file tree Collapse file tree 4 files changed +92
-16
lines changed
solution/1300-1399/1328.Break a Palindrome Expand file tree Collapse file tree 4 files changed +92
-16
lines changed Original file line number Diff line number Diff line change @@ -94,17 +94,17 @@ class Solution {
9494 if (n == 1 ) {
9595 return " " ;
9696 }
97- char [] cs = palindrome. toCharArray();
97+ char [] s = palindrome. toCharArray();
9898 int i = 0 ;
99- while (i < n / 2 && cs [i] == ' a' ) {
99+ while (i < n / 2 && s [i] == ' a' ) {
100100 ++ i;
101101 }
102102 if (i == n / 2 ) {
103- cs [n - 1 ] = ' b' ;
103+ s [n - 1 ] = ' b' ;
104104 } else {
105- cs [i] = ' a' ;
105+ s [i] = ' a' ;
106106 }
107- return String . valueOf(cs );
107+ return String . valueOf(s );
108108 }
109109}
110110```
@@ -177,6 +177,33 @@ function breakPalindrome(palindrome: string): string {
177177}
178178```
179179
180+ #### Rust
181+
182+ ``` rust
183+ impl Solution {
184+ pub fn break_palindrome (palindrome : String ) -> String {
185+ let n = palindrome . len ();
186+ if n == 1 {
187+ return "" . to_string ();
188+ }
189+ let mut s : Vec <char > = palindrome . chars (). collect ();
190+ let mut i = 0 ;
191+
192+ while i < n / 2 && s [i ] == 'a' {
193+ i += 1 ;
194+ }
195+
196+ if i == n / 2 {
197+ s [n - 1 ] = 'b' ;
198+ } else {
199+ s [i ] = 'a' ;
200+ }
201+
202+ s . into_iter (). collect ()
203+ }
204+ }
205+ ```
206+
180207<!-- tabs: end -->
181208
182209<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -95,17 +95,17 @@ class Solution {
9595 if (n == 1 ) {
9696 return " " ;
9797 }
98- char [] cs = palindrome. toCharArray();
98+ char [] s = palindrome. toCharArray();
9999 int i = 0 ;
100- while (i < n / 2 && cs [i] == ' a' ) {
100+ while (i < n / 2 && s [i] == ' a' ) {
101101 ++ i;
102102 }
103103 if (i == n / 2 ) {
104- cs [n - 1 ] = ' b' ;
104+ s [n - 1 ] = ' b' ;
105105 } else {
106- cs [i] = ' a' ;
106+ s [i] = ' a' ;
107107 }
108- return String . valueOf(cs );
108+ return String . valueOf(s );
109109 }
110110}
111111```
@@ -178,6 +178,33 @@ function breakPalindrome(palindrome: string): string {
178178}
179179```
180180
181+ #### Rust
182+
183+ ``` rust
184+ impl Solution {
185+ pub fn break_palindrome (palindrome : String ) -> String {
186+ let n = palindrome . len ();
187+ if n == 1 {
188+ return "" . to_string ();
189+ }
190+ let mut s : Vec <char > = palindrome . chars (). collect ();
191+ let mut i = 0 ;
192+
193+ while i < n / 2 && s [i ] == 'a' {
194+ i += 1 ;
195+ }
196+
197+ if i == n / 2 {
198+ s [n - 1 ] = 'b' ;
199+ } else {
200+ s [i ] = 'a' ;
201+ }
202+
203+ s . into_iter (). collect ()
204+ }
205+ }
206+ ```
207+
181208<!-- tabs: end -->
182209
183210<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -4,16 +4,16 @@ public String breakPalindrome(String palindrome) {
44 if (n == 1 ) {
55 return "" ;
66 }
7- char [] cs = palindrome .toCharArray ();
7+ char [] s = palindrome .toCharArray ();
88 int i = 0 ;
9- while (i < n / 2 && cs [i ] == 'a' ) {
9+ while (i < n / 2 && s [i ] == 'a' ) {
1010 ++i ;
1111 }
1212 if (i == n / 2 ) {
13- cs [n - 1 ] = 'b' ;
13+ s [n - 1 ] = 'b' ;
1414 } else {
15- cs [i ] = 'a' ;
15+ s [i ] = 'a' ;
1616 }
17- return String .valueOf (cs );
17+ return String .valueOf (s );
1818 }
19- }
19+ }
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn break_palindrome ( palindrome : String ) -> String {
3+ let n = palindrome. len ( ) ;
4+ if n == 1 {
5+ return "" . to_string ( ) ;
6+ }
7+ let mut s: Vec < char > = palindrome. chars ( ) . collect ( ) ;
8+ let mut i = 0 ;
9+
10+ while i < n / 2 && s[ i] == 'a' {
11+ i += 1 ;
12+ }
13+
14+ if i == n / 2 {
15+ s[ n - 1 ] = 'b' ;
16+ } else {
17+ s[ i] = 'a' ;
18+ }
19+
20+ s. into_iter ( ) . collect ( )
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments