给定两个单词,return 两个单词中出现的字母列表 - Java

Given two words, return a list of letters that occur in both words - Java

我有另一个挑战,当用户输入 2 个单词时,java 向控制台打印两个单词中出现的字母列表。我的想法是从两个输入中创建一个 char 数组。但是当我这样做时,我意识到有些单词比其他单词长(很明显),所以我使用 if 语句来解释这两种可能性(单词 1 比单词 2 大,反之亦然)。当我这样做时,我得到了一个 ArrayIndexOutOfBoundsException。我不知道该怎么做。我在 Whosebug 上寻找其他解决方案。请帮忙!我的挑战方法如下。 PS:这是一个挑战 "within a challenge",我必须让用户从五个程序中选择一个。当用户选择3时,它运行下面的程序。

System.out.println("Enter a String");
Scanner scan = new Scanner(System.in);
    String word1 = scan.nextLine();
    System.out.println("Enter another String");
    String word2 = scan.nextLine();
    String list = "";
    System.out.println("");
    System.out.println("");
    char[] word1Chars = word1.toCharArray();
    char[] word2Chars = word2.toCharArray();
    if(word1Chars.length > word2Chars.length) {
        for(int s = 1; s < word1Chars.length;) {
            if(word1Chars[s] == word2Chars[s]) {
                list = "" + word1Chars[s];
            }
        }
    } else if(word2Chars.length > word2Chars.length) {
        for(int s = 1; s < word2Chars.length;) {
            if(word1Chars[s] == word2Chars[s]) {
                list = "" + word2Chars[s];
            }
        }
    }
    System.out.println(list);
    return list;

这个:

if(word1Chars.length > word2Chars.length) {
    for(int s = 1; s < word1Chars.length;) {

递增直到 s 是两个字符串的 LONGER 的长度,但是调用这个:

if(word1Chars[s] == word2Chars[s])

因此,如果 word1 有 10 个字母,最终 s 将等于 9,您将检查 word2Chars[9],这可能是越界的。

你可以这样做:

Scanner input = new Scanner(System.in);
System.out.println("Enter 2 strings : ");
String word1 = input.next();
String word2 = input.next();
String commonChar="";
for(char ch: word1.toCharArray()) {
    if(word2.contains(ch+"") && !commonChar.contains(ch+"")) {
        commonChar+=ch;
    }
}
System.out.println(commonChar);

o/p

Enter 2 strings : 
12345678
111222
12

在这里我们检查字母是否包含在另一个词中然后我们添加它但我们只添加一次。

一个简单的正则表达式就可以解决问题:

public static void main(String[] args) {
        String s1 = "abcd";
        String s2 = "saxydp";
        System.out.println(s1.replaceAll("[^+" + s2 + "]", "").replaceAll("(\w).*?\1+",""));    
}

O/P :

ad

解释:首先在 s1 中替换在 s2 中没有出现的所有内容。接下来,替换每个重复的元素 :)。纯实验代码..

如果这不应该很快,我建议编写尽可能少的代码,例如给定字符串 word1, word2:

Set<String> s1 = new HashSet<>( Arrays.asList( word1.split( "" ) ) );
Set<String> s2 = new HashSet<>( Arrays.asList( word2.split( "" ) ) );
s1.retainAll( s2 );
for( String c: s1 ) System.out.print(c);
System.out.println();

替换word1和word2初始化后的代码

我认为最简单的方法是使用嵌套的 for 循环遍历 char 数组并比较每个单独的 char。然后根据需要添加到您的列表中。

类似于:

for (int i=0; i < word1chars.length; i++)
for(int x=0; x < word2chars.length; x++)
    if (word1chars[i] == word2chars[x])
    {
        //add your letter to your list here
    }

使用集合也许可以获得更好的性能。它遵循一个例子:

import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

class C {
    public static void main(String[] args) {
        String w1 = "samefoo";
        String w2 = "barsame";

        Set set = new HashSet();

        char[] a1 = w1.toCharArray();
        char[] a2 = w2.toCharArray();
        List list = new ArrayList();

        for(int i = 0; i < a1.length; i++)
            set.add(a1[i]);

        for(int i = 0; i < a2.length; i++)
            if(set.contains(a2[i]))
                list.add(a2[i]);

        System.out.println(list);
    }
}

这可能不如其他一些好,但既然我写了它,不妨将它添加到列表中。

char[] word1Chars = word1.toCharArray(); char[] word2Chars = word2.toCharArray(); Arrays.sort(word1Chars); Arrays.sort(word2Chars); int i = 0, j = 0; String duplicates = ""; while (i < word1Chars.length && j < word2Chars.length) { if (word1Chars[i] == word2Chars[j]) { duplicates += word1Chars[i]; i++; j++; } else if (word1Chars[i] > word2Chars[j]) j++; else i++; } System.out.println(duplicates);