Skip to content

Commit 61b0590

Browse files
authored
Update OA 1 Question 1.java
1 parent 16f5507 commit 61b0590

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

Amazon OA/OA 1 Question 1.java

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
/* Question 1:
2+
23
Change a String format be palindrome, need follow the alphabetical order
34
4-
Example:
5+
Example 1:
56
Input: "xyxy"
67
Output: "xyyx"
8+
9+
Example 2:
10+
Input: "aaabbbbbcccc"
11+
Output: "abbccabccbba"
12+
713
*/
14+
815
//Solution 1: Used bucket to sort and save element
916
public class MyClass {
1017
public static void main(String args[]) {
11-
String str = "xyxy";
18+
String str = "aaabbbbbcccc";
1219
System.out.println(reverse(str));
1320
}
1421

@@ -28,20 +35,24 @@ public static String reverse(String s){
2835
//Use bucket to sort String to be palindrome
2936
for(int i = 0; i < array.length; i++){
3037
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
3239
for(int k = 0; k < (array[i] / 2); k++){
3340
firstPart.append((char) (i + 'a')); //add half part of current character into firstPart StringBuilder
3441
}
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
3749
mid.append((char) (i + 'a')); //add all the number of current characters into mid Part
3850
}
3951
}
4052
}
4153
}
4254
//Output be palindrome format
4355
return firstPart.toString() + mid.toString() + firstPart.reverse().toString();
44-
4556
}
4657
}
4758

@@ -52,7 +63,7 @@ public static String reverse(String s){
5263

5364
public class MyClass {
5465
public static void main(String args[]) {
55-
String str = "abbbaaa";
66+
String str = "aaabbbbbcccc";
5667
System.out.println(reverse(str));
5768
}
5869

@@ -74,15 +85,20 @@ public static String reverse(String s){
7485
for(int k = 0; k < (number / 2); k++){
7586
firstPart.append((char) (i + 'a')); //add half part of current character into firstPart StringBuilder
7687
}
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
8096
}
8197
}
8298
}
8399
}
84-
//Output be palindrome format
85-
return firstPart.toString() + mid.toString() + firstPart.reverse().toString();
86100

101+
//Output be palindrome format
102+
return firstPart.toString() + mid.toString() + firstPart.reverse().toString();
87103
}
88104
}

0 commit comments

Comments
 (0)