Dada una string de palabras separadas por espacios. La tarea es encontrar la primera palabra par de longitud máxima de la string. Ej.: “Te dan una array de n números” La respuesta sería “una” y no “de” porque “una” viene antes de “de”.
Ejemplos:
Input: "this is a test string" Output: string Even length words are this, is, test, string. Even maximum length word is string. Input: "geeksforgeeks is a platform for geeks" Output: platform Only even length word is platform.
Enfoque: La idea es recorrer la string de entrada y encontrar la longitud de cada palabra. Compruebe si la longitud de la palabra es par o no. Si es par, compare la longitud con la longitud máxima encontrada hasta ahora. Si la longitud es estrictamente mayor que la longitud máxima, almacene la palabra actual como string requerida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find maximum length even word #include <bits/stdc++.h> using namespace std; // Function to find maximum length even word string findMaxLenEven(string str) { int n = str.length(); int i = 0; // To store length of current word. int currlen = 0; // To store length of maximum length word. int maxlen = 0; // To store starting index of maximum // length word. int st = -1; while (i < n) { // If current character is space then // word has ended. Check if it is even // length word or not. If yes then // compare length with maximum length // found so far. if (str[i] == ' ') { if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // Set currlen to zero for next word. currlen = 0; } else { // Update length of current word. currlen++; } i++; } // Check length of last word. if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // If no even length word is present // then return -1. if (st == -1) return "-1"; return str.substr(st, maxlen); } // Driver code int main() { string str = "this is a test string"; cout << findMaxLenEven(str); return 0; }
Java
// Java program to find maximum length even word class GFG { // Function to find maximum length even word static String findMaxLenEven(String str) { int n = str.length(); int i = 0; // To store length of current word. int currlen = 0; // To store length of maximum length word. int maxlen = 0; // To store starting index of maximum // length word. int st = -1; while (i < n) { // If current character is space then // word has ended. Check if it is even // length word or not. If yes then // compare length with maximum length // found so far. if (str.charAt(i) == ' ') { if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // Set currlen to zero for next word. currlen = 0; } else { // Update length of current word. currlen++; } i++; } // Check length of last word. if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // If no even length word is present // then return -1. if (st == -1) return "-1"; return str.substring(st, st + maxlen); } // Driver code public static void main(String args[]) { String str = "this is a test string"; System.out.println( findMaxLenEven(str)); } } // This code is contributed by Arnab Kundu
Python 3
# Python3 program to find maximum # length even word # Function to find maximum length # even word def findMaxLenEven(str): n = len(str) i = 0 # To store length of current word. currlen = 0 # To store length of maximum length word. maxlen = 0 # To store starting index of maximum # length word. st = -1 while (i < n): # If current character is space then # word has ended. Check if it is even # length word or not. If yes then # compare length with maximum length # found so far. if (str[i] == ' '): if (currlen % 2 == 0): if (maxlen < currlen): maxlen = currlen st = i - currlen # Set currlen to zero for next word. currlen = 0 else : # Update length of current word. currlen += 1 i += 1 # Check length of last word. if (currlen % 2 == 0): if (maxlen < currlen): maxlen = currlen st = i - currlen # If no even length word is present # then return -1. if (st == -1): print("trie") return "-1" return str[st: st + maxlen] # Driver code if __name__ == "__main__": str = "this is a test string" print(findMaxLenEven(str)) # This code is contributed by Ita_c
C#
// C# program to find maximum length even word using System; class GFG { // Function to find maximum length even word static String findMaxLenEven(string str) { int n = str.Length; int i = 0; // To store length of current word. int currlen = 0; // To store length of maximum length word. int maxlen = 0; // To store starting index of maximum // length word. int st = -1; while (i < n) { // If current character is space then // word has ended. Check if it is even // length word or not. If yes then // compare length with maximum length // found so far. if (str[i] == ' ') { if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // Set currlen to zero for next word. currlen = 0; } else { // Update length of current word. currlen++; } i++; } // Check length of last word. if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // If no even length word is present // then return -1. if (st == -1) return "-1"; return str.Substring(st, maxlen); } // Driver code public static void Main() { string str = "this is a test string"; Console.WriteLine(findMaxLenEven(str)); } // This code is contributed by Ryuga }
Javascript
<script> // Javascript program to find maximum length even word // Function to find maximum length even word function findMaxLenEven(str) { var n = str.length; var i = 0; // To store length of current word. var currlen = 0; // To store length of maximum length word. var maxlen = 0; // To store starting index of maximum // length word. var st = -1; while (i < n) { // If current character is space then // word has ended. Check if it is even // length word or not. If yes then // compare length with maximum length // found so far. if (str[i] == ' ') { if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // Set currlen to zero for next word. currlen = 0; } else { // Update length of current word. currlen++; } i++; } // Check length of last word. if (currlen % 2 == 0) { if (maxlen < currlen) { maxlen = currlen; st = i - currlen; } } // If no even length word is present // then return -1. if (st == -1) return "-1"; return str.substr(st, maxlen); } // Driver code var str = "this is a test string"; document.write( findMaxLenEven(str)); // This code is contributed by noob2000. </script>
Producción:
string
Complejidad de tiempo: O(N), donde N es la longitud de la string.
Espacio Auxiliar: O(1)