Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
| 1203 | Sort Items by Groups Respecting Dependencies | [Ruby](./algorithms/ruby/1203-sort-items-by-groups-respecting-dependencies.rb) | Hard |
| 1207 | Unique Number of Occurrences | [Ruby](./algorithms/ruby/1207-unique-number-of-occurrences.rb) | Easy |
| 1218 | Longest Arithmetic Subsequence of Given Difference | [Ruby](./algorithms/ruby/1218-longest-arithmetic-subsequence-of-given-difference.rb) | Medium |
| 1220 | Count Vowels Permutation | [Ruby](./algorithms/ruby/1220-count-vowels-permutation.rb) | Hard |
| 1232 | Check If It Is a Straight Line | [Ruby](./algorithms/ruby/1232-check-if-it-is-a-straight-line.rb) | Easy |
| 1254 | Number of Closed Islands | [Ruby](./algorithms/ruby/1254-number-of-closed-islands.rb) | Medium |
| 1268 | Search Suggestions System | [Ruby](./algorithms/ruby/1268-search-suggestions-system.rb) | Medium |
Expand Down
57 changes: 57 additions & 0 deletions algorithms/ruby/1220-count-vowels-permutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

# 1220. Count Vowels Permutation
# Hard
# https://leetcode.com/problems/count-vowels-permutation

=begin
Given an integer n, your task is to count how many strings of length n can be formed under the following rules:

Each character is a lower case vowel ('a', 'e', 'i', 'o', 'u')
Each vowel 'a' may only be followed by an 'e'.
Each vowel 'e' may only be followed by an 'a' or an 'i'.
Each vowel 'i' may not be followed by another 'i'.
Each vowel 'o' may only be followed by an 'i' or a 'u'.
Each vowel 'u' may only be followed by an 'a'.
Since the answer may be too large, return it modulo 10^9 + 7.

Example 1:
Input: n = 1
Output: 5
Explanation: All possible strings are: "a", "e", "i" , "o" and "u".

Example 2:
Input: n = 2
Output: 10
Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".

Example 3:
Input: n = 5
Output: 68

Constraints:
1 <= n <= 2 * 10^4
=end

# @param {Integer} n
# @return {Integer}
def count_vowel_permutation(n)
a = e = i = o = u = 1
(2..n).each do
a, e, i, o, u = e + i + u, a + i, e + o, i, i + o
end
(a + e + i + o + u) % 1000000007
end

# **************** #
# TEST #
# **************** #

require "test/unit"
class Test_count_vowel_permutation < Test::Unit::TestCase
def test_
assert_equal 5, count_vowel_permutation(1)
assert_equal 10, count_vowel_permutation(2)
assert_equal 68, count_vowel_permutation(5)
end
end