Modify the string by swapping continuous vowels or consonants
Given a string str. The task is to modify the string by swapping two adjacent characters if both of them are vowels or both of them are consonants.
Examples:
Input: str = “geeksforgeeks”
Output: geesfkogreesk
The alphabets ‘e’ and ‘e’ in geeksforgeeks are vowels so they are swapped so the string becomes geeksforgeeks.
The alphabets ‘k’ and ‘s’ in geeksforgeeks are consonants so they are swapped so the string becomes geeskforgeeks.
The alphabets ‘k’ and ‘f’ in geeskforgeeks are consonants so they are swapped so the string becomes geesfkorgeeks.
The alphabets ‘r’ and ‘g’ in geesfkorgeeks are consonants so they are swapped so the string becomes geeskfogreeks.
The alphabets ‘e’ and ‘e’ in geeskfogreeks are vowels so they are swapped so the string becomes geeskfogreeks.
The alphabets ‘k’ and ‘s’ in geeskfogreeks are vowels so they are swapped so the string becomes geeskfogreesk.
Input:str = “gefeg”
Output: gefeg
No continuous vowels or consonants.
Approach:
- Traverse through the characters in the string.
- Consider the current character and the next character.
- If both the characters are consonants or both the characters are vowels.
- Then swap the characters.
- Else continue the process till the end of the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
c = tolower (c);
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
return true ;
return false ;
}
string swapRepeated(string str)
{
for ( int i = 0; i < str.length() - 1; i++) {
if ((isVowel(str[i]) && isVowel(str[i + 1]))
|| (!isVowel(str[i]) && !isVowel(str[i + 1])))
swap(str[i], str[i + 1]);
}
return str;
}
int main()
{
string str = "geeksforgeeks" ;
cout << swapRepeated(str);
return 0;
}
|
Java
class GFG
{
static boolean isVowel( char c)
{
c = Character.toLowerCase(c);
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
{
return true ;
}
return false ;
}
static String swapRepeated( char str[])
{
for ( int i = 0 ; i < str.length - 1 ; i++)
{
char c = 0 ;
if ((isVowel(str[i]) && isVowel(str[i + 1 ]))
|| (!isVowel(str[i]) && !isVowel(str[i + 1 ])))
{
c = str[i];
str[i] = str[i + 1 ];
str[i + 1 ] = c;
}
}
return String.valueOf(str);
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.println(swapRepeated(str.toCharArray()));
}
}
|
Python 3
def isVowel(c) :
c = c.lower();
if (c = = 'a' or c = = 'e' or c = = 'i'
or c = = 'o' or c = = 'u' ) :
return True ;
return False ;
def swapRepeated(string) :
for i in range ( len (string) - 1 ) :
if ((isVowel(string[i]) and isVowel(string[i + 1 ])) or
( not (isVowel(string[i])) and not (isVowel(string[i + 1 ])))) :
(string[i],
string[i + 1 ]) = (string[i + 1 ],
string[i]);
string = "".join(string)
return string;
if __name__ = = "__main__" :
string = "geeksforgeeks" ;
print (swapRepeated( list (string)));
|
C#
using System;
class GFG
{
static bool isVowel( char c)
{
c = char .ToLower(c);
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
{
return true ;
}
return false ;
}
static String swapRepeated( char []str)
{
for ( int i = 0; i < str.Length - 1; i++)
{
char c = ( char )0;
if ((isVowel(str[i]) && isVowel(str[i + 1]))
|| (!isVowel(str[i]) && !isVowel(str[i + 1])))
{
c = str[i];
str[i] = str[i + 1];
str[i + 1] = c;
}
}
return String.Join( "" ,str);
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
Console.WriteLine(swapRepeated(str.ToCharArray()));
}
}
|
PHP
<?php
function isVowel( $c )
{
$c = strtolower ( $c );
if ( $c == 'a' || $c == 'e' || $c == 'i'
|| $c == 'o' || $c == 'u' )
return true;
return false;
}
function swapRepeated( $str )
{
for ( $i = 0; $i < strlen ( $str ) - 1; $i ++) {
if ((isVowel( $str [ $i ]) && isVowel( $str [ $i + 1]))
|| (!isVowel( $str [ $i ]) && !isVowel( $str [ $i + 1])))
{
$t = $str [ $i ];
$str [ $i ]= $str [ $i + 1];
$str [ $i +1] = $t ;
}
}
return $str ;
}
$str = "geeksforgeeks" ;
echo swapRepeated( $str );
return 0;
?>
|
Javascript
<script>
function isVowel(c) {
c = c.toLowerCase();
if (c === "a" || c === "e" || c === "i" || c === "o" || c === "u" ) {
return true ;
}
return false ;
}
function swapRepeated(str) {
for ( var i = 0; i < str.length - 1; i++) {
if (
(isVowel(str[i]) && isVowel(str[i + 1])) ||
(!isVowel(str[i]) && !isVowel(str[i + 1]))
) {
var c = str[i];
str[i] = str[i + 1];
str[i + 1] = c;
}
}
return str.join( "" );
}
var str = "geeksforgeeks" ;
document.write(swapRepeated(str.split( "" )));
</script>
|
Complexity Analysis:
- Time Complexity: O(N) for traversing the given string str.
- Auxiliary Space: O(1) as constant space is used.
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...