Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) {
- int m = words1.length, n = words2.length;
- if(m != n) return false;
- Map<String, Set<String>> map = new HashMap<>();
- for(String[] pair : pairs)
- {
- String u = pair[0], v = pair[1];
- if(!map.containsKey(u)) map.put(u, new HashSet<>());
- if(!map.containsKey(v)) map.put(v, new HashSet<>());
- map.get(u).add(v);
- map.get(v).add(u);
- }
- for(int i = 0; i < m; i++)
- {
- String w1 = words1[i], w2 = words2[i];
- if(w1.equals(w2) || map.containsKey(w1) && map.get(w1).contains(w2) ||
- map.containsKey(w2) && map.get(w2).contains(w1)) continue;
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment