The problem is defined as follows:
Given two string arrays word1
and word2
, return true
if the two arrays represent the same string, and false
otherwise.
A string array represents a string formed by concatenating its elements in order. For example, if word1 = ["ab", "c"]
, then it represents the string "abc"
.
word1
and word2
?To solve this problem, follow these steps:
word1
to form a single string str1
.word2
to form a single string str2
.str1
and str2
. If they are equal, return true
; otherwise, return false
.Here’s how you can implement this in Python:
def arrayStringsAreEqual(word1, word2):
# Join all elements of word1 and word2
str1 = ''.join(word1)
str2 = ''.join(word2)
# Compare the joined strings
return str1 == str2
The time complexity of this solution is determined by the time it takes to concatenate the strings:
n
be the total length of all strings in word1
.m
be the total length of all strings in word2
.word1
takes O(n) time.word2
takes O(m) time.n
and m
in the worst case can take O(min(n, m)).Therefore, the overall time complexity is O(n + m).
word1 = ["ab", "c"]
word2 = ["a", "bc"]
print(arrayStringsAreEqual(word1, word2)) # Output: True
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?