Interview Question
Country: United States
Treating it like an intersection problem just complicates it. If a sentence contains any of the words... why test it with another word...?
The idea would be to linearly traverse all sentences.
Test each sentence for the presence of one word. If you find it.. print it.. if you dont find it, test the same sentence again with the 2nd word and so on..
Can this be treated as a set intersection problem?
Create sets of lines which contains string1. Similarly for string2 and string3.
The solution would be to union of all the three sets.
The question then would be how to create sets for these strings? I mean, should it be line number or the entire line itself that should go in the sets?
this problem is solved by using strstr in built c function...
first intialize the variable str1,atr2,str3 with the given input string
and take one buf[maxsize]; int i=0;
file* fp= fopen(argv[1],"r");
while(ch!=null){
ch=fread(fp,1,1);
buff[i]=ch;
if(ch=='\n'){if((strstr(buf,str1))!==null) printf("%s",buf);
if((strstr(buf,str2))!==null) printf("%s",buf);
if((strstr(buf,str1))!==null) printf("%s",buf);
i=0;
}
else
{ i++
}
}
We can use Hash code for the above problem... For the 3 strings we can calculate Hash code and we can store it.... then we can start reading the file, and we will calculate the Hash code for each word and if it matches with any 3 string hash code, we will print the entire line and then the same process is repeated for the next line and so on..
First off.. lets just use a fast string compare algorithm... we can easily use Knuth Morris Pratt.
Start string matching with the first word.
For each sentence first just try out the entire sentence for the first word. If there is a match, print the sentence and go to the next. If there is no match, repeat the search on the sentence with the next word and so on...
That way.. if we get a match.. we print the sentence.. and we dont need to test the sentence again with another word because it is redundant.
grep 'string1\|string2\|string3' filename
- RossM June 17, 2012