package com.company;
import java.util.*;
public class Solution {
static public void main(String[] args) {
String strA1 = "FRANCE";
String strB1 = "french";
String strA2 = "handshake";
String strB2 = "shake hands";
String strA3 = "aa1+aa2";
String strB3 = "AAAA12";
String strA4 = "E=M*C^2";
String strB4 = "e=m*c^2";
Solution testSolution = new Solution();
System.out.println(testSolution.solution(strA1, strB1));
System.out.println(testSolution.solution(strA2, strB2));
System.out.println(testSolution.solution(strA3, strB3));
System.out.println(testSolution.solution(strA4, strB4));
System.out.println(testSolution.solution("abc ", " abbb"));
System.out.println(testSolution.solution("ab ", " bbbb"));
}
public int solution(String str1, String str2) {
String[] splitStringArr1 = splitString(str1);
String[] splitStringArr2 = splitString(str2);
double similarityOfTwoString = caculateSimilarity(splitStringArr1, splitStringArr2);
return (int) Math.floor(similarityOfTwoString * 65536);
}
private double caculateSimilarity(String[] splitStringArr1, String[] splitStringArr2) {
if(splitStringArr1.length == 0 && splitStringArr2.length == 0)
return 1;
double unionLength = 0;
double intersectionLength = 0;
Map<String, Integer> map1 = new HashMap<String, Integer>();
Map<String, Integer> map2 = new HashMap<String, Integer>();
for (String splitString : splitStringArr1) {
map1.put(splitString, map1.getOrDefault(splitString, 0) + 1);
}
for (String splitString : splitStringArr2) {
map2.put(splitString, map2.getOrDefault(splitString, 0) + 1);
}
for (String mapKey : map1.keySet()) {
if(map2.containsKey(mapKey)){
intersectionLength += Math.min(map1.get(mapKey), map2.get(mapKey));
}
}
unionLength = splitStringArr1.length + splitStringArr2.length - intersectionLength;
return intersectionLength / unionLength;
}
private String[] splitString(String str) {
List<String> answer = new ArrayList<>();
for (int i = 0; i < (str.length() - 1); i++) {
String tempStr = str.substring(i, i + 2).toLowerCase();
if(!Character.isLowerCase(tempStr.charAt(0)) || !Character.isLowerCase(tempStr.charAt(1)))
continue;
answer.add(tempStr);
}
return answer.toArray(new String[0]);
}
}