Python Palindrome Rearranging
from collections import Counter
def palindrome_rearranging(input_string: str) -> bool:
counts = Counter(input_string)
odd = sum(1 for v in counts.values() if v % 2 != 0)
return odd <= 1
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Rust Palindrome Rearranging
use std::collections::HashMap;
fn palindrome_rearranging(input_string: &str) -> bool {
let mut counts: HashMap<char, i64> = HashMap::new();
for c in input_string.chars() {
*counts.entry(c).or_insert(0) += 1;
}
counts.values().filter(|&&v| v % 2 != 0).count() <= 1
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
TypeScript Palindrome Rearranging
function palindromeRearranging(inputString: string): boolean {
const counts = new Map<string, number>();
for (const ch of inputString) {
counts.set(ch, (counts.get(ch) ?? 0) + 1);
}
let c = 0;
for (const v of counts.values()) {
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.
Bash Passing Cars
passing_cars() {
local -n _a="$1"
local _passing=0 _multiply=0 _v
for _v in "${_a[@]}"; do
if (( _v == 0 )); then
((_multiply++))
elif (( _multiply > 0 )); then
_passing=$(( _passing + _multiply ))
if (( _passing > 1000000000 )); then
echo -1
return
fi
fi
done
echo "$_passing"
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
C++ Passing Cars
#include <vector>
long long passingCars(const std::vector<int>& a)
{
long long passing = 0;
long long multiply = 0;
for (int i : a) {
if (i == 0) {
++multiply;
} else if (multiply > 0) {
passing += multiply;
if (passing > 1000000000) {
return -1;
}
}
}
return passing;
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
C# Passing Cars
static long PassingCars(int[] a)
{
long passingCars = 0;
long multiply = 0;
foreach (var i in a)
{
if (i == 0)
{
multiply++;
}
else if (multiply > 0)
{
passingCars += multiply;
if (passingCars > 1000000000)
{
return -1;
}
}
}
return passingCars;
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
Elixir Passing Cars
defmodule PassingCars do
def passing_cars(a) do
a
|> Enum.reduce_while({0, 0}, fn i, {passing, multiply} ->
cond do
i == 0 ->
{:cont, {passing, multiply + 1}}
multiply > 0 ->
passing = passing + multiply
if passing > 1_000_000_000 do
{:halt, {:found, -1}}
else
{:cont, {passing, multiply}}
end
true ->
{:cont, {passing, multiply}}
end
end)
|> case do
{:found, -1} -> -1
{passing, _multiply} -> passing
end
end
end
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
Erlang Passing Cars
-module(passing_cars).
-export([passing_cars/1]).
passing_cars(A) ->
{Result, _Multiply} = lists:foldl(fun(I, {Passing, Multiply}) ->
case I of
0 -> {Passing, Multiply + 1};
_ when Multiply > 0 -> {Passing + Multiply, Multiply};
_ -> {Passing, Multiply}
end
end, {0, 0}, A),
case Result > 1000000000 of
true -> -1;
false -> Result
end.
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
Go Passing Cars
func passingCars(a []int) int {
passing, eastbound := 0, 0
for _, v := range a {
if v == 0 {
eastbound++
} else if eastbound > 0 {
passing += eastbound
if passing > 1000000000 {
return -1
}
}
}
return passing
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
Haskell Passing Cars
passingCars :: [Int] -> Int
passingCars a = go 0 0 a
where
go _ total [] = total
go zeroes total (x : xs)
| x == 0 = go (zeroes + 1) total xs
| zeroes > 0 = if total' > 1000000000 then -1 else go zeroes total' xs
| otherwise = go zeroes total xs
where
total' = total + zeroes
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.