Java compressing Strings

前端 未结 20 1022
误落风尘
误落风尘 2020-11-29 09:24

I need to create a method that receives a String and also returns a String.

Ex input: AAABBBBCC

Ex output: 3A4B2C

Well, this is quite embarrassing a

20条回答
  •  离开以前
    2020-11-29 10:18

    The easiest approach:- Time Complexity - O(n)

    public static void main(String[] args) {
        String str = "AAABBBBCC";       //input String
        int length = str.length();      //length of a String
    
        //Created an object of a StringBuilder class        
        StringBuilder sb = new StringBuilder(); 
    
        int count=1;   //counter for counting number of occurances
    
        for(int i=0; i

提交回复
热议问题