Skip to content

Commit 774c2d6

Browse files
author
hariniparth
committed
Pattern Matching
1 parent f059620 commit 774c2d6

7 files changed

Lines changed: 240 additions & 0 deletions

File tree

‎InterviewCake/PatternMatching/.idea/description.html‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/PatternMatching/.idea/misc.xml‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/PatternMatching/.idea/modules.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/PatternMatching/.idea/project-template.xml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/PatternMatching/.idea/workspace.xml‎

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.company;
2+
3+
import java.util.*;
4+
5+
// https://www.geeksforgeeks.org/find-all-strings-that-match-specific-pattern-in-a-dictionary/
6+
7+
public class Main {
8+
9+
public void patternMatching(List<String> strings, String pattern){
10+
int len = pattern.length();
11+
String hash = findPattern(pattern);
12+
for(String string : strings){
13+
if(string.length() == len && findPattern(pattern).equals(hash))
14+
System.out.println(string);
15+
}
16+
}
17+
18+
public String findPattern(String pattern){
19+
HashMap<Character, Integer> map = new HashMap<>();
20+
String result = "";
21+
int count = 0;
22+
23+
for(int i=0;i<pattern.length();i++){
24+
char ch = pattern.charAt(i);
25+
if(!map.containsKey(ch))
26+
map.put(ch, count++);
27+
28+
result += map.get(ch);
29+
}
30+
return result;
31+
}
32+
}

0 commit comments

Comments
 (0)