猿问

Java | 如何从字符串中删除常见单词,然后连接不常见单词?

在一次采访中有人问我,你有两个段落

P1 = I am Lalit
P2 = Lalit Kumar

现在找到普通单词,而不是字符,然后仅打印不常见的单词。

Ex: I am Kumar

解决这个问题的最佳方法是什么?


慕侠2389804
浏览 138回答 3
3回答

芜湖不芜

这可能是一个矫kill过正,但是我将两个字符串分割,将它们收集为一个LinkedHashMap(以保留原始顺序),并计算每个字符串出现多少次,然后过滤掉非唯一条目:String p1 = "I am Lalit";String p2 = "Lalit Kumar";String result =    Stream.concat(Arrays.stream(p1.split("\\s")), Arrays.stream(p2.split("\\s")))          .collect(Collectors.groupingBy(Function.identity(),                                          LinkedHashMap::new,                                         Collectors.counting()))          .entrySet()          .stream()          .filter(e -> e.getValue() == 1)          .map(Map.Entry::getKey)          .collect(Collectors.joining(" "));

呼唤远方

试试下面的代码:public static void main(String[] args) {&nbsp; String str1 = "I am Lalit";&nbsp; String str2 = "Lalit Kumar";&nbsp; List<String> set1 = new ArrayList<>(Arrays.asList(str1.split(" ")));&nbsp; for (String s : Arrays.asList(str2.split(" "))) {&nbsp; &nbsp; if (set1.contains(s)) set1.remove(s);&nbsp; &nbsp; else set1.add(s);&nbsp; }&nbsp; System.out.println(String.join(" ", set1));}

手掌心

你可以做这样的事情...public static void printUncommon(String s1, String s2) {&nbsp; &nbsp; &nbsp; &nbsp; String[] a = s1.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; String[] b = s2.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(a);&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(b);&nbsp; &nbsp; &nbsp; &nbsp; int n1 = a.length;&nbsp; &nbsp; &nbsp; &nbsp; int n2 = b.length;&nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; int j = 0;&nbsp; &nbsp; &nbsp; &nbsp; while(i < n1 && j < n2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int compare = a[i].compareToIgnoreCase(b[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(compare == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(compare < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(compare > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(b[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(i == n1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(j < n2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(b[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(j == n2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(i < n1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }因此,考虑到数组a和的大小b,可以说这种算法的运行效率为O(n1 + n2),其中我们不包括排序时间和比较字符串所花费的时间。我希望你能理解算法。如果没有,请告诉我,我会逐步指导您。干杯!
随时随地看视频慕课网APP

相关分类

Java
我要回答