Untitled

Run Settings
LanguageJava
Language Version
Run Command
//Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return //the original string. You can assume the string has only uppercase and lowercase letters (a - z). import java.util.Scanner; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String input = s.nextLine(); Main m = new Main(); System.out.println(m.compress(input)); } public String compress(String a){ if(a == null && a.length() == 0 ){ return a; } StringBuilder result = new StringBuilder(); char current = a.charAt(0); int counter = 1; int rC = 0; for(int i=1;i<a.length();i++){ if(a.charAt(i) != current){ result.append(current); result.append(counter); current = a.charAt(i); counter = 1; }else{ counter++; } } return result.toString(); } }
Editor Settings
Theme
Key bindings
Full width
Lines