-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCompression.java
More file actions
41 lines (37 loc) · 820 Bytes
/
Copy pathStringCompression.java
File metadata and controls
41 lines (37 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class StringCompression {
public static void main(String[] args)
{
String input="aabbbbccccccdeeeeeaa";
String output=compressString(input);
System.out.println("Compressed String is "+output);
}
private static String compressString(String input) {
// TODO Auto-generated method stub
int count=0;
String output="";
for (int i=0;i<input.length();i++)
{
count=countNoOfOccurences(input.charAt(i),input);
if(output.indexOf(input.charAt(i))==-1)
output=output+input.charAt(i)+count;
}
if(output.length()>=input.length())
return input;
else
{
// System.out.println(output);
return output;
}
}
private static int countNoOfOccurences(char chr,String input)
{
int count=0;
for(int i=0;i<input.length();i++)
{
if (input.charAt(i)==chr)
count++;
}
//System.out.println(count);
return count;
}
}