Dada una string str que contiene solo los caracteres x e y , la tarea es realizar las siguientes operaciones mientras sea posible:
encontrar un índice tal que s[i] = ‘x’ y s[i+1] = ‘y’ y eliminar ambos caracteres s[i] y s[i+1] , si no se encuentra dicho índice, busque un índice tal que s[i] = ‘y’ y s[i+1] = ‘x’ y swap(s[i ], s[i+1]) .
Imprime la string final después de realizar la operación dada.
Ejemplos:
Entrada: str = “xyyxx”
Salida: x
Paso 1: yxx (xy se eliminó)
Paso 2: xyx (yx se intercambió)
Paso 3: x (xy se eliminó)
Entrada: str = “xxyyxyy”
Salida: y
Enfoque: en la string final habrá solo x o solo y porque si tenemos tanto x como y en la string, habría un punto en el que tendríamos xy o yx como substring.
- Si es xy , lo eliminamos de inmediato.
- Si es yx , lo invertimos para obtener xy y luego lo eliminamos.
Dado que en cada paso de eliminación, se eliminan una x y una y , la string final tendría un número mínimo (x, y) de caracteres x e y eliminados.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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
<?php // PHP implementation of the approach // Function to return the modified string function printFinalString($s) { $n = strlen($s); $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++; } $finalString = (string)null; // 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 $s = "xxyyxyy"; echo printFinalString($s); // This code is contributed by ihritik ?>
Javascript
<script> // Javascript implementation of the approach // Function to return the modified string function printFinalString(s) { var i, n; n = s.length; var 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++; } var 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 var s = "xxyyxyy"; document.write( printFinalString(s)); // This code is contributed by famously. </script>
y
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA