Remove Vowels

Easy
0/40
Average time to solve is 10m
28 upvotes
Asked in companies
OptumPractoCGI

Problem statement

You are given a string STR of length N. Your task is to remove all the vowels present in that string and print the modified string.

English alphabets ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are termed as vowels. All other alphabets are called consonants.

Note: You have to only remove vowels from the string. There will be no change in the relative position of all other alphabets.

For example:
(i)  If the input string is 'CodeGeek', the output should be CdGk after removing ‘o’ and ‘e’.

(ii) If the input string is 'Odinson', the output should be 'dnsn' after removing ‘o’ and ‘i’. 
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.

The first and only line of each test case contains a string STR of length N.
Output Format:
For each test case, return the modified string that contains ‘NO VOWELS’.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10^4

STR may contain alphabets from 'a' to 'z' or 'A' to 'Z' and blank spaces.

Time Limit: 1 sec 
Sample Input 1:
2
Mobile
CodingNinjas
Sample Output 1:
Mbl
CdngNnjs
Explanation of Input 1:
(i) The output ‘Mbl’ is obtained after removing vowels ‘o’ and ‘i’ from second and fourth position respectively of given string ‘Mobile’.

(ii) The output ‘CdngNnjs’ is obtained after removing vowels ‘o’, ‘i’, ‘i’, and ‘a’ from second, fourth, eighth, and eleventh position respectively of given string ‘CodingNinjas’.
Sample Input 2:
3
B
aB
BcE
Sample Output 2
B
B
Bc
Hint

Try to find vowels in the string and replace them by the next alphabet of string.

Approaches (2)
Brute Force
  1. Iterate through the string STR from i = 0 to i = N-1 and check:
    1. If the current character is not vowel: Do nothing.
    2. Else
      1. Iterate from j = i+1 to N-2 and copy j th alphabet to j-1 th position
      2. Reduce the value of N by 1 as an alphabet is removed now.
Time Complexity

O(N^2), where N is the length of string STR.

 

Since here in the worst case, you will get all vowels in the string which you’ll have to replace each vowel by traversing the entire string N times.

Space Complexity

O(N), where N is the length of string STR.

 

Using an ‘N’ size array.

Code Solution
(100% EXP penalty)
Remove Vowels
Full screen
Console