英文:
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-- > 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-- > 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);
}
答案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 = "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
<sup>See also: How do you check if a word has an anagram that is a palindrome?</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论