Dada una string S y un carácter K . La tarea es encontrar la longitud de la substring más larga de S que tenga todos los caracteres iguales al carácter K.
Ejemplos:
Entrada: S = «abcd1111aabc», K = ‘1’
Salida: 4
Explicación:
1111 es la substring más grande de longitud 4.Entrada: S = “#1234#@@abcd”, K = ‘@’
Salida: 2
Explicación:
@@ es la substring más grande de longitud 2.
Enfoque: la idea es iterar sobre la string y comprobar las dos condiciones siguientes:
- Si el carácter actual es el mismo que el carácter K, aumente el valor del contador en uno.
- Si el carácter actual no es el mismo que K, actualice el recuento anterior y reinicie el contador a 0.
- Repita los pasos anteriores hasta la longitud de la cuerda.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the length of // longest sub-string having all // characters same as character K int length_substring(string S, char K) { // Initialize variables int curr_cnt = 0, prev_cnt = 0, max_len; // Iterate till size of string for (int i = 0; i < S.size(); i++) { // Check if current character is K if (S[i] == K) { curr_cnt += 1; } else { prev_cnt = max(prev_cnt, curr_cnt); curr_cnt = 0; } } prev_cnt = max(prev_cnt, curr_cnt); // Assigning the max // value to max_len max_len = prev_cnt; return max_len; } // Driver code int main() { string S = "abcd1111aabc"; char K = '1'; // Function call cout << length_substring(S, K); return 0; }
Java
// Java program for // the above approach import java.util.*; class GFG { // Function to find the length of // longest sub-string having all // characters same as character K static int length_substring(String S, char K) { // Initialize variables int curr_cnt = 0, prev_cnt = 0, max_len; // Iterate till size of string for (int i = 0; i < S.length(); i++) { // Check if current character is K if (S.charAt(i) == K) { curr_cnt += 1; } else { prev_cnt = Math.max(prev_cnt, curr_cnt); curr_cnt = 0; } } prev_cnt = Math.max(prev_cnt, curr_cnt); // Assigning the max // value to max_len max_len = prev_cnt; return max_len; } // Driver code public static void main(String[] args) { String S = "abcd1111aabc"; char K = '1'; // Function call System.out.print(length_substring(S, K)); } } // This code is contributed by Chitranayal
Python3
# Python3 program for the above approach # Function to find the length of # longest sub-string having all # characters same as character K def length_substring(S, K): # Initialize variables curr_cnt = 0 prev_cnt = 0 max_len = 0 # Iterate till size of string for i in range(len(S)): # Check if current character is K if (S[i] == K): curr_cnt += 1 else: prev_cnt = max(prev_cnt, curr_cnt) curr_cnt = 0 prev_cnt = max(prev_cnt, curr_cnt) # Assigning the max # value to max_len max_len = prev_cnt return max_len # Driver code if __name__ == '__main__': S = "abcd1111aabc" K = '1' # Function call print(length_substring(S, K)) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; class GFG{ // Function to find the length of // longest sub-string having all // characters same as character K static int length_substring(string S, char K) { // Initialize variables int curr_cnt = 0, prev_cnt = 0, max_len; // Iterate till size of string for(int i = 0; i < S.Length; i++) { // Check if current character is K if (S[i] == K) { curr_cnt += 1; } else { prev_cnt = Math.Max(prev_cnt, curr_cnt); curr_cnt = 0; } } prev_cnt = Math.Max(prev_cnt, curr_cnt); // Assigning the max // value to max_len max_len = prev_cnt; return max_len; } // Driver code static public void Main() { string S = "abcd1111aabc"; char K = '1'; // Function call Console.WriteLine(length_substring(S, K)); } } // This code is contributed by rag2127
Javascript
<script> // Javascript program for // the above approach // Function to find the length of // longest sub-string having all // characters same as character K function length_substring(S, K) { // Initialize variables let curr_cnt = 0, prev_cnt = 0, max_len; // Iterate till size of string for(let i = 0; i < S.length; i++) { // Check if current character is K if (S[i] == K) { curr_cnt += 1; } else { prev_cnt = Math.max(prev_cnt, curr_cnt); curr_cnt = 0; } } prev_cnt = Math.max(prev_cnt, curr_cnt); // Assigning the max // value to max_len max_len = prev_cnt; return max_len; } // Driver code let S = "abcd1111aabc"; let K = '1'; // Function call document.write(length_substring(S, K)); // This code is contributed by avanitrachhadiya2155 </script>
Producción:
4
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por charlie_04 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA