Skip to content

Instantly share code, notes, and snippets.

@omokehinde
Created March 7, 2022 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omokehinde/1fc3d5f8b4eb44cfa5410404eee1ea17 to your computer and use it in GitHub Desktop.
Save omokehinde/1fc3d5f8b4eb44cfa5410404eee1ea17 to your computer and use it in GitHub Desktop.
def StringReduction(str)
# code goes here
hash = {"ab":"c", "ac":"b", "bc":"a",
"ba":"c", "ca":"b", "cb":"a"}
count = 0
while str != str[0]*str.length
if hash.has_key? :"#{str[count..count+1]}"
str = str.sub(str[count..count+1], hash[:"#{str[count..count+1]}"])
count = 0
else
count += 1
end
end
return str.length
end
puts(StringReduction("abcabc")) #=>2
puts(StringReduction("abb")) #=>1
puts(StringReduction("abc")) #=>2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment