PHP Ladder
function ladder(array $a, array $b): array
{
$size = count($a);
$r = array_fill(0, $size, 0);
$mod = (1 << max($b)) - 1;
$fib = [0, 1];
for ($i = 2, $limit = max($a); $i < $limit + 2; $i++) {
$fib[$i] = ($fib[$i - 1] + $fib[$i - 2]) & $mod;
}
for ($i = 0; $i < $size; $i++) {
$r[$i] = $fib[$a[$i] + 1] & ((1 << $b[$i]) - 1);
}
return $r;
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Python Ladder
def ladder(a: list[int], b: list[int]) -> list[int]:
size = len(a)
mod = (1 << max(b)) - 1
fib = [0, 1]
for i in range(2, max(a) + 2):
fib.append((fib[i - 1] + fib[i - 2]) & mod)
result = [0] * size
for i in range(size):
result[i] = fib[a[i] + 1] & ((1 << b[i]) - 1)
return result
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Rust Ladder
fn ladder(a: &[i64], b: &[i64]) -> Vec<i64> {
let size = a.len();
let mut r = vec![0i64; size];
let max_b = *b.iter().max().unwrap();
let mod_mask = (1i64 << max_b) - 1;
let max_a = *a.iter().max().unwrap();
let mut fib = vec![0i64, 1];
for i in 2..(max_a as usize + 2) {
fib.push((fib[i - 1] + fib[i - 2]) & mod_mask);
}
for i in 0..size {
r[i] = fib[(a[i] + 1) as usize] & ((1i64 << b[i]) - 1);
}
r
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
TypeScript Ladder
function ladder(a: number[], b: number[]): number[] {
const size = a.length;
const r: number[] = new Array(size).fill(0);
const mod = (1 << Math.max(...b)) - 1;
const fib: number[] = [0, 1];
const limit = Math.max(...a);
for (let i = 2; i < limit + 2; i++) {
fib[i] = (fib[i - 1] + fib[i - 2]) & mod;
}
for (let i = 0; i < size; i++) {
r[i] = fib[a[i] + 1] & ((1 << b[i]) - 1);
}
return r;
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Bash Largest String
largest_string() {
local _s=$1
local -a _c
local _i _len=${#_s}
for ((_i = 0; _i < _len; _i++)); do _c[_i]=${_s:_i:1}; done
local _cur=""
for ((_i = _len - 1; _i >= 0; _i--)); do
_cur="${_c[_i]}${_cur}"
if (( ${#_cur} == 3 )); then
if [[ "$_cur" == "abb" ]]; then
_c[_i]="b"; _c[_i+1]="a"; _c[_i+2]="a"
if [[ -n "${_c[_i+4]:-}" && "${_c[_i+4]}" == "b" ]]; then
_i=$(( _i + 4 + 1 ))
elif [[ -n "${_c[_i+3]:-}" && "${_c[_i+3]}" == "b" ]]; then
_i=$(( _i + 3 + 1 ))
elif [[ "${_c[_i+2]}" == "b" ]]; then
_i=$(( _i + 2 + 1 ))
fi
fi
if [[ "${_c[_i+1]}" == "b" ]]; then
_i=$(( _i + 1 + 1 ))
else
((_i++))
fi
_cur=""
fi
done
local _out=""
for ((_i = 0; _i < _len; _i++)); do _out="${_out}${_c[_i]}"; done
echo "$_out"
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
C++ Largest String
#include <string>
std::string largestString(std::string s)
{
int len = static_cast<int>(s.size());
std::string cur;
for (int i = len - 1; i >= 0; --i) {
cur = s[i] + cur;
if (cur.size() == 3) {
if (cur == "abb") {
s[i] = 'b';
s[i + 1] = 'a';
s[i + 2] = 'a';
if (i + 4 < len && s[i + 4] == 'b') {
i += 4 + 1;
} else if (i + 3 < len && s[i + 3] == 'b') {
i += 3 + 1;
} else if (s[i + 2] == 'b') {
i += 2 + 1;
}
}
if (i + 1 < len && s[i + 1] == 'b') {
i += 1 + 1;
} else {
++i;
}
cur.clear();
}
}
return s;
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
C# Largest String
static string LargestString(string s)
{
var chars = s.ToCharArray();
var len = chars.Length;
var cur = "";
for (int i = len - 1; i >= 0; i--)
{
cur = chars[i] + cur;
if (cur.Length == 3)
{
if (cur == "abb")
{
chars[i] = 'b';
chars[i + 1] = 'a';
chars[i + 2] = 'a';
if (i + 4 < len && chars[i + 4] == 'b')
{
i += 4 + 1;
}
else if (i + 3 < len && chars[i + 3] == 'b')
{
i += 3 + 1;
}
else if (chars[i + 2] == 'b')
{
i += 2 + 1;
}
}
if (chars[i + 1] == 'b')
{
i += 1 + 1;
}
else
{
i++;
}
cur = "";
}
}
return new string(chars);
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
Elixir Largest String
defmodule LargestString do
def largest_string(s) do
s
|> String.to_charlist()
|> Enum.chunk_by(&(&1 in ~c"ab"))
|> Enum.map(&sort_segment/1)
|> List.flatten()
|> List.to_string()
end
defp sort_segment([c | _] = segment) when c in ~c"ab" do
Enum.sort(segment, :desc)
end
defp sort_segment(segment), do: segment
end
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
Erlang Largest String
-module(largest_string).
-export([largest_string/1]).
largest_string(S) ->
Len = length(S),
Arr0 = array:from_list(S),
ArrFinal = process(Len - 1, Arr0, Len),
array:to_list(ArrFinal).
%% Fewer than 3 characters remain before the current position, so no more
%% "abb" windows can be completed.
process(P, Arr, _Len) when P < 2 ->
Arr;
process(P, Arr, Len) ->
I = P - 2,
{Arr1, I2} = handle_triple(I, Arr, Len),
process(I2 - 1, Arr1, Len).
handle_triple(I, Arr, Len) ->
Window = [array:get(I, Arr), array:get(I + 1, Arr), array:get(I + 2, Arr)],
case Window of
"abb" ->
Arr1 = array:set(I, $b, array:set(I + 1, $a, array:set(I + 2, $a, Arr))),
I1 = advance_on_b(I, Arr1, Len),
final_adjust(I1, Arr1, Len);
_ ->
final_adjust(I, Arr, Len)
end.
advance_on_b(I, Arr, Len) ->
case I + 4 < Len andalso array:get(I + 4, Arr) =:= $b of
true -> I + 4 + 1;
false ->
case I + 3 < Len andalso array:get(I + 3, Arr) =:= $b of
true -> I + 3 + 1;
false -> I
end
end.
final_adjust(I, Arr, Len) ->
case I + 1 < Len andalso array:get(I + 1, Arr) =:= $b of
true -> {Arr, I + 2};
false -> {Arr, I + 1}
end.
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
Go Largest String
func largestString(s string) string {
b := []byte(s)
length := len(b)
cur := ""
for i := length - 1; i >= 0; i-- {
cur = string(b[i]) + cur
if len(cur) == 3 {
if cur == "abb" {
b[i] = 'b'
b[i+1] = 'a'
b[i+2] = 'a'
if i+4 < length && b[i+4] == 'b' {
i += 4 + 1
} else if i+3 < length && b[i+3] == 'b' {
i += 3 + 1
} else if b[i+2] == 'b' {
i += 2 + 1
}
}
if b[i+1] == 'b' {
i += 1 + 1
} else {
i++
}
cur = ""
}
}
return string(b)
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.