i have the following problem
Given a string, return a \"cleaned\" string where adjacent chars that are the same have been reduced to a single char. So \"yyzzza\"
public static String stringClean(String str) {
if (str == null || "".equals(str)) {
return str;
}
char lastChar = str.charAt(0);
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append(lastChar);
for (int index = 1; index < str.length(); index++) {
char next = str.charAt(index);
if (lastChar != next) {
resultBuilder.append(next);
lastChar = next;
}
}
return resultBuilder.toString();
}
How about trying this one:
public String stringClean(String string){
char sc[] = string.toCharArray();
for(int i =0;i<sc.length;i++){
if(i!=sc.length-1){
if(sc[i]!=(sc[i+1])){
output+=sc[i];
}
}else {
output+=sc[i];
}
}
return output;
//System.out.println(output);
}
How about:
public String stringClean(String str) {
if (str.length() < 2)return str;
String nextStr = str.substring(1);
if (str.charAt(0) == str.charAt(1)) {
return stringClean(nextStr);
}
else return str.substring(0,1) + stringClean(nextStr);
}
First of all, your code is overly complicated. There is absolutely no need to
str = str.substring(1);
startIndex--;
inside the loop - you are effectively keeping startIndex
at 0 and chopping off characters from the beginning of the string. Instead, you should just iterate through the characters of string (and print str.substring(startIndex)
if you want to see what's left to process).
Also, here
if(sb.toString().indexOf(startChar) != -1){
sb.append(adjacentChar);
System.out.println("Appended String in if part-->"
+ sb.toString());
}
you aim to prevent adding the same character again if it is repeated more than twice in a row - but the code actually prevents adding a character to the builder ever if it is already in there, i.e. an input like "aba" will yield the incorrect output "ab".
And actually, there is the source of your error too. The condition is wrong:
if(sb.toString().indexOf(startChar) != -1){
yields true
when startChar
is found in the string contained by sb
! If you change !=
to ==
, you will get your 'd' in the output (however, you will get an extra 'b' too).
Your approach of always comparing the actual character to the next one fails when the same character is repeated more than twice in a row. The better approach is to just remember the last character appended to the buffer and skip until you find a character different from it:
public static String stringClean(String str){
final StringBuilder sb = new StringBuilder();
char lastAppendedChar = '\u0000';
for(int index = 0; index < str.length(); index += 1){
char actualChar = str.charAt(index);
if (actualChar != lastAppendedChar){
sb.append(actualChar);
lastAppendedChar = actualChar;
}
}// end of for loop
return sb.toString();
}
The problem in you code is that you append the char not when new is found but when adjetance is different then curent, so always last character would not be appended.
public static String stringClean(String str){
int startIndex = str.indexOf(str);
char startChar = '\u0000';
char adjacentChar = '\u0000';
boolean flag = false; // added
System.out.println("startIndex-->" + startIndex);
final StringBuilder sb = new StringBuilder();
for(startIndex = 0; startIndex < str.length(); startIndex++){
startChar = str.charAt(startIndex);
System.out.println("startIndex ::" + startIndex);
System.out.println("startChar ::" + startChar);
final int adjacentPosition = startIndex + 1;
System.out.println("adjacentPosition ::" + adjacentPosition);
if(adjacentPosition != str.length()){
adjacentChar = str.charAt(adjacentPosition);
System.out.println("adjacentChar ::" + adjacentChar);
} else {
flag = true;
}
if(startChar == adjacentChar){
System.out.println("startChar ::" + startChar);
System.out.println("adjacentChar::" + adjacentChar);
System.out.println("Before Substring string --->" + str);
str = str.substring(1);
startIndex--;
System.out.println("After Substring string --->" + str);
System.out.println("IndexOf check ---->"
+ sb.toString().indexOf(startChar));
if(sb.toString().indexOf(startChar) != -1){
sb.append(adjacentChar);
System.out.println("Appended String in if part-->"
+ sb.toString());
} else if(flag) { /* added */
sb.append(adjacentChar);
}
} else{
str = str.substring(1);
startIndex--;
sb.append(startChar);
System.out.println("Appended String --->" + sb.toString());
}
}// end of for loop
return sb.toString();
}