Discussion - A hash map question

You are given two lists containing words called sentence_1 and sentence_2, and have to check if the two lists are similar or not. Similarity is evaluated via similarity matrix, which is list of lists containing pair of words which are similar. eg. [["A","B"], ["C", "D"]] means A is similar to B (and vice-versa) and C is similar to D.

Consider " -> " as similarity operation. If A is similar to B then A ->B

Rules of similarity:
1. A word is similar to itself. i.e. A -> A
2. Symmetry: If A is similar to B, then B is similar to A. (A->B means B-> A )
3. Transitive: If A -> B and B-> C then A-> C (and C->A as well).
4. Two lists are similar only if all correspoing words are similar. Words not present in matrix are considered not similar to all words present in matrix.

Input are given in format:

# Case 1
#I/P:
sentence_1 = ["amazing" , "near", "big"]
sentence_2 = ["fine", "close", "oversize"]
similarity_matrix = [["fine", "amazing"], ["close", "proximity"], ["around","proximity"], ["near","proximiity"], ["large", "big"], ["big", "huge"],["huge","large"],["massive","oversize"], ["massive", "large"] ]

#O/P:
Output: True

# Case 2
#I/P:
sentence_1 = ["amazing" , "near", "big"]
sentence_2 = ["fine", "close", "wrong"]
similarity_matrix = [["fine", "amazing"], ["close", "proximity"], ["around","proximity"], ["near","proximiity"], ["large", "big"], ["big", "huge"],["huge","large"],["massive","oversize"], ["massive", "large"] ]

#O/P:
Output: False #Since word "wrong" is not similar to any word present in matrix

How to solve this question? How would you rate it - LC Easy/Medium/Hard ?

Comments (1)