raveeshajoshi
BAN USERpublic class CountStrLengthNew {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("ttllmmnn ------------After Compress ---------"+compress("ttllmmnn"));
System.out.println("aaaaabaaaaabaaaaabZaaaaab ------------After Compress ---------"+compress("aaaaabaaaaabaaaaabZaaaaab"));
System.out.println("aasasatb ------------After Compress ---------"+compress("aasasatb"));
System.out.println("ababababababababab ------------After Compress ---------"+compress("ababababababababab"));
System.out.println("aabbaabba ------------After Compress ---------"+compress("aabbaabba"));
System.out.println("abababababababababkc ------------After Compress ---------"+compress("abababababababababkc"));
System.out.println("aaaaabaaaaabaaaaabZaaaaab ------------After Compress ---------"+compress("aaaaabaaaaabaaaaabZaaaaab"));
}
public static String compress(String input) {
int inputLength = input.length();
int compLength = input.length() / 2;
String compressedString = "";
for (int startIndex = 0; startIndex < input.length(); startIndex++) {
int wordcount = 0;
compLength = (input.length() - startIndex) / 2;
if (input.length() - startIndex == 1) {
compressedString = compressedString + 1
+ String.valueOf(input.charAt(inputLength - 1));
break;
}
while (compLength != 0) {
String cmpStr = input.substring(startIndex, startIndex
+ compLength);
String remStr = input.substring(startIndex + compLength,
inputLength);
while (remStr.length() >= cmpStr.length()
&& (remStr.startsWith(cmpStr))) {
if (wordcount == 0) {
startIndex = startIndex + compLength;
}
wordcount++;
startIndex = startIndex + compLength;
remStr = input.substring(startIndex, input.length());
}
if (wordcount != 0) {
int newInterVals = getInternalPatternLen(cmpStr);
wordcount = (wordcount + 1) * newInterVals;
int newPatterLength = cmpStr.length() / newInterVals;
String newString = cmpStr.substring(0, newPatterLength);
if (remStr != null && remStr.length() >= newPatterLength
&& remStr.startsWith(newString)) {
wordcount++;
startIndex = startIndex + newPatterLength;
}
compressedString = compressedString + wordcount + newString;
startIndex--;
break;
} else {
compLength--;
}
}
if (wordcount == 0 && compLength == 0) {
compressedString = compressedString + 1
+ input.substring(startIndex, startIndex + 1);
}
}
return compressedString;
}
public static int getInternalPatternLen(String cmpStr) {
int intervalcounter = 1;
int count = 1;
if (cmpStr.length() == 1)
return intervalcounter;
while (count <= cmpStr.length() / 2) {
intervalcounter = 1;
for (int k = count; k < cmpStr.length(); k += count) {
if (cmpStr.substring(0, count).equals(
cmpStr.substring(k, count + k))) {
intervalcounter++;
} else {
intervalcounter = 1;
break;
}
}
if (intervalcounter != 1)
break;
count++;
}
return intervalcounter;
}
}
- raveeshajoshi May 14, 2015
RepProcess Technicians are responsible for monitoring and improving manufacturing and engineering processes. They are employed by a wide variety of ...
Repchristinetcollazoc, Software Analyst
I am a pediatric nurse.I administer directly procedures and medicines to children according to prescribed I also continually assess ...
You can see my thoughts below
- raveeshajoshi May 14, 20151) keep starting index 0, take half of the string (compare length)and start comparing with remaining
2) decrement compare counter length if there is no mach in previous compare length
3) If there is a match find count of the match
4) if no matches till it reaches count 1 , mark it as 1<first character> , move index by 1
5) Repeat these steps until index moves to input string length
6) There is a extra method call added once number of matching words found to find out if any internal matchings available. ex:abababab ,