Redian新闻
>
[面试题求教]remove common phrases from each sentence
avatar
[面试题求教]remove common phrases from each sentence# JobHunting - 待字闺中
h*2
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".
avatar
n*r
2
空格做token切词出来,然后hash,找公共的,然后再分别扫两个string,删除公共的
word就可以了吧
avatar
n*r
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();
}
avatar
h*2
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("[ ]+");

avatar
G*A
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";

avatar
h*2
6
yes

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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。