File tree Expand file tree Collapse file tree 1 file changed +5
-16
lines changed Expand file tree Collapse file tree 1 file changed +5
-16
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String breakPalindrome (String palindrome ) {
3
- // If empty or of length 1 => return empty
4
- if (palindrome .length () <= 1 ) {
5
- return "" ;
6
- }
7
3
char [] chars = palindrome .toCharArray ();
8
- for (int i = 0 ; i < chars .length ; i ++) {
9
- if (chars [i ] != 'a' ) {
10
- // Skip if the string is of odd length and it is the middle index
11
- if (palindrome .length () % 2 != 0 && i == palindrome .length () / 2 ) {
12
- continue ;
13
- }
4
+ for (int i = 0 ; i < palindrome .length () / 2 ; i ++) {
5
+ if (chars [i ] > 'a' ) {
14
6
chars [i ] = 'a' ;
15
- break ;
16
- }
17
- // If all chars all 'a' update last char to 'b'
18
- if (i == chars .length - 1 ) {
19
- chars [i ] = 'b' ;
7
+ return String .valueOf (chars );
20
8
}
21
9
}
22
- return String .valueOf (chars );
10
+ chars [chars .length - 1 ] = 'b' ;
11
+ return chars .length <= 1 ? "" : String .valueOf (chars );
23
12
}
24
13
}
You can’t perform that action at this time.
0 commit comments