输出错误,比较异位词字符串时。

huangapple go评论60阅读模式
英文:

Output error when comparing anagrams strings

问题

这是自定义案例的输出:

输出错误,比较异位词字符串时。

输出错误,比较异位词字符串时。

英文:

I was solving an anagram question on geek for geeks but I think the output format is not similar as shown in question over all my code is correct but there is a problem in test cases so see my code and tell me what's wrong in this.

/*package whatever //do not write package name here */
    
import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine();
        boolean isAnagram = true;
        while (t-- > 0) {
            String a = sc.nextLine();
            String b = sc.nextLine();

            int al[] = new int[256];
            for (char c : a.toCharArray()) {
                int index = (int) c;
                al[index]++;
            }
            for (char c : b.toCharArray()) {
                int index = (int) c;
                al[index]--;
            }
            for (int i = 0; i < 256; i++) {
                if (al[i] != 0) {
                    isAnagram = false;
                    break;
                }
            }

            if (isAnagram) {
                System.out.println("1");
            } else {
                System.out.println("0");
            }
        }
    }
}

This is the output of custom case:

输出错误,比较异位词字符串时。

输出错误,比较异位词字符串时。

答案1

得分: 1

需要将boolean isAnagram = true;移到while (t-- > 0)循环内。

目前,如果在第一个测试中文本不是变位词,现有代码不会将isAnagram设置为true

另外,如果两个输入字符串的长度不同,它们不能是变位词,将char显式转换为int是多余的。

话虽如此,while循环的内容应该更新如下:

while (t-- > 0) {
    String a = sc.nextLine();
    String b = sc.nextLine();

    boolean isAnagram = a.length() == b.length();
    if (isAnagram) {
        int al[] = new int[256];
        for (int c : a.toCharArray()) {
            al[c]++;
        }

        for (int c : b.toCharArray()) {
            al[c]--;
        }

        for (int i = 0; i < al.length; i++) {
            if (al[i] != 0) {
                isAnagram = false;
                break;
            }
        }
    }
    System.out.println(isAnagram ? 1 : 0);
}
英文:

You need to move boolean isAnagram = true; inside while (t-- &gt; 0) loop.

Currently, if in the first test the text is not an anagram, existing code does not set isAnagram to true.

Also, the two input strings cannot be anagrams if their lengths are different, and explicit casting of char to int is redundant.

That being said, the contents of the while loop should be updated as follows:

while (t-- &gt; 0) {
    String a = sc.nextLine();
    String b = sc.nextLine();

    boolean isAnagram = a.length() == b.length();
    if (isAnagram) {
        int al[] = new int[256];
        for (int c : a.toCharArray()) {
            al[c]++;
        }

        for (int c : b.toCharArray()) {
            al[c]--;
        }

        for (int i = 0; i &lt; al.length; i++) {
            if (al[i] != 0) {
                isAnagram = false;
                break;
            }
        }
    }
    System.out.println(isAnagram ? 1 : 0);
}

答案2

得分: 0

要检查两个字符串是否是变位词,您可以比较这些字符串的两个已排序字符数组:

String a = "chinmay";
String b = "maychin";

char[] aArr = a.toCharArray();
char[] bArr = b.toCharArray();

Arrays.sort(aArr);
Arrays.sort(bArr);

boolean isAnagram = Arrays.equals(aArr, bArr);

System.out.println(isAnagram); // true

另请参阅:如何检查一个单词是否有一个回文变位词?

英文:

To check if two strings are anagrams, you can compare the two sorted character arrays of those strings:

String a = &quot;chinmay&quot;;
String b = &quot;maychin&quot;;

char[] aArr = a.toCharArray();
char[] bArr = b.toCharArray();

Arrays.sort(aArr);
Arrays.sort(bArr);

boolean isAnagram = Arrays.equals(aArr, bArr);

System.out.println(isAnagram); // true

<sup>See also: How do you check if a word has an anagram that is a palindrome?</sup>

huangapple
  • 本文由 发表于 2020年8月11日 14:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63352574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定