1

Given a string, I want to compress the string based on each character's number of consecutive occurrences next to it. For example, let's say we have a string like "abaasass". 'a' occurs one time, 'b' occurs one time, 'a' occurs two times consecutively, 's' occurs one time, 'a' occurs one time, and 's' occurs two times consecutively. The method should then return a string like "aba2sas2".

This is what I have so far:

public static String compressedString(String message) {
        StringBuilder compressedString = new StringBuilder();
        int total = 0;
        for (int i = 0; i < message.length() - 1; i++){
            if (message.charAt(i) == message.charAt(i+1)){
                total += 2;
                compressedString.append(message.charAt(i)).append(total);
            }
            else {
                compressedString.append(message.charAt(i));
            }
            total = 0;
        }
        return compressedString.toString();
    }

It instead returns: "aba2asas2" which is somewhat close, anyone sees the issue?

2
  • I see a host of issues. Try putting in a string with 3 or more consecutive characters. Commented Jul 19, 2021 at 2:33
  • 2
    A quick google for Java run length encoding should bring up a plethora of correct implementations. Commented Jul 19, 2021 at 2:34

3 Answers 3

2
public static String compressedString(String message) {
    StringBuilder compressedString = new StringBuilder();
    int total = 1;
    for (int i = 0; i < message.length() - 1; i++){
        if (message.charAt(i) == message.charAt(i+1)){
            total++;
        }
        else if(total==1){
            compressedString.append(message.charAt(i));
        }
        else 
        {
            compressedString.append(message.charAt(i)).append(total);
            total = 1;
        }
    }
    if(message.charAt(message.length()-2) != message.charAt(message.length()-1)
        compressedString.append(message.charAt(message.length()-1));
    else
        compressedString.append(message.charAt(message.length()-1)).append(total);

    return compressedString.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

2
public static String compressedString(String message)
{
    String result = "" ;
    
    for ( int i = 0, t = message.length() - 1 ; i < t ; )
    {
        String letter = String.valueOf( message.charAt(i) ) ;
        int currentChain = consec( i, message ) ;
        
        result += ( currentChain > 1 ? ( letter + currentChain ) : letter ) ;
        
        i += currentChain ;

    }
    return result ;
}

private static int consec( int startIndex, String text )
{
    int chain = 1 ;
    
    for( int i = startIndex ; i < text.length() - 1 ; ++i )
    {
        if( text.charAt(i) == text.charAt(i+1) )
            chain++ ;
        else
            break ;
    }
    
    return chain ;
}

Comments

0

This is your solution for your question

static void compressedString(String str) {
    int n = str.length();
    for (int i = 0; i < n; i++) {

        // Count occurrences of current character
        int count = 1;
        while (i < n - 1 && str.charAt(i) == str.charAt(i + 1)) {
            count++;
            i++;
        }

        if (count == 1) {
            System.out.print(str.charAt(i));
        } else {
            System.out.print(str.charAt(i));
            System.out.print(count);
        }
    }
}

public static void main(String[] args) {
    String str = "abaasass";
    compressedString(str);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.