Sample
-----------
input : “I saw a cd player and a modem in ccd”
output : de--> 1
cd --> 2
I saw a player and a mom in c
ContiAlpha.java
ContiSortByValue.java
de-->1
cd-->2
i saw a player and a mom in c
-----------
input : “I saw a cd player and a modem in ccd”
output : de--> 1
cd --> 2
I saw a player and a mom in c
ContiAlpha.java
package com.test;
import
java.io.IOException;
import
java.util.ArrayList;
import java.util.Collections;
import
java.util.HashMap;
import java.util.Set;
public class ContiAlpha
{
public static void main(String[]
args) throws IOException
{
try
{
HashMap<String, Integer> map= new
HashMap<String, Integer>();
String s1="I saw a
cd ghplayer and a modem degh in
ccd";
s1=s1.toLowerCase();
for(int
i=0;i<(s1.length()-1);i++)
{
if(s1.charAt(i+1)-s1.charAt(i)==1)
{
String word=s1.charAt(i)+""+s1.charAt(i+1);
if(map.containsKey(word))
{
int
value=map.get(word);
map.put(word,++value);
}
else
{
map.put(word, 1);
}
}
}
ArrayList<ContiSortByValue> list=new
ArrayList<ContiSortByValue>();
Set<String>
keySet=map.keySet();
String s2=s1;
for(String k :
keySet)
{
s2=s2.replace(k, "");
int
value=map.get(k);
list.add(new
ContiSortByValue(k, value));
}
System.out.println(s2);
Collections.sort(list);
for(ContiSortByValue
sort : list)
{
System.out.println(sort);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
ContiSortByValue.java
package com.test;
public class
ContiSortByValue implements Comparable<ContiSortByValue>
{
private String key;
private Integer value;
public
ContiSortByValue()
{
// TODO
Auto-generated constructor stub
}
public
ContiSortByValue(String key, Integer value)
{
super();
this.key = key;
this.value = value;
}
@Override
public int
compareTo(ContiSortByValue obj)
{
return this.value - obj.value;
}
@Override
public String
toString()
{
return key + "-->" + value;
}
}
output:de-->1
cd-->2
i saw a player and a mom in c
No comments:
Post a Comment