- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rearrange a string to maximize the minimum distance between any pair of vowels
In this article, we are going to unravel an interesting problem from the domain of string manipulation: "Rearrange a string to maximize the minimum distance between any pair of vowels". This problem challenges us to manipulate the arrangement of characters in a string to ensure the maximum possible minimum distance between any two vowel characters. We'll discuss the problem in detail, providing the various programs.
Understanding the Problem Statement
Given a string, the task is to rearrange the characters in the string in such a way that the minimum distance between any pair of vowels is maximized. In other words, we want to place the vowels as far apart as possible from each other.
The vowels in the English language are 'a', 'e', 'i', 'o', 'u', and their uppercase versions.
Approach
To solve this problem, we'll take a two-step approach −
First, count the number of vowels in the string and store their positions in an array.
Next, sort this array and calculate the maximum difference between any two consecutive elements. This difference represents the maximum minimum distance between any pair of vowels.
Example
Let's implement this strategy in various programming languages −
// C program to rearrange a string to maximize the minimum distance between any pair of vowels #include <stdio.h> #include <stdbool.h> #include <string.h> // Function to check if a character is a vowel bool isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; } // Function to find maximum minimum distance between vowels int maxMinDist(char* s) { int len = strlen(s); int pos[len]; int count = 0; for (int i = 0; i < len; i++) { if (isVowel(s[i])) { pos[count++] = i; } } int maxDist = 0; for (int i = 1; i < count; i++) { int dist = pos[i] - pos[i - 1]; if (dist > maxDist) { maxDist = dist; } } return maxDist; } int main() { char s[] = "programming"; printf("Max minimum distance between vowels: %d\n", maxMinDist(s)); return 0; }
Output
Max minimum distance between vowels: 3
#include <bits/stdc++.h> using namespace std; // Function to check if a character is a vowel bool isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; } // Function to find maximum minimum distance between vowels int maxMinDist(string s) { vector<int> pos; for (int i = 0; i < s.size(); i++) { if (isVowel(s[i])) pos.push_back(i); } sort(pos.begin(), pos.end()); int maxDist = 0; for (int i = 1; i < pos.size(); i++) { maxDist = max(maxDist, pos[i] - pos[i-1]); } return maxDist; } int main() { string s = "programming"; cout << "Max minimum distance between vowels: " << maxMinDist(s); return 0; }
Output
Max minimum distance between vowels: 3
// Java program to rearrange a string to maximize the minimum distance between any pair of vowels import java.util.ArrayList; import java.util.List; public class Main { public static boolean isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; } public static int maxMinDist(String s) { List<Integer> pos = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { if (isVowel(s.charAt(i))) { pos.add(i); } } int maxDist = 0; for (int i = 1; i < pos.size(); i++) { int dist = pos.get(i) - pos.get(i - 1); if (dist > maxDist) { maxDist = dist; } } return maxDist; } public static void main(String[] args) { String s = "programming"; System.out.println("Max minimum distance between vowels: " + maxMinDist(s)); } }
Output
Max minimum distance between vowels: 3
# Python program to rearrange a string to maximize the minimum distance between any pair of vowels def is_vowel(c): return c in 'aeiouAEIOU' def max_min_dist(s): pos = [i for i in range(len(s)) if is_vowel(s[i])] max_dist = 0 for i in range(1, len(pos)): dist = pos[i] - pos[i - 1] if dist > max_dist: max_dist = dist return max_dist s = "programming" print("Max minimum distance between vowels:", max_min_dist(s))
Output
Max minimum distance between vowels: 3
This code first finds the positions of all vowels in the string and stores them in a vector. It then sorts this vector and finds the maximum difference between consecutive elements. This difference represents the maximum minimum distance between any pair of vowels.
Test Case
Let's consider the string "programming". The positions of the vowels 'o', 'a', and 'i' are 1, 4, and 7 respectively. Therefore, the maximum minimum distance between any pair of vowels is 3.
Conclusion
This article provided a step-by-step approach to solving the problem of maximizing the minimum distance between any pair of vowels in a given string. The solution involved counting the vowels, storing their positions, and then finding the maximum difference between these positions. Although the problem may seem complex at first glance, it simplifies considerably when broken down into these steps.