-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMirror.java
More file actions
36 lines (36 loc) · 1.03 KB
/
StringMirror.java
File metadata and controls
36 lines (36 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public static String stringMirror(String str) {
// code here
char last=str.charAt(0);
StringBuilder tmp=new StringBuilder(),ans1=new StringBuilder(),ans2=new StringBuilder();
tmp.append(str.charAt(0));
for(int i=1;i<str.length();i++){
if(str.charAt(i)<last){
last=str.charAt(i);
tmp.append(last);
}
else break;
}
ans1.append(tmp);
tmp.reverse();
ans1.append(tmp);
tmp=new StringBuilder();
tmp.append(str.charAt(0));
last=str.charAt(0);
for(int i=1;i<str.length();i++){
if(str.charAt(i)<=last){
last=str.charAt(i);
tmp.append(last);
}
else break;
}
ans2.append(tmp);
tmp.reverse();
ans2.append(tmp);
if(ans1.toString().compareTo(ans2.toString())<=0){
return ans1.toString();
}else{
return ans2.toString();
}
}
}