Problem Statement |
|||||||||||||
Fox Ciel received a string as a birthday present. However, the
string was too long for her, so she decided to make it shorter by
erasing some characters. The erasing process will look as follows:
For example, if she receives "cieeilll", she will change the string as follows: "cieeilll" -> "ciilll" -> "clll" -> "cl". You are given a String s. Return the string she will get after she erases characters as described above. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | s will contain between 1 and 50 characters, inclusive. | ||||||||||||
- | Each character in s will be a lowercase letter ('a'-'z'). | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
My Solution is Below
public static String simulate(String str){
int start =0;
while(str.length()-1 >start){
char firstChar = str.charAt(start);
// System.out.println(firstChar + " : fisrt");
char nextChar = str.charAt(start+1);
// System.out.println(nextChar +" : next");
if(firstChar == nextChar){
str = str.substring(0, start) + str.substring(start+2, str.length());
// System.out.println("complete:" + str);
if(start !=0){
start--;
}
}else{
start++;
}
}
// System.out.println(str);
return str;
}
## Access Logic ##
my access is below but i don't think this is a good sample
because i just got 144point / 250 point
so other soultions are better than my solution
i attached other solution after my solution
|
first+1 point is i
c and i is differenct so first point will be 2
first point is i
first+1 point is e
c and i is differenct so first point will be 3
firtst point e
first+1 point is e
e and e is the same so cut before first point "ci" and cut after firstPoint +1 to end "ill"
and concat before string and after string
this will be ciill
first point changed first-1
Otheres are Below
public static String simulate(String s){
for(int i=0; i<s.length()-1 i++){
if(s.charAT(i) == s.charAT(i+1)){
s= s.substring(0, i) + s.substring(i+2, s.length);
i=-1;
}
}
}
sombody use StringBuffer.delete
and sombody use Stack
many different way i learend from topcoder --V
No comments:
Post a Comment