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

时间:2018-07-09 05:00:20

标签: java string collections

在一次采访中我被问到你有两个段落

P1 = I am Lalit
P2 = Lalit Kumar

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

Ex: I am Kumar

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

4 个答案:

答案 0 :(得分:2)

这可能是一个矫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(" "));

答案 1 :(得分:1)

尝试以下代码:

public static void main(String[] args) {
  String str1 = "I am Lalit";
  String str2 = "Lalit Kumar";

  List<String> set1 = new ArrayList<>(Arrays.asList(str1.split(" ")));

  for (String s : Arrays.asList(str2.split(" "))) {
    if (set1.contains(s)) set1.remove(s);
    else set1.add(s);
  }

  System.out.println(String.join(" ", set1));
}

答案 2 :(得分:0)

您可以执行以下操作...

public static void printUncommon(String s1, String s2) {
        String[] a = s1.split(" ");
        String[] b = s2.split(" ");
        Arrays.sort(a);
        Arrays.sort(b);
        int n1 = a.length;
        int n2 = b.length;
        int i = 0;
        int j = 0;
        while(i < n1 && j < n2) {
            int compare = a[i].compareToIgnoreCase(b[j]);
            if(compare == 0) {
                i++;
                j++;
            }
            else if(compare < 0) {
                System.out.println(a[i]);
                i++;
            }
            else if(compare > 0) {
                System.out.println(b[j]);
                j++;
            }
        }
        if(i == n1) {
            while(j < n2) {
                System.out.println(b[j]);
                j++;
            }
        }
        else if(j == n2) {
            while(i < n1) {
                System.out.println(a[i]);
                i++;
            }
        }
    }

因此,考虑到数组ab的大小,可以说此算法的运行效率为O(n1 + n2),其中我们不包括排序时间和比较字符串所需的时间。

我希望您了解算法。如果没有,请告诉我,我会逐步指导您。干杯!

答案 3 :(得分:0)

使用ArrayList,以下解决方案应该可以解决,

String p1 = "I am Lalit";
        String p2 = "Lalit Kumar";
        List p2_wordList = Arrays.asList(p2.split(" ")); // create List reference
        List<String> listOfAlllWords = new ArrayList<String>(Arrays.asList(p1.split(" "))); // create a new ArrayList Object for all words in p1
        listOfAlllWords.addAll(p2_wordList); // add all words from p2
        System.out.println(listOfAlllWords); // output-> [I, am, Lalit, Lalit,Kumar]
        List<String> listOfCommonWords = new ArrayList<String>(Arrays.asList(p1.split(" "))); // create a new ArrayList Object for all word in p1 one more time
        listOfCommonWords.retainAll(p2_wordList); // retain only common words from p1 and p2
        System.out.println(listOfCommonWords); // output--> [Lalit]
        listOfAlllWords.removeAll(listOfCommonWords); // remove above common words from istOfAlllWords
        System.out.println(listOfAlllWords); // output--> [I, am, Kumar]
        StringBuffer sb = new StringBuffer(); // for final output
        Iterator<String> itr = listOfAlllWords.iterator();
        while (itr.hasNext()) {
            sb.append(itr.next() + " ");
        }
        System.out.println(sb);