Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Last active May 31, 2017 21:49
Show Gist options
  • Save tyler-austin/e3f254697a69976cbbdc59f8aee3412a to your computer and use it in GitHub Desktop.
Save tyler-austin/e3f254697a69976cbbdc59f8aee3412a to your computer and use it in GitHub Desktop.
Code Fights - allLongestStrings

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

  • [time limit] 4000ms (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.
def all_longest_strings(in_array: list):
longest_len = _longest_string_size(in_array)
result = list()
for s in in_array:
if len(s) == longest_len:
result.append(s)
return result
def _longest_string_size(in_array):
longest_len = 0
for s in in_array:
s_len = len(s)
if s_len > longest_len:
longest_len = s_len
return longest_len
if __name__ == '__main__':
input_array = ["aba", "aa", "ad", "vcd", "aba"]
print(all_longest_strings(input_array))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment