Dada la string binaria str , la tarea es encontrar el recuento de substrings que no se superponen de la forma «010» o «101» .
Ejemplos:
Entrada: str = “10101010101”
Salida: 3
str[0..2] = “101”
str[3..5] = “010”
str[6..8] = “101”
Entrada: str = “111111111111110”
Salida: 0
Enfoque: Inicialice el conteo = 0 y para cada índice i en la string dada verifique si la substring de tamaño 3 que comienza en el índice actual i coincide con «010» o «101» . Si es una coincidencia, actualice count = count + 1 e i = i + 3 (para evitar la superposición de substrings), de lo contrario, incremente i en 1 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the count of // required non-overlapping sub-strings int countSubStr(string s, int n) { // To store the required count int count = 0; for (int i = 0; i < n - 2;) { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code int main() { string s = "10101010101"; int n = s.length(); cout << countSubStr(s, n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the count of // required non-overlapping sub-strings static int countSubStr(char[] s, int n) { // To store the required count int count = 0; for (int i = 0; i < n - 2😉 { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code public static void main(String[] args) { char[] s = "10101010101".toCharArray(); int n = s.length; System.out.println(countSubStr(s, n)); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach # Function to return the count of # required non-overlapping sub-strings def countSubStr(s, n) : # To store the required count count = 0; i = 0 while i < (n-2) : # If "010" matches the sub-string # starting at current index i if (s[i] == '0' and s[i + 1] == '1'and s[i + 2] == '0') : count += 1; i += 3; # If "101" matches the sub-string # starting at current index i elif (s[i] == '1' and s[i + 1] == '0'and s[i + 2] == '1') : count += 1; i += 3; else : i += 1; return count; # Driver code if __name__ == "__main__" : s = "10101010101"; n = len(s); print(countSubStr(s, n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to return the count of // required non-overlapping sub-strings static int countSubStr(char[] s, int n) { // To store the required count int count = 0; for (int i = 0; i < n - 2;) { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code public static void Main(String[] args) { char[] s = "10101010101".ToCharArray(); int n = s.Length; Console.WriteLine(countSubStr(s, n)); } } // This code is contributed by Rajput-Ji
Javascript
<script> // javascript implementation of the approach // Function to return the count of // required non-overlapping sub-strings function countSubStr( s , n) { // To store the required count var count = 0; for (i = 0; i < n - 2;) { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code var s = "10101010101"; var n = s.length; document.write(countSubStr(s, n)); // This code contributed by Rajput-Ji </script>
3
Complejidad de tiempo: O(n), donde n es la longitud de la string.
Espacio Auxiliar: O(n)
Publicación traducida automáticamente
Artículo escrito por ayushgoyal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA