Skip to content

Instantly share code, notes, and snippets.

@netmask
Created May 11, 2022 20:44
Show Gist options
  • Save netmask/6174ea07bacd06e1004dae5dd1b1f203 to your computer and use it in GitHub Desktop.
Save netmask/6174ea07bacd06e1004dae5dd1b1f203 to your computer and use it in GitHub Desktop.
non consecutive next letter
def replace(r)
starray = r.split("")
starray.each_with_index.reduce([]) do |word, current|
letter, index = current
next_letter = starray[index + 1]
word <<
if letter == '?'
get_next_letter(word.last, next_letter)
else
letter
end
end.join
end
def get_next_letter(last, next_letter)
return 'a' unless last
non_consecutive = last.next
return non_consecutive if next_letter == '?'
if non_consecutive != next_letter
non_consecutive
else
get_next_letter(non_consecutive, next_letter)
end
end
puts replace("????ab?????")
#abcdabcdefg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment