执行给定操作后的最终字符串
给定一个仅包含字符x和y的字符串str ,任务是尽可能执行以下操作:
找到一个索引,使得s[i] = 'x'和s[i+1] = 'y'并删除两个字符s[i]和s[i+1] ,如果没有找到这样的索引,则找到一个索引使得s[i] = 'y'和s[i+1] = 'x'和swap(s[i], s[i+1]) 。
执行给定操作后打印最终字符串。
例子:
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
方法:在最后的字符串中,要么只有x ,要么只有y ,因为如果我们在字符串中同时有x和y ,那么就会有一个点,我们有xy或yx作为子字符串。
- 如果它是xy ,我们立即将其删除。
- 如果是yx ,我们将其反转以获取xy ,然后将其删除。
由于在每个删除步骤中,一个x和一个y被删除,因此最终的字符串将删除min(x, y)个x和y字符。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the modified string
string printFinalString(string s)
{
int i, n;
n = s.length();
int x = 0, y = 0;
for (i = 0; i < n; i++) {
// Count number of 'x'
if (s[i] == 'x')
x++;
// Count number of 'y'
else
y++;
}
string finalString = "";
// min(x, y) number of 'x' and 'y' will be deleted
if (x > y)
for (i = 0; i < x - y; i++)
finalString += "x";
else
for (i = 0; i < y - x; i++)
finalString += "y";
return finalString;
}
// Driver Program to test above function
int main()
{
string s = "xxyyxyy";
cout << printFinalString(s);
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the modified String
static String printFinalString(String s)
{
int i, n;
n = s.length();
int x = 0, y = 0;
for (i = 0; i < n; i++)
{
// Count number of 'x'
if (s.charAt(i) == 'x')
{
x++;
} // Count number of 'y'
else
{
y++;
}
}
String finalString = "";
// min(x, y) number of 'x' and
// 'y' will be deleted
if (x > y)
{
for (i = 0; i < x - y; i++)
{
finalString += "x";
}
}
else
{
for (i = 0; i < y - x; i++)
{
finalString += "y";
}
}
return finalString;
}
// Driver Code
public static void main(String args[])
{
String s = "xxyyxyy";
System.out.println(printFinalString(s));
}
}
// This code is contributed
// by 29AjayKumar
Python3
# Python 3 implementation of the approach
# Function to return the modified string
def prFinalString(s):
i, n = 0, 0
n = len(s)
x, y = 0, 0
for i in range(0, n):
# Count number of 'x'
if (s[i] == 'x'):
x += 1
# Count number of 'y'
else:
y += 1
finalString = ""
# min(x, y) number of 'x' and
# 'y' will be deleted
if (x > y):
for i in range(0, x - y):
finalString += "x"
else:
for i in range(0, y - x):
finalString += "y"
return finalString
# Driver Code
if __name__ == '__main__':
s = "xxyyxyy"
print(prFinalString(s))
# This code contributed by 29AjayKumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the modified String
static string printFinalString(string s)
{
int i, n;
n = s.Length;
int x = 0, y = 0;
for (i = 0; i < n; i++)
{
// Count number of 'x'
if (s[i] == 'x')
{
x++;
} // Count number of 'y'
else
{
y++;
}
}
string finalString = "";
// min(x, y) number of 'x' and
// 'y' will be deleted
if (x > y)
{
for (i = 0; i < x - y; i++)
{
finalString += "x";
}
}
else
{
for (i = 0; i < y - x; i++)
{
finalString += "y";
}
}
return finalString;
}
// Driver Code
public static void Main()
{
string s = "xxyyxyy";
Console.WriteLine(printFinalString(s));
}
}
// This code is contributed
// by Akanksha Rai
PHP
$y)
for ($i = 0; $i < $x - $y; $i++)
$finalString .= "x";
else
for ($i = 0; $i < $y - $x; $i++)
$finalString .= "y";
return $finalString;
}
// Driver Code
$s = "xxyyxyy";
echo printFinalString($s);
// This code is contributed by ihritik
?>
Javascript
输出:
y