Skip to content

Instantly share code, notes, and snippets.

@faethonm
Created December 18, 2015 17:42
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 faethonm/42779766e90876ae1c94 to your computer and use it in GitHub Desktop.
Save faethonm/42779766e90876ae1c94 to your computer and use it in GitHub Desktop.
A string is called lucky if no two consecutive characters are equal. How many lucky strings can you get by reordering letters in a given string *s*? *s* itself counts, too, if it is lucky. 1 <= length(*s*) <= 10 *s[i]* is in 'a'..'z' Example 1 input: "ab" output: 2 Two lucky strings - "ab" and "ba". Example 2 input: "aaab" output: 0 It's impossi…
def permutations(string)
permutations = string.split('').permutation.map(&:join).uniq
end
def lucky_strings(string)
permutations(string).select{|s| s.squeeze == s}.count
end
lucky_strings('ab') #=> 2
lucky_strings('aaab') #=> 0
lucky_strings('aabbbaa') #=> 1
lucky_strings('abcdefghij') #=> 3628800
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment