class Solution {
static String[][] wordCountEngine(String document) {
Hashtable ht=new Hashtable();
String words[]=document.split(" ");
int largestcount=0;
String word;
for(int i=0;i<words.length;i++)
{ word=words[i].toLowerCase();
List<character> li=new ArrayList<character>();
for (char ch:word.tocharArray())
if(ch>='a' && ch<='z')
li.add(ch);
String s="";
for(char ch:li)
s=s+ch;
if(s.length()<1)
continue;
int count=0;
while(document.contains(s))
{
document=document.substring(document.indexOf(s)+s.length(),document.length()-1);
count++;
}
else
count=1;
if(count>largestcount)
largestcount=count;
}
What I have tried:
After counting the words I'm not able to convert it into a list consisting of word and its occurrences and displaying them according to their descending order of occurrences.
Example
input:
document = "Practice makes perfect. you'll only
get Perfect by practice. just practice!"
output:
[ ["practice", "3"], ["perfect", "2"],
["makes", "1"], ["youll", "1"], ["only", "1"],
["get", "1"], ["by", "1"], ["just", "1"] ]