-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringCompression.java
57 lines (45 loc) · 1.18 KB
/
StringCompression.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Rosenkrantz
*/
import java.util.*;
public class StringCompression {
public static void main(String args[])
{
System.out.println("Enter String");
Scanner s=new Scanner(System.in);
String ss=s.nextLine();
Hashtable alpha=new Hashtable();
for(int i=0;i<ss.length();i++)
{
if(!alpha.containsKey(ss.charAt(i)))
{
alpha.put(ss.charAt(i),1);
}
else
{ int balance=Integer.parseInt(alpha.get(ss.charAt(i)).toString());
alpha.put(ss.charAt(i),balance+1);
}
}
String fin="";
Set set=alpha.entrySet();
Iterator it=set.iterator();
while(it.hasNext())
{ //System.out.println("kkk "+it.next());
fin+=it.next();
}
fin=fin.replace("=", "");
if(fin.length()<ss.length())
{
System.out.println("Compressed String "+fin);
}
else
{
System.out.println("Original String "+ss);
}
}
}