public static boolean isNearby(String str1, String str2) {
Map<Character, int[]> vectorMap = new HashMap<>();
char[] chArray1 = str1.toCharArray();
for (char c : chArray1) {
if (vectorMap.containsKey(c)) {
vectorMap.get(c)[0]++;
} else {
int[] arr = new int[2];
arr[0] = 1;
vectorMap.put(c, arr);
}
}
char[] chArray2 = str2.toCharArray();
for (char c : chArray2) {
if (vectorMap.containsKey(c)) {
vectorMap.get(c)[1]++;
} else {
int[] arr = new int[2];
arr[1] = 1;
vectorMap.put(c, arr);
}
}
double vector1Modulo = 0;
double vector2Modulo = 0;
double vectorProduct = 0;
for (Map.Entry<Character, int[]> entry : vectorMap.entrySet()) {
int[] arr = entry.getValue();
vector1Modulo += arr[0] * arr[0];
vector2Modulo += arr[1] * arr[1];
vectorProduct += arr[0] * arr[1];
}
vector1Modulo = Math.sqrt(vector1Modulo);
vector2Modulo = Math.sqrt(vector2Modulo);
if (vector1Modulo == 0 || vector2Modulo == 0) {
return false;
} else {
double v = vectorProduct / (vector1Modulo * vector2Modulo);
return v >= 0.8;
}
}