分割字符串并检查它们是否彼此相似 [英] To split a string and to check if they are anagram to each other

查看:61
本文介绍了分割字符串并检查它们是否彼此相似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决以下问题: https://www.hackerrank.com/challenges/anagram

I am trying to solve this question: https://www.hackerrank.com/challenges/anagram

这是我的代码:

import java.util.*;

public class Anagram {

    public static void main(String[] args)
    {
        Scanner reader = new Scanner(System.in);
        int t = reader.nextInt();

        while((t--) > 0)
        {    
            String input = reader.nextLine();

            if((input.length()) % 2 == 1)
                System.out.println(-1);
            else
            {
                int x = input.length();
                int q = (int)(Math.floor((x / 2)));

                String input1 = input.substring(0, q);
                String input2 = input.substring(q, x);

                int [] count2 = new int[26];
                for(int i = 0; i < input2.length(); i++)
                {
                    char ch2 = input2.charAt(i);
                    count2[ch2 - 'a']++;
                }

                // int [] count1 = new int[26];
                for(int i = 0; i < input1.length(); i++)
                {
                    char ch1 = input1.charAt(i);
                    if(count2[i] > 0)
                        count2[ch1 - 'a']--;
                }

                int count = 0;
                for(int j = 0; j < 26; j++)
                {
                    count = count + Math.abs(count2[j]);
                }

                System.out.println(count);
            } 
        }
    }
} 

样本输入

6
aaabbb
ab
abc
mnop
xyyx
xaxbbbxx

预期产量

3
1
-1
2
0
1

我的输出

0
4
1
-1
2
2

有人可以告诉我哪里出了问题吗?我找不到错误...

Can anyone please tell me where it went wrong? I couldn't find the error...

推荐答案

由于此行,您的第一个输出始终为0:

Your first output always comes 0, because of this line:

int t = reader.nextInt();

后跟reader.nextLine();. 查看此信息,以获取更多有关的信息.为了快速解决,请将该行更改为:

followed by reader.nextLine();. Check this post for more details on that. For quick fix, change that line to:

int t = Integer.parseInt(reader.nextLine());

现在,让我们从以下两个语句开始:

Now, let's start with the below two statements:

int x = input.length();
int q = (int)(Math.floor((x/2)));

无需在此处执行Math.floor. x/2是整数除法,只会给您整数结果.

No need to do a Math.floor there. x/2 is an integer division, and will give you integer result only.

移动到第二个for循环.您使用了以下条件:

Moving to the 2nd for loop. You used the following condition:

if(count2[i]>0)
    count2[ch1-'a']--;

是否注意到条件错误?它应该是count2[ch1 - 'a'] > 0.而且,您将错过该计数不大于0的情况,在这种情况下,您将必须执行++.顺便说一句,由于您始终在执行Math.abs(),因此不需要该条件.只需执行--:

Notice the mistake there in condition? It should be count2[ch1 - 'a'] > 0. And also, you will miss the case where that count is not greater than 0, in which case you would have to do a ++. BTW, since you're anyways doing a Math.abs(), you don't need the condition. Just do a --:

for( int i = 0; i < input1.length(); i++ ) {
    char ch1 = input1.charAt(i);
    count2[ch1-'a']--;
}

顺便说一句,最终结果将是count / 2,而不是count,因为count包含从输入1到输入2的总不匹配,反之亦然.但是我们只需要修复其中一个以匹配另一个即可.因此,只需考虑总失配的一半.

BTW, the final result would be count / 2, and not count, because count contains the total mismatch from input1 to input2 and vice-versa. But we just have to fix one of them to match the other. So, just consider half the total mismatch.

这篇关于分割字符串并检查它们是否彼此相似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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