Guest User

Untitled

a guest
Feb 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. public class StringCompression {
  2. public int compress(char[] chars) {
  3. int indexAns = 0, index = 0;
  4. while(index < chars.length){
  5. char currentChar = chars[index];
  6. int count = 0;
  7. while(index < chars.length && chars[index] == currentChar){
  8. index++;
  9. count++;
  10. }
  11. chars[indexAns++] = currentChar;
  12. if(count != 1)
  13. for(char c : Integer.toString(count).toCharArray())
  14. chars[indexAns++] = c;
  15. }
  16. return indexAns;
  17. }
  18. }
Add Comment
Please, Sign In to add comment