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.