java—检查字符串的字符是否可以重新排列以生成回文

eivnm1vs  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(335)

我正在解决codesignal的一个问题,它要求确定字符串的字符是否可以重新排列以生成回文。我的方法是创造一个 HashMap 并将字符串的每个字符放入其中。如果 HashMap 包含所有值的偶数和,我们必须确保每个值本身是偶数。否则,如果和是奇数,我们需要看看最多有一个值不是偶数。我目前正在通过9/10考试。
我失败的测试有以下输入: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbccccaaaaaaaaaaaaa" . 我的解决方案输出 true 应该什么时候 false . 我看不出我把事情搞砸了。

boolean palindromeRearranging(String inputString) {

    HashMap<Character,Integer> letters=new HashMap<Character,Integer>();
    boolean isPalidrome=false;

    //one character strings are palindromes
    if(inputString.length()==1)
    {
        isPalidrome=true;
    }

    //look at the string and count the instances of each character
    for(int i=0;i<inputString.length();i++)
    {
        //add each character not in the hashmap already
        if(!letters.containsKey(inputString.charAt(i)))
        {
            letters.put(inputString.charAt(i), 1);
        }
        //otherwise, increment the count
        else if(letters.containsKey(inputString.charAt(i)))
        {
            letters.put(inputString.charAt(i), letters.get(inputString.charAt(i)) + 1);
        }
    }

    //sum the values in the hashmap
    int sum=0;
    Collection<Integer>val=letters.values();
    //count of values that are not even
    int v=0;

    for(int n:val)
    {
        //System.out.println("n:"+n);
        sum+=n;

        //if a value corresponding to a key isn't even, increase the count 
        if(sum%2!=0)
        {
            v++;
        }
    }

    //System.out.println(sum);

    //if we have an even sum then we have to make sure each key has an even value
    if(sum%2==0)
    {
        for(int n:val)
        {
            //if a key doesn't have an even value
            if(n%2!=0)
            {
                isPalidrome=false;
            }
            else
                isPalidrome=true;
        }
    }

    if(sum%2!=0)
    {
        if(v==1)
        {
            isPalidrome=true;
        }
        else
            isPalidrome=false;
    }

    return isPalidrome;

}
k5hmc34c

k5hmc34c1#

你忘了在里面加上中断循环 if(sum%2==0) ```
boolean palindromeRearranging(String inputString) {

HashMap<Character,Integer> letters=new HashMap<Character,Integer>();
boolean isPalidrome=false;

//one character strings are palindromes
if(inputString.length()==1)
{
    isPalidrome=true;
}

//look at the string and count the instances of each character
for(int i=0;i<inputString.length();i++)
{
    //add each character not in the hashmap already
    if(!letters.containsKey(inputString.charAt(i)))
    {
        letters.put(inputString.charAt(i), 1);
    }
    //otherwise, increment the count
    else if(letters.containsKey(inputString.charAt(i)))
    {
        letters.put(inputString.charAt(i), letters.get(inputString.charAt(i)) + 1);
    }
}

//sum the values in the hashmap
int sum=0;
Collection<Integer>val=letters.values();
//count of values that are not even
int v=0;

for(int n:val)
{
    //System.out.println("n:"+n);
    sum+=n;

    //if a value corresponding to a key isn't even, increase the count 
    if(n%2!=0)
    {
        v++;
    }
}

//System.out.println(sum);

//if we have an even sum then we have to make sure each key has an even value
if(sum%2==0)
{
    for(int n:val)
    {
        //if a key doesn't have an even value
        if(n%2!=0)
        {
            isPalidrome=false;
            break;
        }
        else
            isPalidrome=true;
    }
}

if(sum%2!=0)
{
    if(v==1)
    {
        isPalidrome=true;
    }
    else
        isPalidrome=false;
}

return isPalidrome;

}

mec1mxoz

mec1mxoz2#

下面是我的逻辑,而不是计算hashmap值的总和。基本上,回文可以由一组奇数字符组成,也可以由任意一组偶数字符组成。假设有一个字符串aaaabbccc。这里有两组偶数字符(a&b)和一组奇数字符,如果重新排列,它们可以是回文。下面是我的c++程序。


# include <bits/stdc++.h>

using namespace std;

int main()
{
        string input;
        cin>> input;

        cout<< input<< endl;

        int a[26] = {0};

        for (int i = 0; input[i] != '\0'; i++)
            a[input[i] - 97]++;

        int odd = 0;
        for (int i = 0; i < 26; i++)
        {
            if (a[i] != 0)
            {
                if (a[i] % 2)
                {
                    odd++;
                    if (odd > 1)
                    {
                        cout<< "false"<< endl;
                        break;
                    }
                }

            }
        }

        if (odd < 2)
            cout<< "true"<< endl;
    return 0;
}

你可以尝试这个逻辑来检查回文。

qyswt5oh

qyswt5oh3#

在这部分代码中

if(sum%2!=0)
    {
        v++;
    }

应该改成

if(n%2!=0)
    {
        v++;
    }

相关问题