Remove adjacent duplicate characters in a String (java) element i.e.: aaaabbbccdbbaae output: abcdbae

my code does not give the expected result, but it works in dry mode. See where the problem is.

public static StringBuffer singleOccurence(String s)
{
 StringBuffer sb = new StringBuffer(s);
 int length=s.length();


 for(int i=0; i< length ; i++)
 {
  for(int j=i; i<length&&j<length ; j++)
  {
    if(sb.charAt(i)!=sb.charAt(j+1)) 
         i=j+1;
    else
    sb.deleteCharAt(j+1);
   } 
 }

 return sb;
}

also provides StringIndexOutOfBounds

+5
source share
8 answers

Your method does a lot of unnecessary work.

The problem can be solved by repeating a line once and comparing each character with the one that precedes it:

public static StringBuilder singleOccurence(String s)
{
    StringBuilder sb = new StringBuilder();
    if (s.length() > 0) {
        char prev = s.charAt(0);
        sb.append(prev);
        for (int i = 1; i < s.length(); ++i) {
            char cur = s.charAt(i);
            if (cur != prev) {
                sb.append(cur);
                prev = cur;
            }
        }
    }
    return sb;
}

This method has linear time complexity.

+4
source

I would use a regex:

String input = "aaaabbbccdbbaae";
String regex = "(.)(\\1)+"; // matches any character followed by the same one(s)
String output = input.replaceAll(regex, "$1");
System.out.println(output); // abcdbae

Your method will look like this:

public static String singleOccurence(String s) {
    return s.replaceAll("(.)(\\1)+", "$1");
}
+4
source

- :

public static StringBuffer singleOccurence(String s)
{
    StringBuffer sb = new StringBuffer(s);        
    for (int i = sb.length() - 2; i >= 0; i--)
        if (sb.charAt(i) == sb.charAt(i + 1))
             sb.deleteCharAt(i + 1);
    return sb;
}
+2

:

 String input = "aaaaabbbbccccddd";
    Pattern p = Pattern.compile("(.)(?!\\1)");
    Matcher m = p.matcher(input);

    while(m.find()){
        System.out.println(m.group(1));
    }
+1

:

    String reg;
    String input;
    String output;
    reg    = "(.)(\\1)+";
    input  = "aaaabbbccdbbaae";
    output = input.replaceAll(reg,"$1");
    System.out.println("Input :"+input);
    System.out.println("Output:"+output);
0

replaceAll regex

string result = myString.replaceAll(/([a-z])\1+/ig, "$1");

regex, , - [a-z], , \1+

( /) , ( g), ( i). g, replaceAll.

$1, , . [a-z], $1 .

, , , .

0

RemoveAdjacentLetters {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter any word: "); // Inputting the word
    String str = scanner.nextLine();
    for (int i = 1; i < str.length(); i++) { // from 2nd letter
        if (str.charAt(i) == str.charAt(i - 1)) { // comparing adjacent
                                                    // letters

            str = str.substring(0, i - 1) + str.substring(i + 1); // eliminating
                                                                    // 1st
                                                                    // duplicates
            System.out.println(str);
            i = 0;                // i=0 because I need to check for all possible adjacent letters.. 

        }
    }
    if (str.length() == 0) {
        System.out.println("Empty String");
    } 
    scanner.close();
}

}

0
source

import java.util.Arrays; import java.util.Scanner;

public class ReverseSkip {

public static void main(String[] args) {
    String str;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the Word or Sentence");
    str =in.nextLine();
    String revStr ="null";

    char [] chars = str.toCharArray();
    char [] reversedChars = new char[chars.length];

    reversedChars[reversedChars.length - 1] = chars[0];


    int r = reversedChars.length - 2;
    for(int i = 1 ; i < chars.length ; i++ ){
        if(chars[i] != chars[i-1]){
            reversedChars[r] = chars[i];
            r--;
        }
    }

    revStr = new String(Arrays.copyOfRange(reversedChars, r+1, reversedChars.length));

    System.out.println(revStr);
}

}

0
source

All Articles