Created
December 18, 2015 17:42
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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