
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count sorted vowel strings in Python
Suppose we have a number n, we have to find the number of strings of size n that consist only of vowels (a, e, i, o, u) and they are lexicographically sorted. We can say that a string s is lexicographically sorted when for all valid index i, s[i] is the same as or comes before s[i+1] in the alphabet.
So, if the input is like n = 2, then the output will be 15 because there are many strings like ["aa", "ae", "ai", "ao", "au", "ee", "ei", "eo", "eu", "ii", "io", "iu", "oo", "ou", "uu"].
To solve this, we will follow these steps −
- if n is same as 1, then
- return 5
- count := an array of size 6, and initially filled with 1
- for i in range 3 to n, do
- count[1] := count[1]+count[2]+count[3]+count[4]+count[5]
- count[2] := count[2]+count[3]+count[4]+count[5]
- count[3] := count[3]+count[4]+count[5]
- count[4] := count[4]+count[5]
- total := 0
- for i in range 1 to 5, do
- total := total + i*count[i]
- return total
Example
Let us see the following implementation to get better understanding −
def solve(n): if n==1: return 5 count = [1 for i in range(6)] for i in range(3,n+1): count[1] = count[1]+count[2]+count[3]+count[4]+count[5] count[2] = count[2]+count[3]+count[4]+count[5] count[3] = count[3]+count[4]+count[5] count[4] = count[4]+count[5] total = 0 for i in range(1,6): total += i*count[i] return total n = 2 print(solve(n))
Input
2
Output
15
- Related Articles
- Python program to Sort Strings by Punctuation count
- Python – Extract Sorted Strings
- Program to count the number of consistent strings in Python
- Python program to count the pairs of reverse strings
- Python Program to accept string starting with vowel
- Python Program to Count number of binary strings without consecutive 1’
- Program to count number of strings we can make using grammar rules in Python
- Javascript Program to Count 1’s in a sorted binary array
- Program to merge two sorted list to form larger sorted list in Python
- Program to merge K-sorted lists in Python
- Program to find minimum deletions to make strings strings in Python
- Python program to check if the given string is vowel Palindrome
- Python Program to Random uppercase in Strings
- Program to find length of longest substring with even vowel counts in Python
- Reverse alphabetically sorted strings in JavaScript

Advertisements