原文:

给定一个字符串的数组arr[],任务是打印给定数组中所有最大长度的字符串。

示例:

输入:arr[]= {“aba”、“aa”、“ad”、“vcd”、“aba”} 输出: aba vcd aba 解释: 给定数组中所有字符串的最大长度为 3。 数组中长度等于 3 的字符串为“aba”、“vcd”、“aba”。

输入:arr[]= {“abb”、“abcd”、“guw”、“v”} 输出: abcd 解释: 给定数组中所有字符串的最大长度为 4。 数组中长度等于 4 的字符串是“abcd”。

方法:按照以下步骤解决问题:

  1. 遍历给定的字符串数组。计算数组中所有字符串的最大长度,并将其存储在一个变量中,比如 len
  2. 现在,再次遍历字符串数组,打印数组中长度等于 len 的字符串。

下面是上述方法的实现:

c

// c   program to implement
// the above approach
#include 
using namespace std;
// function to find the length
// of the longest string from
// the given array of strings
int maxlength(vector arr)
{
    int len = int_min;
    int n = arr.size();
    // traverse the array
    for (int i = 0; i < n; i  ) {
        // stores the length
        // of current string
        int l = arr[i].size();
        // update maximum length
        if (len < l) {
            len = l;
        }
    }
    // return the maximum length
    return len;
}
// function to print the
// longest strings from the array
void maxstrings(vector arr, int len)
{
    int n = arr.size();
    vector ans;
    // find the strings having length
    // equals to len
    for (int i = 0; i < n; i  ) {
        if (len == arr[i].size()) {
            ans.push_back(arr[i]);
        }
    }
    // print the resultant
    // vector of strings
    for (int i = 0; i < ans.size(); i  ) {
        cout << ans[i] << " ";
    }
}
// function to print all the
// longest strings from the array
void printstrings(vector& arr)
{
    // find the length of longest string
    int max = maxlength(arr);
    // find and print all the strings
    // having length equals to max
    maxstrings(arr, max);
}
// driver code
int main()
{
    vector arr
        = { "aba", "aa", "ad", "vcd", "aba" };
    printstrings(arr);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to implement
// the above approach
import java.util.*;
class gfg{
// function to find the length
// of the longest string from
// the given array of strings
static int maxlength(string []arr)
{
    int len = integer.min_value;
    int n = arr.length;
    // traverse the array
    for(int i = 0; i < n; i  )
    {
        // stores the length
        // of current string
        int l = arr[i].length();
        // update maximum length
        if (len < l)
        {
            len = l;
        }
    }
    // return the maximum length
    return len;
}
// function to print the
// longest strings from the array
static void maxstrings(string []arr, int len)
{
    int n = arr.length;
    vector ans = new vector();
    // find the strings having length
    // equals to len
    for(int i = 0; i < n; i  )
    {
        if (len == arr[i].length())
        {
            ans.add(arr[i]);
        }
    }
    // print the resultant
    // vector of strings
    for(int i = 0; i < ans.size(); i  )
    {
        system.out.print(ans.get(i)   " ");
    }
}
// function to print all the
// longest strings from the array
static void printstrings(string [] arr)
{
    // find the length of longest string
    int max = maxlength(arr);
    // find and print all the strings
    // having length equals to max
    maxstrings(arr, max);
}
// driver code
public static void main(string[] args)
{
    string []arr = { "aba", "aa", "ad",
                     "vcd", "aba" };
    printstrings(arr);
}
}
// this code is contributed by amit katiyar

python 3

# python3 program to implement
# the above approach
import sys
# function to find the length
# of the longest string from
# the given array of strings
def maxlength(arr):
    lenn = -sys.maxsize - 1
    n = len(arr)
    # traverse the array
    for i in range(n):
        # stores the length
        # of current string
        l = len(arr[i])
        # update maximum length
        if (lenn < l):
            lenn = l
    # return the maximum length
    return lenn
# function to print the
# longest strings from the array
def maxstrings(arr, lenn):
    n = len(arr)
    ans = []
    # find the strings having length
    # equals to lenn
    for i in range(n):
        if (lenn == len(arr[i])):
            ans.append(arr[i])
    # print the resultant
    # vector of strings
    for i in range(len(ans)):
        print(ans[i], end = " ")
# function to print all the
# longest strings from the array
def printstrings(arr):
    # find the length of longest string
    max = maxlength(arr)
    # find and print all the strings
    # having length equals to max
    maxstrings(arr, max)
# driver code
if __name__ == '__main__':
    arr = [ "aba", "aa", "ad",
            "vcd", "aba" ]
    printstrings(arr)
# this code is contributed by mohit kumar 29

c

// c# program to implement
// the above approach
using system;
using system.collections.generic;
class gfg{
// function to find the length
// of the longest string from
// the given array of strings
static int maxlength(string []arr)
{
    int len = int.minvalue;
    int n = arr.length;
    // traverse the array
    for(int i = 0; i < n; i  )
    {       
        // stores the length
        // of current string
        int l = arr[i].length;
        // update maximum length
        if (len < l)
        {
            len = l;
        }
    }
    // return the maximum length
    return len;
}
// function to print the
// longest strings from the array
static void maxstrings(string []arr,
                       int len)
{
    int n = arr.length;
    list ans = new list();
    // find the strings having length
    // equals to len
    for(int i = 0; i < n; i  )
    {
        if (len == arr[i].length)
        {
            ans.add(arr[i]);
        }
    }
    // print the resultant
    // vector of strings
    for(int i = 0; i < ans.count; i  )
    {
        console.write(ans[i]   " ");
    }
}
// function to print all the
// longest strings from the array
static void printstrings(string [] arr)
{   
    // find the length of longest string
    int max = maxlength(arr);
    // find and print all the strings
    // having length equals to max
    maxstrings(arr, max);
}
// driver code
public static void main(string[] args)
{
    string []arr = {"aba", "aa", "ad",
                    "vcd", "aba"};                    
    printstrings(arr);
}
}
// this code is contributed by rajput-ji

java 描述语言


output: 

aba vcd aba

时间复杂度:o(n) t5辅助空间:** o(n)