forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimilar String Groups.cpp
63 lines (55 loc) · 1.39 KB
/
Similar String Groups.cpp
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
// Runtime: 634 ms (Top 19.62%) | Memory: 166 MB (Top 12.21%)
class Solution {
public:
bool similar(string s1,string s2)
{
int n=s1.size();
int i=0,j=n-1;
int missMatch=0;
for(i=0;i<n;i++)
{
if(s1[i]!=s2[i] and ++missMatch>2)return 0;
}
return 1;
}
void dfs(string x,unordered_map<string,vector<string>>&adj,unordered_set<string>&visited)
{
visited.insert(x);
for(auto nbr:adj[x])
{
if(visited.find(nbr)==visited.end())
{
dfs(nbr,adj,visited);
}
}
}
int numSimilarGroups(vector<string>& strs) {
int n = strs.size();
unordered_map<string,vector<string>>adj;
for(int i=0;i<n;i++)
{
string s1 = strs[i];
for(int j=i+1;j<n;j++)
{
string s2 = strs[j];
if(similar(s1,s2))
{
adj[s1].push_back(s2);
adj[s2].push_back(s1);
}
}
}
int count=0;
unordered_set<string>visited;
for(auto x:strs)
{
if(visited.find(x)==visited.end())
{
//cout<<x<<endl;
dfs(x,adj,visited);
count++;
}
}
return count;//(strs[0],strs[2]);
}
};