Problem 1662

Abstract / Summary

Check if two arrays of strings have the same counts for each string after sorting them.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 1662.py

class Solution:
    def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
        return "".join(word1) == "".join(word2)
3 lines | 147 bytes | submitted December 1, 2023 | GitHub   tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 86% (600k+ submissions).

Typical patterns to solve this problem:
  • Array
  • String
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
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.
LeetCode profile | GitHub repo

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.

written by openai gpt

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.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 9 | μ = 8.00 | σ = 1.72

Complexity Analysis

Time: linear
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
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
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
34 links | 21 authors | 10 languages

Videos

10 videos   did my crawler come up with invalid videos ? drop me a line.

Similar Problems

1 similar problem

Solved On The Same Day

6 solutions   December 1, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • 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