从字符串中查找第一个最大长度偶数单词
给定字符串由空格分隔的单词。任务是从字符串中找到第一个最大长度偶数单词。例如:“给你一个包含 n 个数字的数组”答案是“an”而不是“of”,因为“an”在“of”之前。
例子:
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.
方法:想法是遍历输入字符串并找到每个单词的长度。检查单词的长度是否均匀。如果是偶数,则将长度与迄今为止找到的最大长度进行比较。如果长度严格大于最大长度,则将当前单词存储为所需的字符串。
以下是上述方法的实现:
C++
// C++ program to find maximum length even word
#include
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
输出:
string
时间复杂度: O(N),其中 N 是字符串的长度。
辅助空间: O(1)