Dados dos caracteres especiales, el primer carácter se puede representar con un bit que es 0 y el segundo carácter se puede representar con dos bits, ya sea 10 u 11 . Ahora dada una string representada por varios bits. La tarea es devolver el número de caracteres que representa. Tenga en cuenta que la string dada siempre es válida.
Ejemplos:
Entrada: str = “11100”
Salida: 3
“11”, “10” y “0” son los caracteres requeridos.
Entrada: str = “100”
Salida: 2
Enfoque: el enfoque para resolver el problema es que si el carácter actual es 0, entonces representa un solo carácter de 1 bit, pero si el carácter actual es 1, entonces el siguiente bit debe incluirse en el carácter que consta de dos bits como no hay caracteres de un solo bit que comiencen con 1.
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 count // of required characters int countChars(string str, int n) { int i = 0, cnt = 0; // While there are characters left while (i < n) { // Single bit character if (str[i] == '0') i++; // Two-bit character else i += 2; // Update the count cnt++; } return cnt; } // Driver code int main() { string str = "11010"; int n = str.length(); cout << countChars(str, n); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to return the count // of required characters static int countChars(String str, int n) { int i = 0, cnt = 0; // While there are characters left while (i < n) { // Single bit character if (str.charAt(i) == '0') i += 1; // Two-bit character else i += 2; // Update the count cnt += 1; } return cnt; } // Driver code public static void main (String[] args) { String str = "11010"; int n = str.length(); System.out.println(countChars(str, n)); } // This code is contributed by AnkitRai01 }
Python3
# Python3 implementation of the approach # Function to return the count # of required characters def countChars(string, n) : i = 0; cnt = 0; # While there are characters left while (i < n) : # Single bit character if (string[i] == '0'): i += 1; # Two-bit character else : i += 2; # Update the count cnt += 1; return cnt; # Driver code if __name__ == "__main__" : string = "11010"; n = len(string); print(countChars(string, n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the above approach using System; class GFG { // Function to return the count // of required characters static int countChars(string str, int n) { int i = 0, cnt = 0; // While there are characters left while (i < n) { // Single bit character if (str[i] == '0') i += 1; // Two-bit character else i += 2; // Update the count cnt += 1; } return cnt; } // Driver code public static void Main () { string str = "11010"; int n = str.Length; Console.WriteLine(countChars(str, n)); } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript implementation of the above approach // Function to return the count // of required characters function countChars(str, n) { let i = 0, cnt = 0; // While there are characters left while (i < n) { // Single bit character if (str[i] == '0') i += 1; // Two-bit character else i += 2; // Update the count cnt += 1; } return cnt; } let str = "11010"; let n = str.length; document.write(countChars(str, n)); </script>
3
Publicación traducida automáticamente
Artículo escrito por vaishalichauhan3003 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA