1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
| import java.util.*;
class Solution
{
public int solution(String s1, String s2)
{
//s1 = adjustString(s1);
//s2 = adjustString(s2);
return (int)(jaccard2(s1.toLowerCase(), s2.toLowerCase()) * 65536);
}
double jaccard(String s1, String s2)
{
int l1 = s1.length(), l2 = s2.length();
double r = 0;
String tmp = "";
HashMap<String, Integer> set1 = new HashMap<>(), set2 = new HashMap<>();
HashMap<String, Integer> intersection = new HashMap<>(), union = new HashMap<>();
int intersectionCnt = 0, unionCnt = 0;
for (int i = 0; i < l1 - 1; i ++)
{
tmp = s1.substring(i, i + 2);
if (set1.containsKey(tmp))
set1.replace(tmp, set1.get(tmp) + 1);
else
{
set1.put(tmp, 1);
if (!union.containsKey(tmp))
union.put(tmp, 0);
}
}
for (int i = 0; i < l2 - 1; i ++)
{
tmp = s2.substring(i, i + 2);
if (set2.containsKey(tmp))
set2.replace(tmp, set2.get(tmp) + 1);
else
{
set2.put(tmp, 1);
if (union.containsKey(tmp))
intersection.put(tmp, 0);
}
}
for (String i:intersection.keySet())
{
intersectionCnt += Math.min(set1.get(i), set2.get(i));
}
for (String u:union.keySet())
{
unionCnt += Math.max(0, 0);
}
return r;
}
double jaccard2(String s1, String s2)
{
int l1 = s1.length(), l2 = s2.length();
char c1, c2;
HashSet<String> intersection = new HashSet<>(), union = new HashSet<>(), set1 = new HashSet<>();
String tmp = "";
for (int i = 0; i < l1 - 1; i ++)
{
c1 = s1.charAt(i);
c2 = s1.charAt(i + 1);
if ('a' <= c1 && c1 <= 'z' && 'a' <= c2 && c2 <= 'z')
{
tmp = s1.substring(i, i + 2);
set1.add(tmp);
union.add(tmp);
}
}
for (int i = 0; i < l2 - 1; i ++)
{
c1 = s2.charAt(i);
c2 = s2.charAt(i + 1);
if ('a' <= c1 && c1 <= 'z' && 'a' <= c2 && c2 <= 'z')
{
tmp = s2.substring(i, i + 2);
union.add(tmp);
if (set1.contains(tmp))
intersection.add(tmp);
}
}
if (union.size() == 0)
return 1;
return (double)intersection.size() / union.size();
}
}
|