Programming/Algorithm

[Algorithm] allLongestStrings

ST1CKER 2018. 2. 9. 14:59


Problem


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


Example

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


Input/Output

  • [input] array.string inputArray

    A non-empty array.

    Guaranteed constraints:
    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.


Solution

def allLongestStrings(inputArray):

length = max([len(i) for i in inputArray])

return [i for i in inputArray if len(i) == length]

*length : string으로 구성된 inputArray 속의 문자열들에 대한 길이의 최대값을 저장하는 변수입니다.

최종적으로 length에 저장된 길이와 같은 문자열들을 반환하여 문제를 해결할 수 있습니다.