在不同索引

时间:2016-01-26 22:46:15

标签: java regex string

我是C ++程序员。出于兴趣,我正在开发一个java应用程序。

我在java中有两个字符串:

String word1 = "Fold";
String word2 = "Flow";

现在我需要一个函数来获取两个字符串中匹配字符的计数,但是那些处于不同索引的字符。字符串可以是任意长度,但总是两个字的长度相同。

添加了: 我们应该通过两个单词中的多次出现来增加一个字符的计数。例如:abcd和xyaa应该返回1,但是abca和xaay应该返回2.希望现在很清楚。

对于例如:,上述示例的计数应为2(仅考虑字母' o'以及' l'。虽然字母' f'是在两个单词中都存在,因为它存在于两个字符串的相同索引处,所以不予考虑。

我的方法是创建两个地图变量Map,并为所有字符初始化为0。然后计算两个字符串中每个字母出现次数的计数,最后检查这些字符中有多少个计数多于一个。

例如:

    Map<Character, Integer> word_count_1 = createMap(); // initialize with a:0, b:0, c:0,...z:0
    Map<Character, Integer> word_count_2 = createMap(); // initialize with a:0, b:0, c:0,...z:0

    int count, value;

    for (int i=0; i<word1.length(); i++)
    {
        if (word1.charAt(i) != word2.charAt(i))
        {
            value = word_count_1.get(word1.charAt(i));
            word_count_1.put(word1.charAt(i), ++value);

            value= word_count_2.get(word2.charAt(i));
            word_count_2.put(word2.charAt(i), ++value);
        }
    }

    Set set = word_count_2.entrySet();
    Iterator i = set.iterator();
    Map.Entry<Character, Integer> iter;

    while(i.hasNext())
    {
        iter = (Map.Entry)i.next();
        if ( (iter.getValue() > 0) && (word_count_1.get(iter.getKey())) > 0 )
        {
            count++; // This line has a bug. We shall ignore it for now
        }
    }

还有其他更好的方法来计算,而不是我想要做的事情吗?我对我所做的事情感觉不太好。

编辑:

行数++(我提到有一个bug)应该改为以下来给出正确的结果:

int letterCount1 = word_count_1.get(iter.getKey());
int letterCount2 = iter.getValue();
if ( (letterCount1 > 0) && (letterCount2 > 0 )
{
    int minVal = letterCount1;
    if (minVal > letterCount2)
        minVal = letterCount2;
    count+= minVal;
}

4 个答案:

答案 0 :(得分:1)

Java 8解决方案

  public int duplicates(String wordOne, String wordTwo ){
    Set<Character> charSet = new HashSet(109);
    wordOne.chars().mapToObj(i -> (char)i).forEach(letter->charSet.add(letter));

    int count = 0;
    for(int i = 0; i < wordTwo.length(); i++)
      if( charSet.contains(wordTwo.charAt(i)) && wordTwo.charAt(i) != wordOne.charAt(i) )
        count++;

    return count;
  }

duplicates("Fold", "Flow"); // -> 2

答案 1 :(得分:0)

//Create set which contains word1's unique chars
Set<Character> word1Chars = new HashSet<>();

for(int i = 0; i< word1.length(); i++)
{
    char ch = word1.charAt(i);
    word1Chars.add(ch);
}

// Count how many chars in word2 are contained in word1 but in another position
int count = 0;
for(int i = 0; i < word2.length(); i++)
{
    char ch = word2.charAt(i);
    if(ch != word1.charAt(i) && word1Chars.contains(ch))
    {
        count++;
    }
}

编辑:您必须考虑到您可能会根据您迭代的单词获得不同的计数。例如:&#34; abc&#34;和&#34; daa&#34 ;; &#34; ABC&#34;有1但是&#34; daa&#34;有2。 如果您想要两个单词中的对应项总数,则需要相应地修改此代码。

答案 2 :(得分:0)

有更好的语法迭代集合(参见下面的示例)但实际计数看起来很好。

Map<Character, Integer> word_count_1 = createMap(); // initialize with a:0, b:0, c:0,...z:0
Map<Character, Integer> word_count_2 = createMap(); // initialize with a:0, b:0, c:0,...z:0<Character, Integer>

int count, value;

for (int i=0; i<word1.length(); i++)
{
    if (word1.charAt(i) != word2.charAt(i))
    {
        value = word_count_1.get(word1.charAt(i));
        word_count_1.put(word1.charAt(i), ++value);

        value= word_count_2.get(word2.charAt(i));
        word_count_2.put(word2.charAt(i), ++value);
    }
}

Set set = word_count_2.entrySet();
for(<Map.Entry<Character, Integer>> iter:set) 
{
    if ( (iter.getValue() > 0) && (word_count_1.get(iter.getKey())) > 0 )
    {
        count++; // This line has a bug. We shall ignore it for now
    }
}

答案 3 :(得分:0)

您无需为所有字符初始化地图。

public static int matchCharCountInDifferentIndex(String word1, String word2) {
    Map<Character, Integer> word_count_1 = new HashMap<>();
    Map<Character, Integer> word_count_2 = new HashMap<>();

    for (int i=0; i<word1.length(); i++)
    {
        if (word1.charAt(i) != word2.charAt(i))
        {
            word_count_1.compute(word1.charAt(i), (k, v) -> v == null ? 1 : v + 1);
            word_count_2.compute(word2.charAt(i), (k, v) -> v == null ? 1 : v + 1);
        }
    }

    int count = 0;
    for (Map.Entry<Character, Integer> e : word_count_2.entrySet())
    {
        count += Math.min(e.getValue(), word_count_1.getOrDefault(e.getKey(), 0));
    }

    System.out.printf("word1=%s word2=%s result=%d%n", word_count_1, word_count_2, count);
    return count;
}

测试

matchCharCountInDifferentIndex("Fold", "Flow"); // -> word1={d=1, l=1, o=1} word2={w=1, l=1, o=1} result=2
matchCharCountInDifferentIndex("abca", "xaay"); // -> word1={a=2, b=1, c=1} word2={a=2, x=1, y=1} result=2

在此代码中

map.compute(key, (k, v) -> v == null ? 1 : v + 1);

相当于

map.put(key, map.getOrDefault(key, 0) + 1);

map.getOrDefault(key, 0)

相当于

map.containsKey(key) ? map.get(key) : 0;
相关问题