Final string after performing given operations
Given a string str containing only characters x and y, the task is to perform the following operations while possible:
Find an index such that s[i] = ‘x’ and s[i+1] = ‘y’ and delete both the characters s[i] and s[i+1], if no such index is found then find an index such that s[i] = ‘y’ and s[i+1] = ‘x’ and swap(s[i], s[i+1]).
Print the final string after performing the given operation.
Examples:
Input: str = “xyyxx”
Output: x
Step 1: yxx (xy got deleted)
Step 2: xyx (yx got swapped)
Step 3: x (xy got deleted)
Input: str = “xxyyxyy”
Output: y
Approach: In the final string there will be either only x or only y because if we have both x and y in the string then there would be a point where we have either xy or yx as sub-string.
- If it’s xy, we delete it straight away.
- If it is yx, we reverse it to get xy and then delete it.
Since in each deletion step, one x and one y gets deleted, the final string would have min(x, y) number of x and y characters deleted.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string printFinalString(string s)
{
int i, n;
n = s.length();
int x = 0, y = 0;
for (i = 0; i < n; i++) {
if (s[i] == 'x' )
x++;
else
y++;
}
string finalString = "" ;
if (x > y)
for (i = 0; i < x - y; i++)
finalString += "x" ;
else
for (i = 0; i < y - x; i++)
finalString += "y" ;
return finalString;
}
int main()
{
string s = "xxyyxyy" ;
cout << printFinalString(s);
}
|
Java
class GFG
{
static String printFinalString(String s)
{
int i, n;
n = s.length();
int x = 0 , y = 0 ;
for (i = 0 ; i < n; i++)
{
if (s.charAt(i) == 'x' )
{
x++;
}
else
{
y++;
}
}
String finalString = "" ;
if (x > y)
{
for (i = 0 ; i < x - y; i++)
{
finalString += "x" ;
}
}
else
{
for (i = 0 ; i < y - x; i++)
{
finalString += "y" ;
}
}
return finalString;
}
public static void main(String args[])
{
String s = "xxyyxyy" ;
System.out.println(printFinalString(s));
}
}
|
Python3
def prFinalString(s):
i, n = 0 , 0
n = len (s)
x, y = 0 , 0
for i in range ( 0 , n):
if (s[i] = = 'x' ):
x + = 1
else :
y + = 1
finalString = ""
if (x > y):
for i in range ( 0 , x - y):
finalString + = "x"
else :
for i in range ( 0 , y - x):
finalString + = "y"
return finalString
if __name__ = = '__main__' :
s = "xxyyxyy"
print (prFinalString(s))
|
C#
using System;
class GFG
{
static string printFinalString( string s)
{
int i, n;
n = s.Length;
int x = 0, y = 0;
for (i = 0; i < n; i++)
{
if (s[i] == 'x' )
{
x++;
}
else
{
y++;
}
}
string finalString = "" ;
if (x > y)
{
for (i = 0; i < x - y; i++)
{
finalString += "x" ;
}
}
else
{
for (i = 0; i < y - x; i++)
{
finalString += "y" ;
}
}
return finalString;
}
public static void Main()
{
string s = "xxyyxyy" ;
Console.WriteLine(printFinalString(s));
}
}
|
PHP
<?php
function printFinalString( $s )
{
$n = strlen ( $s );
$x = 0;
$y = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] == 'x' )
$x ++;
else
$y ++;
}
$finalString = (string)null;
if ( $x > $y )
for ( $i = 0; $i < $x - $y ; $i ++)
$finalString .= "x" ;
else
for ( $i = 0; $i < $y - $x ; $i ++)
$finalString .= "y" ;
return $finalString ;
}
$s = "xxyyxyy" ;
echo printFinalString( $s );
?>
|
Javascript
<script>
function printFinalString(s)
{
var i, n;
n = s.length;
var x = 0, y = 0;
for (i = 0; i < n; i++) {
if (s[i] == 'x' )
x++;
else
y++;
}
var finalString = "" ;
if (x > y)
for (i = 0; i < x - y; i++)
finalString += "x" ;
else
for (i = 0; i < y - x; i++)
finalString += "y" ;
return finalString;
}
var s = "xxyyxyy" ;
document.write( printFinalString(s));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string.
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...