Tap here to switch tabs
Problem
Submissions

Problem: Sentence Similarity II

med
30 min
Try to solve the Sentence Similarity II problem.

Statement

Given two arrays of strings sentence1 and sentence2, and an array of string pairs similarPairs where each similarPairs[i] = [x_i, y_i] indicates that the words x_i and y_i are similar, determine if sentence1 and sentence2 are similar.

Two sentences are similar if:

  • They have the same length.

  • For each valid index i, the word sentence1[i] is similar to sentence2[i].

Similarity is an equivalence relation:

  • Every word is similar to itself.

  • If word AA is similar to word BB, then word BB is similar to word AA.

  • If word AA is similar to word BB and word BB is similar to word CC, then word AA is similar to word CC.

Return true if sentence1 and sentence2 are similar, and false otherwise.

Note: Similarity relation is transitive, unlike Sentence Similarity I.

Constraints:

  • 1≤1 \leq sentence1.length, sentence2.length ≤1000\leq 1000

  • 1≤1 \leq sentence1[i].length, sentence2[i].length ≤20\leq 20

  • sentence1[i] and sentence2[i] consist of lowercase and uppercase English letters.

  • 0≤0 \leq similarPairs.length ≤2000\leq 2000

  • similarPairs[i].length =2= 2

  • 1≤1 \leq x_i.length, y_i.length ≤20\leq 20

  • x_i and y_i consist of English letters.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Sentence Similarity II

med
30 min
Try to solve the Sentence Similarity II problem.

Statement

Given two arrays of strings sentence1 and sentence2, and an array of string pairs similarPairs where each similarPairs[i] = [x_i, y_i] indicates that the words x_i and y_i are similar, determine if sentence1 and sentence2 are similar.

Two sentences are similar if:

  • They have the same length.

  • For each valid index i, the word sentence1[i] is similar to sentence2[i].

Similarity is an equivalence relation:

  • Every word is similar to itself.

  • If word AA is similar to word BB, then word BB is similar to word AA.

  • If word AA is similar to word BB and word BB is similar to word CC, then word AA is similar to word CC.

Return true if sentence1 and sentence2 are similar, and false otherwise.

Note: Similarity relation is transitive, unlike Sentence Similarity I.

Constraints:

  • 1≤1 \leq sentence1.length, sentence2.length ≤1000\leq 1000

  • 1≤1 \leq sentence1[i].length, sentence2[i].length ≤20\leq 20

  • sentence1[i] and sentence2[i] consist of lowercase and uppercase English letters.

  • 0≤0 \leq similarPairs.length ≤2000\leq 2000

  • similarPairs[i].length =2= 2

  • 1≤1 \leq x_i.length, y_i.length ≤20\leq 20

  • x_i and y_i consist of English letters.