检查字符串数组输入的Anagram [英] Checking Anagram of a String array input

查看:75
本文介绍了检查字符串数组输入的Anagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我下面的代码,用于检查给定字符串数组的字谜.
即使在最简单的情况下,只有一个输入,也总是给我错误.
我不明白我是无法将字符串数组正确转换为字符串还是算法是错误的.

This is my code below to check anagram of a given string array.
It always gives me false even in the simplest case with only one input.
I don't understand am I not converting string array into string correctly or my algorithm is plain wrong.

public class anagram
{
     static boolean isAnagram(String[] s1, String[] s2) { 
        String str = s1.toString();
        String str2 = s2.toString();
        if (str.length() != str2.length()) 
            return false;

        for (int i =0; i<str.length();i++)
        { 
            for (int j = 0;j<str2.length();j++)
            { 
                if (s1[i] == s2[j]) {
                    return true; 
                }
                return false; 
            }                   
        } 
        return true; 
    }

    public static void main(String [] args){
        String [] s1 = {"shot"};
        String [] s2 = {"host"};
        System.out.println(isAnagram(s1,s2));
    }
}

能帮我找出问题所在吗?

Can you please help me identify what is wrong?

推荐答案

您的检查算法似乎有些不正确.
在此处编辑了 isAnagram 函数:

Your algorithm for checking seems to be a little incorrect.
Edited the isAnagram function here:

public static void main(String[] args)
{
    String s1 = "shotaabb";
    String s2 = "hostbaba";
    System.out.printf("String s1: %s, String s2: %s%n", s1, s2);
    System.out.println(isAnagram(s1, s2) ?
            "Is anagram" : "Is not an anagram");
}

static boolean isAnagram(String s1, String s2)
{
    String str1 = new String(s1);
    String str2 = new String(s2);

    // Ensures that both strings are of the same length
    if (str1.length() != str2.length())
        return false;

    int str1Len = str1.length();
    for (int i = 0; i < str1Len; i++)
    {
        int charIndex = str2.indexOf(str1.charAt(i));

        if(charIndex == -1) // Not found in str2
            return false;
        else
        {
            // Remove the character from str2
            str2 = str2.substring(0, charIndex) +
                str2.substring(charIndex + 1);
        }
    }

    return true;
}

代码的作用是

  • 从s1中获取一个字符,在s2中找到该字符的索引
  • 如果索引为-1(在s2中找不到字符),则返回false
  • 如果可以在s2中找到该字符,请将其从s2中删除
  • 最后,如果可以在s2中找到s1中的所有字符,请返回true
  • 基于两个字符串的长度相同的事实,如果可以找到s1中的所有字符&从s2 中删除后,s1是s2&的字谜.反之亦然.
  • Gets a character from s1, finds the index of that character inside s2
  • If the index is -1, character not found inside s2, return false
  • If the character can be found inside s2, remove it from s2
  • At the end, if all characters inside s1 can be found in s2, return true
  • Based on the fact that both strings are of the same length, if all character in s1 can be found & removed from s2, s1 is an anagram of s2 & vice versa.

输出:

String s1: shotaabb, String s2: hostbaba
Is anagram


更新(比较字符串数组):

String[] strArr1 = {"shot", "dcba"};
String[] strArr2 = {"host", "abcd"};

for(String s1 : strArr1)
{
    for(String s2 : strArr2)
    {
        System.out.printf("%nString s1: %s, String s2: %s%n", s1, s2);
        System.out.println(isAnagram(s1, s2) ?
            "Is anagram" : "Is not an anagram");
    }
}

更新代码的输出:

String s1: shot, String s2: host
Is anagram

String s1: shot, String s2: abcd
Is not an anagram

String s1: dcba, String s2: host
Is not an anagram

String s1: dcba, String s2: abcd
Is anagram 

这篇关于检查字符串数组输入的Anagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆