Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StringCompression {
- public int compress(char[] chars) {
- int indexAns = 0, index = 0;
- while(index < chars.length){
- char currentChar = chars[index];
- int count = 0;
- while(index < chars.length && chars[index] == currentChar){
- index++;
- count++;
- }
- chars[indexAns++] = currentChar;
- if(count != 1)
- for(char c : Integer.toString(count).toCharArray())
- chars[indexAns++] = c;
- }
- return indexAns;
- }
- }
Add Comment
Please, Sign In to add comment