forked from imnishant/GeeksforGeeks-Java-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchSpecificPattern
More file actions
27 lines (27 loc) · 774 Bytes
/
MatchSpecificPattern
File metadata and controls
27 lines (27 loc) · 774 Bytes
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
class GfG{
static String encode(String s)
{
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
int i = 0;
String res = "";
for(int j=0 ; j<s.length() ; j++)
{
if(!hm.containsKey(s.charAt(j)))
hm.put(s.charAt(j), i++);
res += hm.get(s.charAt(j));
}
return res;
}
public static ArrayList<String> findMatchedWords(ArrayList<String> dict, String pattern)
{
ArrayList<String> al = new ArrayList<String>();
String hash = encode(pattern);
for(int i=0 ; i<dict.size() ; i++)
{
String s = dict.get(i);
if(s.length() == pattern.length() && encode(s).equals(hash))
al.add(s);
}
return al;
}
}