
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Arrangement of words without changing the relative position of vowel and consonants?
Suppose we have a string with n elements (n < 10). We have to find the number of ways that the string can be arranged without changing the relative position of the vowel and consonants.
The approach is simple. We have to count the number of vowels and consonants in the given string, then we have to find how many ways we can arrange the vowels only, then find the number of ways to arrange the consonant only, after that multiply these two results to get the total ways.
Algorithm
arrangeWayCount(str)
Begin define an array ‘freq’ to store frequency. count and place frequency of each characters in freq array. such that freq[‘0’] will hold frequency of letter ‘a’, freq[1] will hold frequency of ‘b’ and so on. v := number of vowels, and c := number of consonants in str vArrange := factorial of v for each vowel v in [a, e, i, o, u], do vArrange := vArrange / factorial of the frequency of v done cArrange := factorial of c for each consonant con, do cArrange := cArrange / factorial of the frequency of con done return vArrange * cArrange End
Example
#include <iostream> using namespace std; long long factorial(int n){ if(n == 0 || n == 1) return 1; return n*factorial(n-1); } long long arrangeWayCount(string str){ long long freq[27] = {0}; //fill frequency array to 0 int v = 0, c = 0; for (int i = 0; i < str.length(); i++) { freq[str[i] - 'a']++; if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') { v++; }else c++; } long long arrangeVowel; arrangeVowel = factorial(v); arrangeVowel /= factorial(freq[0]); // vowel a arrangeVowel /= factorial(freq[4]); // vowel e arrangeVowel /= factorial(freq[8]); // vowel i arrangeVowel /= factorial(freq[14]); // vowel o arrangeVowel /= factorial(freq[20]); // vowel u long long arrangeConsonant; arrangeConsonant = factorial(c); for (int i = 0; i < 26; i++) { if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20) arrangeConsonant /= factorial(freq[i]); //frequency of all characters except vowels } long long total = arrangeVowel * arrangeConsonant; return total; } main() { string str = "computer"; long long ans = arrangeWayCount(str); cout << "Possible ways to arrange: " << ans << endl; }
Output
Possible ways to arrange: 720
- Related Questions & Answers
- Counting occurrences of vowel, consonants - JavaScript
- Reversing array without changing the position of certain elements JavaScript
- Changing the Position of List Markers using CSS
- Changing the Position of List Markers in CSS
- CSS position: relative;
- Frequency of vowels and consonants in JavaScript
- How to return the position of the element relative to floating objects in JavaScript?
- How to return the position of a document relative to the collection in MongoDB?
- Beautiful Arrangement of Numbers in JavaScript
- How to set the position of a Tkinter window without setting the dimensions?
- Changing Matplotlib subplot size/position after axes creation
- Find the arrangement of queue at given time in C++
- How to change the column position of MySQL table without losing column data?
- Maximum height of triangular arrangement of array values in C++
- Industry 4.0 the changing face of manufacturing
Advertisements