<commit_before>package com.akh.algorithms.stringAlgorithms;

import java.io.ObjectOutputStream.PutField;
import java.util.Scanner;
import java.util.Stack;

public class StringCompression {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Enter String to compress : \t");
		Scanner scanner = new Scanner(System.in);
		String input = scanner.nextLine();
		System.out.println("Input - "+input);
		System.out.println("Compressed - "+compress(input));

	}

	/**
	 * Input: OriginalString = "aabcccccaaa"
	 * Output: "a2b1c5a3"
	 * if(compressed string longer than original string then return original string)
	 * 
	 */
	public static String compress(String str){
		int repeatCount = 0;
		int size = 0;
		char last = str.charAt(0);
		for(int i=1; i<str.length(); i++){
			if(str.charAt(i) == last)
				repeatCount++;
			else{
				last = str.charAt(i);
				size = size + 1 + String.valueOf(repeatCount).length();
				repeatCount = 1;
			}
		}
		
		size = size + 1 + String.valueOf(repeatCount).length();
		if(size>= str.length())
			return str;
		
		StringBuffer buf = new StringBuffer(size);
		repeatCount = 1;
		last = str.charAt(0);
		for(int i=1; i<str.length();i++){
			if(str.charAt(i) == last)
				repeatCount++;
			else{
				buf.append(last);
				buf.append(repeatCount);
				last = str.charAt(i);
				repeatCount = 1;
			}
		}
		
		buf.append(last);
		buf.append(repeatCount);
		return buf.toString();
		
	}

}
<commit_msg>Updated with new implementation with space complexity O(1). Uses Sliding Window technique<commit_after>package com.akh.algorithms.stringAlgorithms;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class StringCompression {

	/**
	 * Given an array of characters, compress it in-place.

		The length after compression must always be smaller than or equal to the original array.

		Every element of the array should be a character (not int) of length 1.

		After you are done modifying the input array in-place, return the new length of the array.


		Follow up:
		Could you solve it using only O(1) extra space?


		Example 1:
		Input:
		["a","a","b","b","c","c","c"]

		Output:
		Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]

		Explanation:
		"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
		Example 2:
		Input:
		["a"]

		Output:
		Return 1, and the first 1 characters of the input array should be: ["a"]

		Explanation:
		Nothing is replaced.
		Example 3:
		Input:
		["a","b","b","b","b","b","b","b","b","b","b","b","b"]

		Output:
		Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].

		Explanation:
		Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
		Notice each digit has it's own entry in the array.

		Note:
		All characters have an ASCII value in [35, 126].
		1 <= len(chars) <= 1000.
	 * 
	 */


	public int compress(char[] chars) {
		if(chars == null || chars.length <= 0)
			return 0;
		if(chars.length == 1)
			return 2;
		
		int lo = 0, hi = lo, lastWrite = -1;
		
		while(hi < chars.length) {
			char ch = chars[lo];
			while(hi<chars.length-1 && chars[hi+1] == ch) {
				hi++;
			}
			int count = hi - lo + 1;
			int tmpCnt = count, dCnt = 0;
			while(tmpCnt > 0) {
				dCnt++;
				tmpCnt /= 10;
			}
			
			if(hi < chars.length-1) {
				lastWrite += 1+1+dCnt;
			}
			lo = hi = hi+1;
		}
		return lastWrite+1;
	}
	
	@Test
	public void test_1() {
		StringCompression sCompress = new StringCompression();
		String input = "aaabbc";
		String output = "a3b2c1";
		System.out.println("Compressed String Length: "+sCompress.compress(input.toCharArray()));
		assertEquals(output.length(), sCompress.compress(input.toCharArray()));
	}
	
	@Test
	public void test_2() {
		StringCompression sCompress = new StringCompression();
		String input = "a";
		String output = "a1";
		System.out.println("Compressed String Length: "+sCompress.compress(input.toCharArray()));
		assertEquals(output.length(), sCompress.compress(input.toCharArray()));
	}
	
	@Test
	public void test_3() {
		StringCompression sCompress = new StringCompression();
		String input = "abc";
		String output = "a1b1c1";
		System.out.println("Compressed String Length: "+sCompress.compress(input.toCharArray()));
		assertEquals(output.length(), sCompress.compress(input.toCharArray()));
	}
	
	@Test
	public void test_4() {
		StringCompression sCompress = new StringCompression();
		String input = "aaaaaaaaaaaabbccc";
		String output = "a12b2c3";
		System.out.println("Compressed String Length: "+sCompress.compress(input.toCharArray()));
		assertEquals(output.length(), sCompress.compress(input.toCharArray()));
	}
}

{'fraction_non_alphanumeric': 0.14138140571197083, 'fraction_numerical': 0.016204172574437917, 'mean_word_length': 3.0945273631840795, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5664773', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1585, 'n_words': 448}