package com.sbs.java8.praticse;
public class StringCompression {
public StringCompression() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
System.out.println(compressString("aaaabbbbbccccAAAAccccccccdefg"));
}
public static String compressString(String str) {
//String str = "aaaabbbbbcccc";
char[] charArray = str.toCharArray();
String compressedString = "";
int i = 0;
while (i < charArray.length) { int counter = 1; int j = i + 1; while (j < charArray.length && charArray[i] == charArray[j]) { counter++; j++; i++; } compressedString = compressedString + charArray[i] + counter; i++; } //System.out.println(compressedString); String output =(compressedString.length() > charArray.length)? str: compressedString;
return output;
}
}
public class StringCompression {
public StringCompression() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
System.out.println(compressString("aaaabbbbbccccAAAAccccccccdefg"));
}
public static String compressString(String str) {
//String str = "aaaabbbbbcccc";
char[] charArray = str.toCharArray();
String compressedString = "";
int i = 0;
while (i < charArray.length) { int counter = 1; int j = i + 1; while (j < charArray.length && charArray[i] == charArray[j]) { counter++; j++; i++; } compressedString = compressedString + charArray[i] + counter; i++; } //System.out.println(compressedString); String output =(compressedString.length() > charArray.length)? str: compressedString;
return output;
}
}
Comments
Post a Comment