Java language question on hashing: Search is a problem where given a long string
ID: 3761698 • Letter: J
Question
Java language question on hashing:
Search is a problem where given a long string, the text, you are to find a list of k-letter words hidden in the text. For example, the text “thequickbrownfoxjumpsoverthelazydog” contains the words (“quick”, “overt”) within it.
Design and implement an algorithm that would find the number of occurrences of a k-letter word within the text, in O(K) average time, after a preprocessing step on the text. Design and implement the preprocessing step too, and find its Big-Oh time complexity.
*Use hashing*
Explanation / Answer
Answer :
Input :"thequickbrownfoxjumpsoverthelazydog"
Output : Display number of occurences of k-letter in input string given
We store the input string in an character array
We start traversing the string in array z[i].
for i=0 to i<z.length
Store the character at index i that is z[i] in temporary variable ch in this case the letter "k"
for z[i+1] to z.length
if ch is equals to the any element of the rest of the array characters
then move the cursor to next index and reduce the length of the string by 1
set s[k]=s[k+1]
set j=i
Create a new array of integers to store the count of total number of appearances of alphabets in the string
Compare every element of string s with the every element of character array z i.e letter k //in our case the letter "k"
if both are equal then
increase count by 1
Add the final count value to the array t (which is used to store the final count of total number of occurrences of the alphabet "k" in the string )
Print the count of the alphabet
Java Program
public class AlphabetCount{
static int i,j,k,c=0,w;
static char m;
public static void main(String[] args) {
System.out.println("Input string is : ");
System.out.println("thequickbrownfoxjumpsoverthelazydog");
System.out.println("");
System.out.println("");
System.out.println("Output :");
frequencycount("thequickbrownfoxjumpsoverthelazydog");
}
static void frequencycount(String s)
{
//counting occurrence of character with loop
int charCount = 0;
for(int i =0 ; i<s.length(); i++){
if(s.charAt(i) == 'k'){
charCount++;
}
}
System.out.println("count of character 'k-letter' on String:
'thequickbrownfoxjumpsoverthelazydog' using for loop " + charCount);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.