Skip to content

Commit 517eb93

Browse files
committed
Check If Two String Arrays are Equivalent
1 parent 23b98f3 commit 517eb93

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
3+
4+
Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.
5+
A string is represented by an array if the array elements concatenated in order forms the string.
6+
7+
Example 1:
8+
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
9+
Output: true
10+
Explanation:
11+
word1 represents string "ab" + "c" -> "abc"
12+
word2 represents string "a" + "bc" -> "abc"
13+
The strings are the same, so return true.
14+
15+
Example 2:
16+
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
17+
Output: false
18+
19+
Example 3:
20+
Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
21+
Output: true
22+
23+
Constraints:
24+
1 <= word1.length, word2.length <= 103
25+
1 <= word1[i].length, word2[i].length <= 103
26+
1 <= sum(word1[i].length), sum(word2[i].length) <= 103
27+
word1[i] and word2[i] consist of lowercase letters.
28+
"""
29+
class Solution:
30+
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
31+
i = j = inner_i = inner_j = 0
32+
while i < len(word1) and j < len(word2):
33+
if word1[i][inner_i] != word2[j][inner_j]:
34+
return False
35+
36+
if inner_i + 1 == len(word1[i]):
37+
inner_i = 0
38+
i += 1
39+
else:
40+
inner_i += 1
41+
42+
if inner_j + 1 == len(word2[j]):
43+
inner_j = 0
44+
j += 1
45+
else:
46+
inner_j += 1
47+
48+
return i >= len(word1) and j >= len(word2)

0 commit comments

Comments
 (0)