1
1
/* Question 1:
2
+
2
3
Change a String format be palindrome, need follow the alphabetical order
3
4
4
- Example:
5
+ Example 1 :
5
6
Input: "xyxy"
6
7
Output: "xyyx"
8
+
9
+ Example 2:
10
+ Input: "aaabbbbbcccc"
11
+ Output: "abbccabccbba"
12
+
7
13
*/
14
+
8
15
//Solution 1: Used bucket to sort and save element
9
16
public class MyClass {
10
17
public static void main (String args []) {
11
- String str = "xyxy " ;
18
+ String str = "aaabbbbbcccc " ;
12
19
System .out .println (reverse (str ));
13
20
}
14
21
@@ -28,20 +35,24 @@ public static String reverse(String s){
28
35
//Use bucket to sort String to be palindrome
29
36
for (int i = 0 ; i < array .length ; i ++){
30
37
if (array [i ] != 0 ){ //need check whether have this character element
31
- if (array [i ] % 2 == 0 ){ //if the number of current character is even
38
+ if (array [i ] % 2 == 0 ){ //if the number of current character is even times
32
39
for (int k = 0 ; k < (array [i ] / 2 ); k ++){
33
40
firstPart .append ((char ) (i + 'a' )); //add half part of current character into firstPart StringBuilder
34
41
}
35
- } else { //if the number of current character is odd
36
- for (int z = 0 ; z < array [i ]; z ++){
42
+ } else { //if the number of current character is odd times
43
+ if (array [i ] > 1 ){ // if the odd times is large than 1
44
+ mid .append ((char ) (i + 'a' )); // add one time into mid part StringBuilder
45
+ for (int z = 0 ; z < array [i ] / 2 ; z ++){
46
+ firstPart .append ((char ) (i + 'a' )); //add half part of (character times - 1) into firstpart StringBuilder
47
+ }
48
+ } else { //if the odd times is one
37
49
mid .append ((char ) (i + 'a' )); //add all the number of current characters into mid Part
38
50
}
39
51
}
40
52
}
41
53
}
42
54
//Output be palindrome format
43
55
return firstPart .toString () + mid .toString () + firstPart .reverse ().toString ();
44
-
45
56
}
46
57
}
47
58
@@ -52,7 +63,7 @@ public static String reverse(String s){
52
63
53
64
public class MyClass {
54
65
public static void main (String args []) {
55
- String str = "abbbaaa " ;
66
+ String str = "aaabbbbbcccc " ;
56
67
System .out .println (reverse (str ));
57
68
}
58
69
@@ -74,15 +85,20 @@ public static String reverse(String s){
74
85
for (int k = 0 ; k < (number / 2 ); k ++){
75
86
firstPart .append ((char ) (i + 'a' )); //add half part of current character into firstPart StringBuilder
76
87
}
77
- } else { //if the number of current character is odd
78
- for (int z = 0 ; z < number ; z ++){
79
- mid .append ((char ) (i + 'a' )); //add all the number of current characters into mid Part
88
+ } else { //if the number of current character is odd times
89
+ if (number > 1 ){ // if the odd times large than one
90
+ mid .append ((char ) (i + 'a' )); //add one time of current characters into the mid Part StringBuilder
91
+ for (int z = 0 ; z < number / 2 ; z ++){
92
+ firstPart .append ((char ) (i + 'a' )); // /add half part of current character into firstPart StringBuilder
93
+ }
94
+ } else {
95
+ mid .append ((char ) (i + 'a' )); //add one time of current characters into mid Part
80
96
}
81
97
}
82
98
}
83
99
}
84
- //Output be palindrome format
85
- return firstPart .toString () + mid .toString () + firstPart .reverse ().toString ();
86
100
101
+ //Output be palindrome format
102
+ return firstPart .toString () + mid .toString () + firstPart .reverse ().toString ();
87
103
}
88
104
}
0 commit comments