Intuit Interview Question
Quality Assurance EngineersCountry: United States
Interview Type: Phone Interview
public static String eliminateStopWords(String str, String[] stopWords) {
Set<String> hashStopWords = new HashSet<String>();
for (String arrStr : stopWords) {
hashStopWords.add(arrStr);
}
String[] splitStr = str.split(" ");
int j = 0;
for (int i = 0; i < splitStr.length; i++) {
if (!hashStopWords.contains(splitStr[i].toLowerCase())) {
splitStr[j] = splitStr[i];
j++;
}
}
System.out.println(StringUtils.join(splitStr, " ", 0, j).toString());
return StringUtils.join(splitStr, " ", 0, j).toString();
}
}
Store stop words in hashset. Split the string using str.split(" ") and iterate over the returned array. If an element in the array is present in the hashset (check using hashset.contains(element)), make it null or "". Display final string by concatenating each element of the array with a whitespace in between.
C++ version:
string remove_unnecessary_words(string &s1, vector<string> &words) {
unordered_set<string> ht;
string s2;
string word_tmp;
for (int i = 0; i < words.size(); i++) {
auto it = ht.find(words[i]);
if (it == ht.end()) {
ht.insert(words[i]);
}
}
for (int i = 0; i < s1.size(); i++) {
if (s1[i] == ' ') {
auto it = ht.find(word_tmp);
if (it == ht.end()) {
s2 += word_tmp;
s2 += " ";
}
word_tmp = "";
}
else {
word_tmp += s1[i];
}
}
return s2;
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
public class UnusedWords {
public static void main(String[] args) {
String s= "To be or not to be - that is the question: Whether it is nobler in the mind to suffer, the slings and arrows of outrageous fortune. Or to take up arms against a sea of troubles, and by opposing end them";
StringTokenizer st = new StringTokenizer(s," ");
List<String> ls = new ArrayList<>();
HashSet<String > hst = new HashSet<String >();
hst.add("a" );
hst.add("be");
hst.add("that" );
hst.add("the" );
hst.add("this" );
hst.add( "or" );
hst.add( "to" );
while (st.hasMoreTokens()){
String Currentword = st.nextToken();
if(!hst.contains(Currentword.toLowerCase())){
ls.add(Currentword.toLowerCase());
}
}
for(String srt:ls){
System.out.println(srt.toString());
}
}
}
public class Example1 {
public static void main(String[] args)
{
String str="To be or not to be - that is the question: Whether it is nobler in the mind to suffer, the slings and arrows of outrageous fortune. Or to take up arms against a sea of troubles, and by opposing end them";
List<String> lst = new ArrayList<String>();
String[] strArr = str.split(" ");
for(String str1 : strArr)
{
lst.add(str1);
}
str = "a,be,to,the,that,this,or,To";
strArr = str.split(",");
List<String> remlst = new ArrayList<String>();
for(String str1 : strArr)
{
remlst.add(str1);
}
lst.removeAll(remlst);
StringBuilder finalSrt = new StringBuilder();
for(String str2 : lst)
{
finalSrt.append(str2).append(" ");
}
System.out.print(finalSrt);
}
}
Here's the java version using String array
public class InterviewPpn {
public static void main(String[] args){
String input = "To be or not to be - that is the question: Whether it is nobler in the mind to suffer, the slings and arrows of outrageous fortune. Or to take up arms against a sea of troubles, and by opposing end them";
String[] stopWordList = {"a","or","to","be","that","the","this"};
String[] token = input.split(" ");
StringBuffer finalString = new StringBuffer();
Boolean flag = false;
for(int j=0;j<token.length;j++){
String newToken = token[j].toLowerCase();
for(int k =0;k<stopWordList.length;k++){
if(newToken.equals(stopWordList[k])){
flag = true;
}
}
if(flag == false){
finalString.append(token[j]+" ");
}
flag = false;
}
String output = finalString.toString();
System.out.println("Final String: "+ output);
}
}
we Can use removeall method provided by java.
we have to build two arraylist one for the Input words and second for the words to be removed and then simply use
arrList1.removeAll(arrList2);
now arrList1 will have the required output just print the result.
It will give the same time complexity as mentioned in above examples.
import java.util.HashSet;
import java.util.Set;
public class stopWord {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
String str = "to be or not to be - that is the question";
String [] splitArray = str.split(" ");
String stt = new String();
set.add("a");set.add("be");set.add("to");set.add("the");set.add("that");set.add("this");set.add("or");
for(int i=0; i< splitArray.length;i++){
if(!set.contains(splitArray[i])){
stt = stt + splitArray[i]+ " ";
}
}
System.out.println(stt);
}
}
#!/usr/bin/env python3
"""
eliminate stop words
"""
def eliminate_stop_words(txt, stop_words):
assert type(txt) is str, "txt must be str"
assert type(stop_words) is set, "stop_words must be a set"
return " ".join([word for word in txt.split() if not word in stop_words])
print(eliminate_stop_words("To be or not to be", set(["or"])))
The implementation in C#, but I think there are similar to Java data structures
- nemestnyi November 14, 2013