-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckTwoStringArraysEquivalent.py
More file actions
38 lines (30 loc) · 1016 Bytes
/
CheckTwoStringArraysEquivalent.py
File metadata and controls
38 lines (30 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.
# A string is represented by an array if the array elements concatenated in order forms the string.
#Faster solution, less memory efficient
def arrayStringsAreEqual(word1, word2):
w1 = ""
w2 = ""
for word in word1:
w1 = w1 + word
for word in word2:
w2 = w2 + word
if w1 == w2:
return True
else:
return False
#More memory efficient, slower solution
def arrayStringsAreEqual2(word1, word2):
return "".join(word1) == "".join(word2)
#Test cases
word1 = ["ab", "c"]
word2 = ["a", "bc"]
print(arrayStringsAreEqual(word1, word2))
print(arrayStringsAreEqual2(word1, word2))
word1 = ["a", "cb"]
word2 = ["ab", "c"]
print(arrayStringsAreEqual(word1, word2))
print(arrayStringsAreEqual2(word1, word2))
word1 = ["abc", "d", "defg"]
word2 = ["abcddefg"]
print(arrayStringsAreEqual(word1, word2))
print(arrayStringsAreEqual2(word1, word2))