Question
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
[execution time limit] 4 seconds (py3)
[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
.
- Array of the longest strings, stored in the same order as in the
MY_ANSWER
def allLongestStrings(inputArray):
answer = []
stringlen = []
for i in inputArray:
stringlen.append(len(i))
for i in inputArray:
if len(i) == max(stringlen):
answer.append(i)
return answer
-> inputArray에 입력된 모든 단어 길이를 확인 후, 가장 긴 단어 길이를 추출한다. 최대 단어 길이와 동일한 길이를 가진 단어들을 출력
Best_ANSWER
def allLongestStrings(inputArray):
m = max(len(s) for s in inputArray)
r = [s for s in inputArray if len(s) == m]
return r
-> 내가 생각한 코드를 list comprehension을 통해서 간단하게 구현이 가능하다.