Not giving the required output

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(compression(str));
}
public static String compression(String str) {
StringBuilder sb = new StringBuilder();
char last = str.charAt(0);
int count = 0;
for (int i=0; i< str.length(); i++) {
if (str.charAt(i) == last) {
count++;
} else {
sb.append(last + β€œβ€ + count);
count = 1; // here count should be 1 but not 0
}
last = str.charAt(i);
}
sb.append(last + β€œβ€ + count); //reflush the last part

	String newStr = sb.toString();
	if (newStr.length() < str.length()) {
		return newStr;
	} else {
		return str;
	}

}
}

@Shivam_balwani,

   if (newStr.length() < str.length()) {
   	return newStr;
   } else {
   	return str;
   }

Don’t use the above condition instead just return newStr.

Also, you need not append the count of a character if count==1. Only append the character if count==1.