Bash Palindrome Rearranging
palindrome_rearranging() {
local _s=$1
local -A _val
local _i _c
for ((_i = 0; _i < ${#_s}; _i++)); do
_c=${_s:_i:1}
_val[$_c]=$(( ${_val[$_c]:-0} + 1 ))
done
local _odd=0
for _c in "${!_val[@]}"; do
(( _val[$_c] % 2 != 0 )) && ((_odd++))
done
if (( _odd <= 1 )); then echo true; else echo false; fi
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
C++ Palindrome Rearranging
#include <string>
#include <unordered_map>
bool palindromeRearranging(const std::string& inputString)
{
std::unordered_map<char, int> counts;
for (char c : inputString) {
++counts[c];
}
int oddCount = 0;
for (const auto& [ch, cnt] : counts) {
if (cnt % 2 != 0) {
++oddCount;
}
}
return oddCount <= 1;
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
C# Palindrome Rearranging
static bool PalindromeRearranging(string inputString)
{
var counts = new Dictionary<char, int>();
foreach (var ch in inputString)
{
counts[ch] = counts.GetValueOrDefault(ch) + 1;
}
var oddCount = counts.Values.Count(v => v % 2 != 0);
return oddCount <= 1;
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Elixir Palindrome Rearranging
defmodule PalindromeRearranging do
def palindrome_rearranging(input_string) do
input_string
|> String.graphemes()
|> Enum.frequencies()
|> Map.values()
|> Enum.count(&(rem(&1, 2) != 0))
|> Kernel.<=(1)
end
end
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Erlang Palindrome Rearranging
-module(palindrome_rearranging).
-export([palindrome_rearranging/1]).
palindrome_rearranging(InputString) ->
Counts = lists:foldl(fun(C, Map) ->
maps:update_with(C, fun(N) -> N + 1 end, 1, Map)
end, #{}, InputString),
OddCount = length([ok || {_, V} <- maps:to_list(Counts), V rem 2 =/= 0]),
OddCount =< 1.
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Go Palindrome Rearranging
func palindromeRearranging(inputString string) bool {
counts := make(map[rune]int)
for _, r := range inputString {
counts[r]++
}
odd := 0
for _, c := range counts {
if c%2 != 0 {
odd++
}
}
return odd <= 1
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Haskell Palindrome Rearranging
import qualified Data.Map.Strict as Map
palindromeRearranging :: String -> Bool
palindromeRearranging inputString = length (filter odd (Map.elems counts)) <= 1
where
counts = Map.fromListWith (+) [(c, 1 :: Int) | c <- inputString]
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Java Palindrome Rearranging
import java.util.HashMap;
import java.util.Map;
public class Solution {
public static boolean palindromeRearranging(String inputString) {
Map<Character, Integer> counts = new HashMap<>();
for (char c : inputString.toCharArray()) {
counts.merge(c, 1, Integer::sum);
}
int oddCount = 0;
for (int v : counts.values()) {
if (v % 2 != 0) {
oddCount++;
}
}
return oddCount <= 1;
}
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Lisp Palindrome Rearranging
(defun palindrome-rearranging (input-string)
(let ((counts (make-hash-table :test 'eql))
(c 0))
(loop for ch across input-string
do (incf (gethash ch counts 0)))
(loop for v being the hash-values of counts
do (when (oddp v) (incf c)))
(<= c 1)))
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
PHP Palindrome Rearranging
function palindromeRearranging($inputString) {
$val = array_count_values(str_split($inputString));
$c = 0;
foreach($val as $v) {
if($v%2 != 0) {$c++;};
}
return $c <= 1;
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.