谁能解释这个代码块的工作原理?

时间:2018-07-16 15:51:39

标签: java char anagram charat

我试图理解这段代码,它检查2个不同字符串的字谜。

int[] charSet = new int[256];

    for (int i = 0; i < sA.length(); i++) {
        charSet[sA.charAt(i)]++;
    }
    for (int i = 0; i < sB.length(); i++) {
        charSet[sB.charAt(i)]--;
    }

    int deletion = 0;
    for (int i = 0; i < 256; i++) {
        deletion += Math.abs(charSet[i]);
    }

    System.out.println("The amount of deletion needed: " + deletion);

我使用调试来真正掌握将char索引放置在数组中的想法,但是当我检查数组的内容时,它们是0或1。但是sA.charat(i)是否返回字符串的索引不是0还是1?

所以charSet[sA.charAt(i)]++;对于我的理解,这段代码实际上是做什么的,它获取字符串的char索引并将其添加到数组中,但是++的意义是什么?

此外,更具体地说,deletion += Math.abs(charSet[i]);这行代码是如何工作的,因为它所做的只是将数组的相应索引添加到另一个数组的顶部。但是我真的不明白如何检查2个字符串的字谜。

如果有人能详细解释,我将不胜感激。

4 个答案:

答案 0 :(得分:1)

您似乎误解了charSet[sA.charAt(i)]++;的目的和行为。一旦您了解了为什么会出现该行,其他所有内容都会变得简单明了。

charSet这里计算的是第一个字符串中每个字符的数量。例如,如果字符串为aab,则charSet的索引97将为2,索引98将为1,其余均为0。 {a的int值为97,b的int值为98。

第一个for循环遍历第一个字符串的每个字符。 charSet[sA.charAt(i)]++;基本上使该字符的计数增加1。对于字符串aab,表达式的计算结果为:

// note that charAt(i) returns the character at index i of the string
charSet[97]++; // 1st iteration
charSet[97]++; // 2nd iteration
charSet[98]++; // 3rd iteration

现在,第二个for循环执行与第二个字符串相反的操作。这次,我们倒计时。为什么?因为如果两个字符串都是字谜,那么在对字符进行递增计数并对相同字符进行递减计数之后,我们将以charSet结尾并填充0。

让我们说这两个字符串不是字谜。这意味着在前两个for循环之后,charSet包含一些非0。我们将这些非0的绝对值相加,以得出需要添加/删除多少个字符才能构成两个字符串字谜。


请注意,如果字符串包含值大于256的字符,此程序将崩溃!解决此问题的更好方法是使用HashMap对字符进行计数。

答案 1 :(得分:0)

charSet表示char基本类型的256个不同的可能值。 正如biziclop和Andreas在评论中指出的那样,还有很多。

两个字符串(本质上是char数组)被迭代。 charSet数组基本上对字符串中每个char的出现进行计数。 对于第一个字符串,每次出现的次数增加一个,对于第二个字符串,减少一个。这样,如果这些字符串是彼此的字谜,则charSet数组中的每个int都应为0。

最后,对charSet数组进行迭代以总结字符串之间缺少的字符。您可以考虑使用删除变量来衡量字符串之间的差异。

答案 2 :(得分:0)

字谜是可以通过重新排列另一个单词的字母而形成的单词或短语。例如pale -> leap。这两个词使用相同的字母集,即l, a, p, e

您共享的代码遵循一种逻辑,该逻辑初始化一个长度为256的空整数数组,以包括所有小写,大写,特殊字符等。

首先循环遍历第一个字符串中的每个字母,然后递增整数数组中字母的ascii值的计数。

然后,它循环遍历第二个字符串中的每个字母,并递减整数数组中该字母的ascii值的计数。

其背后的逻辑是,如果两个字符串都是字谜,这意味着它们使用相同的字符集。如果是这种情况,则在两次迭代之后,int数组中每个元素的值仍将保持等于零。如果任何值大于或小于零,则表示两个字符串中使用的字母存在差异,因此它们不是点字符号。

答案 3 :(得分:0)

public static int getDeletion(String sA, String sB) {
    //this creates an array with 256 items, so that is an item for all the characters.
    int[] charSet = new int[256];

    //This goes through each letter of sA, and it increments the value at the value of the current letter in ASCII
    //So if the first letter is A , it will increment the 65th item of charSet, because 65 is the ASCII value of a.
    //It does this for all letters of sA.
    for (int i = 0; i < sA.length(); i++) {
        charSet[sA.charAt(i)]++;
    }

    //It does the same here for sB, but it subtracts the value.
    for (int i = 0; i < sB.length(); i++) {
        charSet[sB.charAt(i)]--;
    }

    //now we have at each item of charSet the amount of difference between the two strings for each letter.
    //So if sA has 2 As and sB has 1 A, item 65 of charSet will be 1.
    //If sA has 1 A and sB has 2 As, item 65 of charSet will be -1.

    int deletion = 0;
    for (int i = 0; i < 256; i++) {
        //Here we add the amount of difference from each character to the 'deletion' variable. This will give us a total difference score.
        //The Math.abs() makes sure the item is positive (so -5 will become 5).
        deletion += Math.abs(charSet[i]);
    }
    return deletion;

    //So say we compare "adc" and "bca". After the 2nd for loop, each item of charSet is 0, except value 98 and 100 
    //(ASCII value for b and c respectively).
    //value 98 will be -1, because sB has 1 more b than sA
    //value 100 will be 1, because sA has 1 more d than sB
    //The -1 will be converted to a 1, because of the Math.abs()
    //So in the for loop every value is added up to become 2, which is returned.
}