Find Common Characters — LeetCode

Soren Rood
2 min readDec 8, 2020

Question:

LeetCode Question: 1002

Conceptually, if we are trying to find characters that exist in all of the strings, we can start with the first string and loop through the entire list, removing elements from our “checker” as they no longer appear in the string.

We can solve this by keeping two lists while we loop. The first list contains all string matches from the last iteration, while the second loop will account for the matches on the current iteration. For each character in each word in the input list, we validate that it exists in the first list, if it does- we remove it from the first list, and move it to the second list. At the end of each loop, the second loop will contain all of the common characters.

This problem may be better explained by showing the code:

Submission Code

It’s important that while we move items from list1 to list 2, we don’t forget to remove()the item from list 1. This is because we check if characters exist in the check list to determine if it is a common character.

Also, something to remember is that we do not need to iterate through the entire input array, we can actually start at A[1:] since we store the first value.

Here are the submission details for the above code:

Submission Details

Source code available here.

My social links are available here.

--

--