Dada una string str de longitud N . La tarea es imprimir la frecuencia de los caracteres repetidos adyacentes.
Ejemplos:
Entrada: str = “Hola”
Salida: l: 2
Explicación: El carácter repetido consecutivo de la string dada es “l” y su frecuencia es 2.Entrada: str = «Hellolllee»
Salida: l: 2
l: 3
e: 2
Explicación: Los caracteres repetidos consecutivos de la string dada son «l», «l» y «e»
y sus frecuencias son las siguientes: 2, 3 , 2.
Enfoque: este problema se puede resolver simplemente recorriendo y siguiendo la pista de los caracteres repetidos adyacentes. Siga los pasos a continuación para resolver el problema dado.
- Iterar desde i = 0 hasta la longitud de la string.
- Mantener un contador.
- Vuelva a iterar a través del siguiente ciclo desde i+1 hasta la longitud de la string.
- El contador se incrementará hasta que el siguiente carácter sea diferente.
- Para caracteres que tengan más de 2 frecuencias, incremente i para que el conteo permanezca intacto.
- Si el contador es mayor que 1 , imprima.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for above approach #include <iostream> using namespace std; // Function to find frequency // of repeating characters void concesStr(string str) { // Length of string int lenStr = str.length(); // Iterate through 1st pointer for (int i = 0; i < lenStr; i++) { // Keep a counter int curr_count = 1; // Iterate through 2nd pointer for (int j = i + 1; j < lenStr; j++) { // if next element is different // then break if (str[i] != str[j]) { break; } curr_count++; // Example: if count is 3 // then move the first // pointer so that // count remains intact if (curr_count > 2) { i++; } } // Condition for print more than 1 // count characters if (curr_count > 1) { cout << str[i] << ": " << curr_count << endl; } } } // Driver Code int main() { string str = "Hellolllee"; concesStr(str); return 0; }
Java
// Java code to implement above approach import java.io.*; class GFG { // Function to find frequency // of repeating characters public static void consecStr(String str) { int lenStr = str.length(); // Iterate through 1st pointer for (int i = 0; i < lenStr; i++) { // keep a counter int curr_count = 1; // Iterate through 2nd pointer for (int j = i + 1; j < lenStr; j++) { // if next element is different // then break if (str.charAt(i) != str.charAt(j)) { break; } curr_count++; // Example: if count is 3 then // move the first pointer // so that count remains intact if (curr_count > 2) { i++; } } // Condition for print // more than 1 count characters if (curr_count > 1) { System.out.print(str.charAt(i) + ": " + curr_count + "\n"); } } } // Driver code public static void main(String[] args) { consecStr("Hellolllee"); } }
Python3
# Python code to implement above approach # Function to find frequency # of repeating characters def consecStr(str): lenStr = len(str); i = 0; # Iterate through 1st pointer for k in range(lenStr): # keep a counter curr_count = 1; # Iterate through 2nd pointer for j in range(i+1,lenStr): # if next element is different # then break if (str[i] != str[j]): break; curr_count += 1; # Example: if count is 3 then # move the first pointer # so that count remains intact if (curr_count > 2): i += 1; # Condition for print # more than 1 count characters if (curr_count > 1): print(str[i] , ": " , curr_count , ""); i += 1; # Driver code if __name__ == '__main__': consecStr("Hellolllee"); # This code is contributed by 29AjayKumar
C#
// C# program for above approach using System; class GFG { // Function to find frequency // of repeating characters static void concesStr(string str) { // Length of string int lenStr = str.Length; // Iterate through 1st pointer for (int i = 0; i < lenStr; i++) { // Keep a counter int curr_count = 1; // Iterate through 2nd pointer for (int j = i + 1; j < lenStr; j++) { // if next element is different // then break if (str[i] != str[j]) { break; } curr_count++; // Example: if count is 3 // then move the first // pointer so that // count remains intact if (curr_count > 2) { i++; } } // Condition for print more than 1 // count characters if (curr_count > 1) { Console.WriteLine(str[i] + ": " + curr_count); } } } // Driver Code public static void Main() { string str = "Hellolllee"; concesStr(str); } } // This code is contributed by gfgking.
Javascript
<script> // JavaScript code for the above approach // Function to find frequency // of repeating characters function concesStr(str) { // Length of string let lenStr = str.length; // Iterate through 1st pointer for (let i = 0; i < lenStr; i++) { // Keep a counter let curr_count = 1; // Iterate through 2nd pointer for (let j = i + 1; j < lenStr; j++) { // if next element is different // then break if (str[i] != str[j]) { break; } curr_count++; // Example: if count is 3 // then move the first // pointer so that // count remains intact if (curr_count > 2) { i++; } } // Condition for print more than 1 // count characters if (curr_count > 1) { document.write(str[i] + ": " + curr_count + '<br>'); } } } // Driver Code let str = "Hellolllee"; concesStr(str); // This code is contributed by Potta Lokesh </script>
l: 2 l: 3 e: 2
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por sourabhnaikssj y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA