|
| 1 | +package string; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 12/04/2018. |
| 5 | + * Given an array of characters, compress it in-place. |
| 6 | +
|
| 7 | + The length after compression must always be smaller than or equal to the original array. |
| 8 | +
|
| 9 | + Every element of the array should be a character (not int) of length 1. |
| 10 | +
|
| 11 | + After you are done modifying the input array in-place, return the new length of the array. |
| 12 | +
|
| 13 | +
|
| 14 | + Follow up: |
| 15 | + Could you solve it using only O(1) extra space? |
| 16 | +
|
| 17 | +
|
| 18 | + Example 1: |
| 19 | + Input: |
| 20 | + ["a","a","b","b","c","c","c"] |
| 21 | +
|
| 22 | + Output: |
| 23 | + Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"] |
| 24 | +
|
| 25 | + Explanation: |
| 26 | + "aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3". |
| 27 | + Example 2: |
| 28 | + Input: |
| 29 | + ["a"] |
| 30 | +
|
| 31 | + Output: |
| 32 | + Return 1, and the first 1 characters of the input array should be: ["a"] |
| 33 | +
|
| 34 | + Explanation: |
| 35 | + Nothing is replaced. |
| 36 | + Example 3: |
| 37 | + Input: |
| 38 | + ["a","b","b","b","b","b","b","b","b","b","b","b","b"] |
| 39 | +
|
| 40 | + Output: |
| 41 | + Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"]. |
| 42 | +
|
| 43 | + Explanation: |
| 44 | + Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12". |
| 45 | + Notice each digit has it's own entry in the array. |
| 46 | + Note: |
| 47 | + All characters have an ASCII value in [35, 126]. |
| 48 | + 1 <= len(chars) <= 1000. |
| 49 | +
|
| 50 | + Solution O(N) time complexity and O(1) space complexity. Maintain read and write pointers. Read from read pointer and |
| 51 | + increment count when a repetition is found, when there is no repetition write the count value using write pointer. |
| 52 | + */ |
| 53 | +public class StringCompression { |
| 54 | + |
| 55 | + /** |
| 56 | + * Main method |
| 57 | + * @param args |
| 58 | + * @throws Exception |
| 59 | + */ |
| 60 | + public static void main(String[] args) throws Exception{ |
| 61 | + char[] A = {'a','a','b','b','c','c','c'}; |
| 62 | + System.out.println(new StringCompression().compress(A)); |
| 63 | + } |
| 64 | + |
| 65 | + public int compress(char[] chars) { |
| 66 | + int count = 0; |
| 67 | + int i = 0; |
| 68 | + int p = 0; |
| 69 | + for(int j = 0; j < chars.length; j ++){ |
| 70 | + if(chars[i] == chars[j]){ |
| 71 | + count ++; |
| 72 | + } else{ |
| 73 | + chars[p] = chars[i]; |
| 74 | + p++; |
| 75 | + if(count > 1){ |
| 76 | + String countStr = String.valueOf(count); |
| 77 | + for (int l = 0; l < countStr.length(); l++){ |
| 78 | + chars[p++] = countStr.charAt(l); |
| 79 | + } |
| 80 | + } |
| 81 | + i = j; |
| 82 | + count = 1; |
| 83 | + } |
| 84 | + } |
| 85 | + chars[p] = chars[i]; |
| 86 | + p++; |
| 87 | + if(count > 1){ |
| 88 | + String countStr = String.valueOf(count); |
| 89 | + for (int l = 0; l < countStr.length(); l++){ |
| 90 | + chars[p++] = countStr.charAt(l); |
| 91 | + } |
| 92 | + } |
| 93 | + return p; |
| 94 | + } |
| 95 | +} |
0 commit comments