String from an array of words
Construct a string from a given array of words
Problem statement
You are given an array of strings arr. Your task is to construct a string from the words in arr starting with the 0th character from each word (in the order they appear in arr), followed by the 1th character, then the 2th character, etc. If one of the words doesn’t have an ith character, skip that word.
Return the resulting string.
Examples
Example 1
For arr = [“Daisy”, “Rose”, “Hyacinth”, “Poppy”], the output should be solution(arr) = “DRHPaoyoisapsecpyiynth”
- First, we append all 0th characters and obtain string “DRHP”
- Then we append all 1th characters and obtain string “DRHPaoyo”
- Then we append all 2th characters and obtain string “DRHPaoyoisap”
- Then we append all 3rd characters and obtain string “DRHPaoyoisapaecp”
- Then we append all 4th characters and obtain string “DRHPaoyoisapaecpyiy”
- Finally, only letters in the arr[2] are left, so we append the rest characters and get “DRHPaoyoisapaecpyiynth”
Example 2
For arr = [“E”, “M”, “I”, “L”, “Y”] the output should be solution(arr) = “EMILY”
- Since each of these strings have only one character, the answer will be concatenation of each string in order, so the answer is EMILY
Input/Output
Guaranteed constraints:
1 ≤ arr.length ≤ 100
1 ≤ arr[i].length < 100
[execution time limit] 4 seconds (py3)
[input] array string arr
An array of strings containing alphanumeric characters.
[output] string
Return the resulting string.
Implementation
def helper(arr, i, temp=""):
for word in arr:
if i < len(word):
temp += word[i]
else:
continue
return temp
def solution(arr):
result_string = ""
i = 0
max_len = 0
for word in arr:
if len(word) > max_len:
max_len = len(word)
for wrd in arr:
while i<max_len:
result_string += helper(arr,i)
i += 1
return result_string