Advertisement
sanya5791

String Compressor (Dima's version)

Aug 15th, 2021
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.33 KB | None | 0 0
  1.     fun compress2(s: String): String {
  2.         val sb = StringBuilder()
  3.         var l = 0
  4.         var r = 0
  5.         while (l < s.length) {
  6.             while (r < s.length && s[l] == s[r]) r++
  7.             sb.append(s[l])
  8.             if(r - l > 1) sb.append(r - l)
  9.  
  10.             l = r
  11.         }
  12.  
  13.         return sb.toString()
  14.     }
  15.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement