import java.util.HashMap;
import java.util.Map;
public class sentenceSimilarity {
public static Map<String,String> father;
public static void main(String[] args) {
String[] word1={"great", "acting", "skills"};
String[] word2={"fine", "drama", "talent"};
String[][] pairs={{"great", "good"}, {"fine", "good"}, {"acting","drama"}, {"skills","talent"}};
System.out.println(isSimiliar(word1,word2,pairs));
}
public static boolean isSimiliar(String[] word1,String[] word2,String[][] pairs){
father=new HashMap<>();
for(int i=0;i<pairs.length;i++){
father.put(pairs[i][0],pairs[i][0]);
father.put(pairs[i][1],pairs[i][1]);
}
for(int i=0;i<pairs.length;i++){
union(pairs[i][0],pairs[i][1]);
}
for(int i=0;i<word1.length;i++){
if(find(word1[i])!=find(word2[i])){
return false;
}
}
return true;
}
static String find(String x){
String temp=x;
while(father.get(temp)!=temp){
temp=father.get(temp);
}
father.put(x,temp);
return temp;
}
static void union(String a,String b){
String x=father.get(a);
String y=father.get(b);
if(x!=y){
father.put(x,y);
}
}
}