Remove Invalid Parentheses | Hard

Leetcode

Intuition:

- Use DFS to recursively either remove the character or just continue.

Gotchas:

- The program expects empty string in the answer if there are no other matches
- non-parenthesis characters is like a no-op character, that moves the iterator without doing anything.
- The answer needs to be in set to avoid duplicates
- left parenthesis(L) has a preferential consideration over right parenthesis(R) since R is an invariant if there is more R than L at any given position.

Base case:

- The resulting answer should have a matching number of parenthesis.
- Iteration ends when the iteration reaches the end of …
more ...

Verifying an Alien Dictionary | Easy

Leetcode

Intuition:

  • Store the alien alphabet in a dictionary so that lookup can be in O(1)

Gotchas

  • Not all characters need to be verified, only when the characters are equal
  • The words might not be of the same length

Base case:

  • at least two words to be given

Solution:

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        order = { v:k for k,v in enumerate(order) }
        if len(words) < 2:
            return True
        i = 0
        j = 1
        def compareWords(w1, w2):
            i = 0
            j = 0
            while i < len(w1) and j < len(w2):
                if order[w1[i …
more ...