由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - [面试题求教]remove common phrases from each sentence
相关主题
这道Amazon面试题怎么做微软有组在招new grad software engineer吗?
Leetcode一题(非OJ)一道面试题(integer to binary string)
请教amazon面试题问一道A家的面试题
面试题讨论请教最近onsite的一道面试题:大数相加
问个java hashcode的题updae: 明天GOOG电面, 求祝福 interview 问题
leetcode 上 wordladderII 求教Facebook interview 面经
贡献G家电面面经G家店面
Word ladder II 感觉算法已经是最优了,但是过不了大测试,能不能帮忙看看?问三道题
相关话题的讨论汇总
话题: string话题: s1话题: s2话题: sentence话题: phrases
进入JobHunting版参与讨论
1 (共1页)
h******2
发帖数: 13
1
请求:
Interview Question – Given a set of Sentences containing lower case letters
only, remove common phrases from each sentence. Here a phrase is defined by
3 or more consecutive words.
E.g:
S1 = "I my bye good";
S2 = "my bye good boy";
common phrase is: "my bye good"
so the two sentence become
S1 = "I";
s2 ="boy";
How about the following input ? S1 = "q a b c d a b c b c d e" S2 = "a b c d
e"
What's the output? Is it ok to the following output? S1 = "q" S2 = ""
Yup, your output is correct. This is because there are three common phrases,
ie: "a b c d", "a b c", and "b c d e".
n****r
发帖数: 120
2
空格做token切词出来,然后hash,找公共的,然后再分别扫两个string,删除公共的
word就可以了吧
n****r
发帖数: 120
3
public static void removeCommon(String[] s){
Set set = new HashSet();
Set commonSet = new HashSet();
String[] list1 = s[0].split("[ ]+");

for (String w : list1){
set.add(w);
}

String[] list2 = s[1].split("[ ]+");
StringBuilder sb = new StringBuilder();
for (String w : list2){
if (!set.contains(w))
sb.append(w + " ");
else
commonSet.add(w);
}
if(sb.length() > 0)
sb.deleteCharAt(sb.length()-1);
s[1] = sb.toString();
sb = new StringBuilder();
for (String w : list1){
if (!commonSet.contains(w))
sb.append(w + " ");
}
if(sb.length() > 0)
sb.deleteCharAt(sb.length()-1);
s[0] = sb.toString();
}
h******2
发帖数: 13
4
你这个方法只是去除了相同的word,但是我们要删除的是 3 or more consecutive
words. 我想到的是对每个sentence都保存到一个map中,然后再
从第一个sentence开始先找头三个word是否在剩下的所有的sentence中,不符合则接着
选第一个sentence的接下来的三个。 总体感觉看上去可以实现,但是还是比较繁琐的


【在 n****r 的大作中提到】
: public static void removeCommon(String[] s){
: Set set = new HashSet();
: Set commonSet = new HashSet();
: String[] list1 = s[0].split("[ ]+");
:
: for (String w : list1){
: set.add(w);
: }
:
: String[] list2 = s[1].split("[ ]+");

G****A
发帖数: 4160
5
"a b c" are considered as three words?

letters
by

【在 h******2 的大作中提到】
: 请求:
: Interview Question – Given a set of Sentences containing lower case letters
: only, remove common phrases from each sentence. Here a phrase is defined by
: 3 or more consecutive words.
: E.g:
: S1 = "I my bye good";
: S2 = "my bye good boy";
: common phrase is: "my bye good"
: so the two sentence become
: S1 = "I";

h******2
发帖数: 13
6
yes

【在 G****A 的大作中提到】
: "a b c" are considered as three words?
:
: letters
: by

1 (共1页)
进入JobHunting版参与讨论
相关主题
问三道题问个java hashcode的题
4sum的那道题leetcode 上 wordladderII 求教
请教leetcode的gray code贡献G家电面面经
贡献1个A家3面的面经,被老印坑了Word ladder II 感觉算法已经是最优了,但是过不了大测试,能不能帮忙看看?
这道Amazon面试题怎么做微软有组在招new grad software engineer吗?
Leetcode一题(非OJ)一道面试题(integer to binary string)
请教amazon面试题问一道A家的面试题
面试题讨论请教最近onsite的一道面试题:大数相加
相关话题的讨论汇总
话题: string话题: s1话题: s2话题: sentence话题: phrases