Skip to content

Commit fa5dc20

Browse files
committed
Add WordPatternMatcher.java new algorithm
1 parent f34fe4d commit fa5dc20

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Class to determine if a pattern matches a string using backtracking.
8+
*
9+
* Example:
10+
* Pattern: "abab"
11+
* Input String: "JavaPythonJavaPython"
12+
* Output: true
13+
*
14+
* Pattern: "aaaa"
15+
* Input String: "JavaJavaJavaJava"
16+
* Output: true
17+
*
18+
* Pattern: "aabb"
19+
* Input String: "JavaPythonPythonJava"
20+
* Output: false
21+
*/
22+
public class WordPatternMatcher {
23+
24+
/**
25+
* Determines if the given pattern matches the input string using backtracking.
26+
*
27+
* @param pattern The pattern to match.
28+
* @param inputString The string to match against the pattern.
29+
* @return True if the pattern matches the string, False otherwise.
30+
*/
31+
public static boolean matchWordPattern(String pattern, String inputString) {
32+
Map<Character, String> patternMap = new HashMap<>();
33+
Map<String, Character> strMap = new HashMap<>();
34+
return backtrack(pattern, inputString, 0, 0, patternMap, strMap);
35+
}
36+
37+
/**
38+
* Backtracking helper function to check if the pattern matches the string.
39+
*
40+
* @param pattern The pattern string.
41+
* @param inputString The string to match against the pattern.
42+
* @param patternIndex Current index in the pattern.
43+
* @param strIndex Current index in the input string.
44+
* @param patternMap Map to store pattern characters to string mappings.
45+
* @param strMap Map to store string to pattern character mappings.
46+
* @return True if the pattern matches, False otherwise.
47+
*/
48+
private static boolean backtrack(String pattern, String inputString, int patternIndex, int strIndex,
49+
Map<Character, String> patternMap, Map<String, Character> strMap) {
50+
if (patternIndex == pattern.length() && strIndex == inputString.length()) {
51+
return true;
52+
}
53+
if (patternIndex == pattern.length() || strIndex == inputString.length()) {
54+
return false;
55+
}
56+
57+
char currentChar = pattern.charAt(patternIndex);
58+
if (patternMap.containsKey(currentChar)) {
59+
String mappedStr = patternMap.get(currentChar);
60+
if (inputString.startsWith(mappedStr, strIndex)) {
61+
return backtrack(pattern, inputString, patternIndex + 1, strIndex + mappedStr.length(), patternMap, strMap);
62+
} else {
63+
return false;
64+
}
65+
}
66+
67+
for (int end = strIndex + 1; end <= inputString.length(); end++) {
68+
String substring = inputString.substring(strIndex, end);
69+
if (strMap.containsKey(substring)) {
70+
continue;
71+
}
72+
73+
patternMap.put(currentChar, substring);
74+
strMap.put(substring, currentChar);
75+
if (backtrack(pattern, inputString, patternIndex + 1, end, patternMap, strMap)) {
76+
return true;
77+
}
78+
79+
patternMap.remove(currentChar);
80+
strMap.remove(substring);
81+
}
82+
83+
return false;
84+
}
85+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class WordPatternMatcherTest {
9+
10+
@Test
11+
public void testPatternMatchingSuccess() {
12+
assertTrue(WordPatternMatcher.matchWordPattern("aba", "GraphTreesGraph"));
13+
assertTrue(WordPatternMatcher.matchWordPattern("xyx", "PythonRubyPython"));
14+
}
15+
16+
@Test
17+
public void testPatternMatchingFailure() {
18+
assertFalse(WordPatternMatcher.matchWordPattern("GG", "PythonJavaPython"));
19+
}
20+
21+
@Test
22+
public void testEmptyPatternAndString() {
23+
assertTrue(WordPatternMatcher.matchWordPattern("", ""));
24+
}
25+
26+
@Test
27+
public void testEmptyPattern() {
28+
assertFalse(WordPatternMatcher.matchWordPattern("", "nonempty"));
29+
}
30+
31+
@Test
32+
public void testEmptyString() {
33+
assertFalse(WordPatternMatcher.matchWordPattern("abc", ""));
34+
}
35+
36+
@Test
37+
public void testLongerPatternThanString() {
38+
assertFalse(WordPatternMatcher.matchWordPattern("abcd", "abc"));
39+
}
40+
}

0 commit comments

Comments
 (0)