Brute force through out since the input size is quite small (1 <= words.length <= 50
)
Leaving another solution approach that leetcode user votrubac had posted in the solutions tab that I liked. It’s an optimized version of storing and searching to check if have seen a newer string earlier by using the ord
or ASCII value of the characters, since it’s given that there would only be two characters in a string.
Code:
Python3
Big O Analysis
-
Runtime
The runtime complexity here is
O(N)
where N = length of listwords
. -
Memory
The memory usage is
O(N)
since we use aret
string.
— A