英文:
HackerRank Anagram program in JAVA is running fine on my Eclipse but not on HackerRank platform
问题
I'm here to provide the translated code:
我正在尝试解决HackerRank上的一个JAVA问题。在我的Eclipse上运行正常,但在HackerRank平台上没有给出预期的输出。问题是要检查两个字符串是否是彼此的字母重排(不区分大小写)。问题链接:https://www.hackerrank.com/challenges/java-anagrams/problem?isFullScreen=true
以下是代码:
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
if(a.length()!=b.length())
return false;
char c1[]=a.toCharArray();
char c2[]=b.toCharArray();
java.util.Arrays.sort(c1);
java.util.Arrays.sort(c2);
a=String.valueOf(c1);
b=String.valueOf(c2);
if(a.equalsIgnoreCase(b))
return true;
else
return false;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}
这是您的Java代码的中文翻译。
英文:
I am trying out a HackerRank problem in JAVA. It is running fine on my Eclipse but it is not giving the expected output on HackerRank platform. The problem is to check whether two strings are anagrams of each other or not (ignoring their case). Link to the problem: https://www.hackerrank.com/challenges/java-anagrams/problem?isFullScreen=true
Below is the code:
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
if(a.length()!=b.length())
return false;
char c1[]=a.toCharArray();
char c2[]=b.toCharArray();
java.util.Arrays.sort(c1);
java.util.Arrays.sort(c2);
a=String.valueOf(c1);
b=String.valueOf(c2);
if(a.equalsIgnoreCase(b))
return true;
else
return false;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}
It is passing two test cases but not the third one in which String a="Hello" and String b="hello".
But it is passing this test case on eclipse.
Please advise.
答案1
得分: 0
在排序结束后,你的数组包含如下内容,
char c1[]=a.toCharArray();
char c2[]=b.toCharArray();
java.util.Arrays.sort(c1); // c1 = ['H','e','l','l','o']
java.util.Arrays.sort(c2); // c2 = ['e','h','l','l','o']
在这一步之后,当你将它们转换回字符串时,
a=String.valueOf(c1); //a = "Hello"
b=String.valueOf(c2); //b = "ehllo"
因此,当你比较 a 和 b 时返回 false。
解决方法:在排序之前,你可能需要将字符串转换为小写/大写,可以使用 toLowerCase()
或 toUpperCase()
。
英文:
At the end of the sorting your arrays contains,
char c1[]=a.toCharArray();
char c2[]=b.toCharArray();
java.util.Arrays.sort(c1); // c1 = ['H','e','l','l','o']
java.util.Arrays.sort(c2); // c2 = ['e','h','l','l','o']
After this step when you convert this back to String,
a=String.valueOf(c1); //a = "Hello"
b=String.valueOf(c2); //b = "ehllo"
Hence it return false when you compare the a and b.
Solution: you might want to convert the strings to lowercase/uppercase before you sort them by using (toLowerCase() or toUpperCase()).
答案2
得分: 0
为了解决这个问题,我们需要创建一个大小为26的字符频率整数数组(26的大小对应每个字母),并将其初始化为零,以便数组中有26个零。
int[] frequency = new int[26];
然后,我们将遍历第一个字符串,创建一个整数索引,该索引将计算字符串中每个字母的频率,并递增表示特定字母的频率数组的索引。例如,如果字符串是CAT,那么frequency[0]、frequency[2]和frequency[19]将分别递增,因为'C'、'A'和'T'分别由索引2、0和19表示。
for(int i=0;i<a.length();i++){
int index = a.charAt(i)-'a';
frequency[index]++;
}
现在,我们将以与第一个字符串相同的方式遍历第二个字符串,但在这种情况下,与递增频率数组不同,我们将递减频率。
for(int i=0;i<b.length();i++){
int index = b.charAt(i)-'a';
frequency[index]--;
}
最后,我们将检查频率数组,如果在执行上述操作后所有频率数组的索引都变为零,这意味着字符串是字谜,因为在第一个字符串中找到的字母,如果在第二个字符串中找到,将减小频率。
for(int i=0;i<26;i++){
if(frequency[i] != 0)
return false;
}
在开始之前不要忘记将字符串转换为小写,否则会显示运行时错误。
a = a.toLowerCase();
b = b.toLowerCase();
英文:
To solve this problem we have to create an int array for character frequency of size 26 ( 26 size is for every alphabet) which is initialized to zero so that we have 26 zeros in the array.
int[] frequency = new int[26];
then we will loop through first string creating an int index which will count the frequency of each alphabet in the string and increment the index of the frequency array representing that particular alphabet for example if the string is CAT the frequency[0], frequency[2]and frequency[19] will be incremented as 'C', 'A', and 'T' are represented by 2,0 and 19 indexes respectively.
for(int i=0;i<a.length();i++){
int index = a.charAt(i)-'a';
frequency[index]++;
}
now we will loop through second string the same way we did with first-string but in this case, rather then incrementing the frequency array we will decrement the frequency.
for(int i=0;i<b.length();i++){
int index = b.charAt(i)-'a';
frequency[index]--;
}
finally, we will check the frequency array if after performing the above operation all the indexes of frequency array come to zero it means that strings are anagram as the alphabets found in the first string which increment the frequency when found in the second string will decrease the frequency.
for(int i=0;i<26;i++){
if(frequency[i] != 0)
return false;
}
don't forget to convert string to lowercase before starting this otherwise, it shows runtime error.
a = a.toLowerCase();
b = b.toLowerCase();
for complete solution visit https://github.com/Gursimir/Java-Practice/blob/master/anagram.java
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论