fork download
  1. public class Solution {
  2. public static String getCompressedString(String str) {
  3.  
  4. int len = str.length();
  5.  
  6. StringBuilder ans = new StringBuilder();
  7.  
  8.  
  9. int cnt = 1;
  10.  
  11. for (int i = 1; i < len; i++) {
  12.  
  13.  
  14. if (str.charAt(i) == str.charAt(i-1))
  15. cnt++;
  16.  
  17.  
  18. else {
  19.  
  20. ans.append(str.charAt(i-1));
  21.  
  22. if (cnt > 1) {
  23. ans.append(cnt);
  24. }
  25.  
  26. cnt = 1;
  27. }
  28.  
  29. if (i == len - 1) {
  30.  
  31. ans.append(str.charAt(i));
  32.  
  33. if (cnt > 1) {
  34. ans.append(cnt);
  35. }
  36. }
  37. }
  38.  
  39.  
  40. return ans.toString();
  41. }
  42.  
  43. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
1 error
stdout
Standard output is empty