CodingGenius
BAN USERimport java.util.*;
class StreamWordsApp
{
public static void main(String[] args)
{
StreamWords sw= new StreamWords();
sw.countAll();
sw.display();
}
}
class StreamWords
{
String input = "acacabcatghhellomvnsdb";
Hashtable<String,Integer> ht= new Hashtable<String,Integer>();
String[] arr={"aca","cat","hello","world"};
public StreamWords()
{
for(int i=0;i<arr.length;i++)
{
ht.put(arr[i],0);
}
}
public int countWords(int index)
{
int count=0;
String currentWord= arr[index];
int j=0;
for(int i=0;i<input.length();i++)
{
if(input.charAt(i)==currentWord.charAt(0))
{
if(input.charAt(i+currentWord.length()-1)==currentWord.charAt(0+currentWord.length()-1))
{
int k=i;
while(true)
{
if(j==currentWord.length()-1)
{
j=0;
count++;
}
else if(input.charAt(k)==currentWord.charAt(j))
{
k++;
j++;
}
else
break;
}
}
}
}
return count;
}
public void countAll()
{
for(int i=0;i<arr.length;i++)
{
int count= countWords(i);
ht.put(arr[i],ht.get(arr[i])+count);
}
}
public void display()
{
System.out.println(ht);
}
}
- CodingGenius April 20, 2016