How to Solve “allLongestStrings” in CodeFights

The Problem:

Given an array of strings, return another array containing all of its longest strings.


For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].


  • [time limit] 4000ms (py)
  • [input] array.string inputArray

    A non-empty array.

    1 ≤ inputArray.length ≤ 10,
    1 ≤ inputArray[i].length ≤ 10.

  • [output] array.string

    Array of the longest strings, stored in the same order as in the inputArray.

The Solution:
def allLongestStrings(inputArray):
maxx =max(map(lambda x:len(x),inputArray))
return filter(lambda x: len(x)==maxx,inputArray)

The Explanation:

I’ve been thinking about these built-in functions of python for a while and wanted to utilize those to familiarize myself.

First statement gets the maximum length in the string array. To do this, we first map the strings to their lengths and find the max of these values.
Then, we filter the array by their lengths. That is, if it has the same length as maxx, it it in the result set. Otherwise, we filter it out.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s