Dada una string str . La tarea es contar el número mínimo de elementos que se eliminarán para que los elementos consecutivos por pares sean iguales .
Ejemplos :
Entrada : str = “11344”
Salida : 1
Retire el dígito 3 del 3er lugar para que la string se convierta en 1144. Por lo tanto, dos elementos consecutivos en pares son iguales. Por lo tanto, la respuesta es 1.
Entrada : str = «55553»
Salida : 1
Retire el dígito 3 del quinto lugar para que la string se convierta en 5555. Por lo tanto, dos elementos consecutivos en pares son iguales. Por lo tanto la respuesta es 1.
Enfoque: compruebe si los dos elementos consecutivos actuales son iguales o no. En caso afirmativo, aumente el índice en 2 y siga comprobando hasta que se atraviesen todos los elementos. De lo contrario, incremente el índice en 1 y cuente en 1.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to count the minimum number of elements // to remove from a number so that pairwise two // consecutive digits are same. int countConsecutive(string s) { // initialize counting variable int count = 0; for (int i = 0; i < s.size(); i++) { // check if two consecutive digits are same if (s[i] == s[i + 1]) i++; else count++; } return count; } // Driver code int main() { string str = "44522255"; cout << countConsecutive(str); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to count the minimum number of elements // to remove from a number so that pairwise two // consecutive digits are same. static int countConsecutive(String s) { // initialize counting variable int count = 0; for (int i = 0; i < s.length(); i++) { // check if two consecutive digits are same if (s.charAt(i) == s.charAt(i + 1)) { i++; } else { count++; } } return count; } // Driver code public static void main(String args[]) { String str = "44522255"; System.out.println(countConsecutive(str)); } } // This code is contributed by PrinciRaj19992
Python3
# Python 3 implementation of the above approach # Function to count the minimum number of # elements to remove from a number so that # pairwise two consecutive digits are same. def countConsecutive(s): # initialize counting variable count = -1 for i in range(len(s)-1): # check if two consecutive # digits are same if(i <= len(s)): if (s[i] is s[i + 1]): i += 1 else: count += 1 return count # Driver code if __name__ == '__main__': str = "44522255" print(countConsecutive(str)) # This code is contributed by PrinciRaj1992
C#
// C# implementation of above approach using System; public class GFG { // Function to count the minimum number of elements // to remove from a number so that pairwise two // consecutive digits are same. static int countConsecutive(String s) { // initialize counting variable int count = 0; for (int i = 0; i < s.Length; i++) { // check if two consecutive digits are same if (s[i] == s[i+1]) { i++; } else { count++; } } return count; } // Driver code public static void Main() { String str = "44522255"; Console.WriteLine(countConsecutive(str)); } } // This code is contributed by 29AjayKumar
PHP
<?php // PHP implementation of the above approach // Function to count the minimum number // of elements to remove from a number so // that pairwise two consecutive digits are same. function countConsecutive($s) { // initialize counting variable $count = 0; for ($i = 0; $i < strlen($s); $i++) { // check if two consecutive // digits are same if ($s[$i] == $s[$i + 1]) $i++; else $count++; } return $count; } // Driver code $str = "44522255"; echo countConsecutive($str); // This code is contributed by Sachin. ?>
Javascript
<script> // Javascript implementation of the above approach // Function to count the minimum number of elements // to remove from a number so that pairwise two // consecutive digits are same. function countConsecutive(s) { // initialize counting variable let count = 0; for (let i = 0; i < s.length; i++) { // check if two consecutive digits are same if (s[i] == s[i + 1]) i++; else count++; } return count; } // Driver code let str = "44522255"; document.write(countConsecutive(str)); // This code is contributed by Surbhi Tyagi. </script>
2
Publicación traducida automáticamente
Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA