[英]check if the characters of a string can be rearranged to make a palindrome
我正在處理 codesignal 的一個問題,它要求確定字符串的字符是否可以重新排列以構成回文。 我的方法是創建一個HashMap
並將字符串的每個字符放入其中。 如果HashMap
包含所有值的偶數和,我們必須確保每個值本身都是偶數。 否則,如果和是奇數,我們需要看是否至多有一個值不是偶數。 我目前正在通過 9/10 測試。
我失敗的測試有輸入: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbccccaaaaaaaaaaaaa"
。 我的解決方案在應該為false
時輸出true
。 我看不出我在哪里搞砸了。
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;
}
您忘記在if(sum%2==0)
內添加 break in loop
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;
}
在這部分代碼中
if(sum%2!=0)
{
v++;
}
它應該更改為
if(n%2!=0)
{
v++;
}
下面是我的邏輯,而不是計算 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;
}
你可以試試這個邏輯來檢查回文。
boolean solution(String str) {
HashMap<String ,Integer> map=new HashMap<>();
int count=0;
int count1=0;
for(String w:str.split("")){
if(!map.containsKey(w)){
map.put(w, 1);
}
else if(map.containsKey(w)){
map.put(w, map.get(w)+1) ;}
}
boolean bool=false;
for(Integer w: map.values()){
if(w==1){
count++;}
else if(w%2==0){
bool=true;}
else if(w%2!=0){
count1++;}
bool=count>1||count1>1?false:true;
}
return bool;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.