Python 1662.py
class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1) == "".join(word2)
Difficulty
and has an acceptance rate of about 86% (600k+ submissions).
Typical patterns to solve this problem:
- Array
- String
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a single method arrayStringsAreEqual inside a Solution class. Its purpose is to determine whether two arrays of strings, word1 and word2, represent the same concatenated string. The method achieves this by joining each list of substrings into a single string and then performing a direct equality comparison.
Key Steps
The core logic consists of two straightforward operations. First, the expression "" .join(word1) concatenates all elements of word1 without any separator, producing a single continuous string. The same operation is applied to word2. Second, the equality operator == compares the two resulting strings, yielding a Boolean result that indicates whether the concatenated forms are identical.
joined1 = "".join(word1)
joined2 = "".join(word2)
return joined1 == joined2
The method returns the Boolean outcome directly, eliminating the need for additional conditional structures.
Edge Cases Handled
When either word1 or word2 is an empty list, the join operation yields an empty string (""). The comparison therefore correctly evaluates to True only when both lists are empty, matching the expected semantics for empty concatenations.
If the input lists contain empty strings among other non-empty elements, those empty strings contribute no characters to the final concatenated result, and the method still produces the correct comparison outcome.
The implementation also gracefully handles cases where the lists contain a single element; the join operation simply returns that element unchanged, and the equality check proceeds as usual.
Performance Optimizations
The solution leverages Python’s built-in str.join method, which is implemented in C and optimized for concatenating sequences of strings. This approach avoids the quadratic time behavior that would arise from repeatedly using the + operator in a loop. By constructing each full string in a single pass and then performing a single equality comparison, the method minimizes the number of intermediate objects created and reduces overall memory churn. The direct return of the comparison result eliminates any extra variable assignments or branching, keeping the execution path tight.
ELI5 - Simplified Explanation
Imagine there are two separate boxes of alphabet blocks. The first box has some blocks, and the second box has some different blocks. The goal is to see if both boxes hold the exact same word when we line all the blocks up in a row.
The first part of the code takes all the blocks from the first box and sticks them together in a long line. The second part does the same thing for the second box. Once those two lines are made, the code simply looks at them side by side to check if they look identical. If the lines are exactly the same, it means the boxes held the same word.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||||
| www | AlgoMonster | C++ | Python | Java | TS | ||||||
| WalkCCC | C++ | Python | Java | ||||||||
| leetcode.ca | C++ | Python | Java | Go | C | Rust | TS | ||||
| Progiez | C++ | Python | Java | ||||||||
| github | black-shadows | C++ | |||||||||
| chandansgowda | Python | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| everthis | JS | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JS | ||||||||||
| Krazune | C++ | ||||||||||
| lkwq007 | Python | ||||||||||
| MacJim | C++ | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java | ||||||||||
| vthwang | Java | ||||||||||
| YuriSpiridonov | Python | ||||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||||
| WalkCCC | C++, Python, Java | ||||||||||
| leetcode.ca | C, C++, Go, Java, Python, Rust, TypeScript | ||||||||||
| Progiez | C++, Java, Python | ||||||||||
| black-shadows | C++, C++ | ||||||||||
| chandansgowda | Python | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| everthis | JavaScript | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JavaScript | ||||||||||
| Krazune | C++ | ||||||||||
| lkwq007 | Python | ||||||||||
| MacJim | C++ | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java | ||||||||||
| vthwang | Java | ||||||||||
| YuriSpiridonov | Python |
Videos
Similar Problems
Solved On The Same Day
- problem 1281: Subtract the Product and Sum of Digits of an Integer
- problem 522: Longest Uncommon Subsequence II
- problem 1401: Circle and Rectangle Overlapping
- problem 2616: Minimize the Maximum Difference of Pairs
- problem 847: Shortest Path Visiting All Nodes
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality