Skip to content

Commit

Permalink
Match
Browse files Browse the repository at this point in the history
  • Loading branch information
Pastor committed Dec 13, 2024
1 parent 6fd133e commit a3ef45d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
11 changes: 11 additions & 0 deletions vol7/src/main/java/ru/mifi/practice/vol7/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ru.mifi.practice.vol7.fibonacci.FibonacciMemorized;
import ru.mifi.practice.vol7.fibonacci.FibonacciRecursion;
import ru.mifi.practice.vol7.subsequence.LongestCommonSubsequence;
import ru.mifi.practice.vol7.wildcard.Match;

import java.util.List;

Expand All @@ -30,6 +31,8 @@ public static void main(String[] args) {
distance("Lev.Recur", new Levenshtein.LevenshteinRecursion(), "boobs", "bomb");
distance("Lev.Dynam", new Levenshtein.VagnerFisherDynamicus(), "boobs", "bomb");
lcs("Sub.Commo", new LongestCommonSubsequence.Default(), "mouse", "house");
match("Dyn.Regex", new Match.DefaultMatch(), "non? po?", "none pop");
match("Dyn.Aggrv", new Match.AbbreviationMatch(), "Algorithm and Data Structure", "ADS");
}

private static void fibonacci(String name, Fibonacci fibonacci) {
Expand Down Expand Up @@ -57,4 +60,12 @@ private static void lcs(String name, LongestCommonSubsequence subsequence, Strin
System.out.println(prefix + "Val : " + d);
System.out.println(prefix + "Cnt : " + counter);
}

private static void match(String name, Match match, String pattern, String text) {
Counter counter = new Counter.Default();
String prefix = String.format("Rex.%s.", name);
var m = match.isMatch(pattern, text, counter);
System.out.println(prefix + "Val : " + m);
System.out.println(prefix + "Cnt : " + counter);
}
}
71 changes: 71 additions & 0 deletions vol7/src/main/java/ru/mifi/practice/vol7/wildcard/Match.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.mifi.practice.vol7.wildcard;

import ru.mifi.practice.vol7.Counter;

public interface Match {
boolean isMatch(String pattern, String text, Counter counter);

final class DefaultMatch implements Match {

@Override
public boolean isMatch(String pattern, String text, Counter counter) {
int m = text.length();
int n = pattern.length();
boolean[][] table = new boolean[m + 1][n + 1];
table[0][0] = true;

for (int j = 1; j <= n; j++) {
if (pattern.charAt(j - 1) == '*') {
table[0][j] = table[0][j - 1];
}
}

for (int i = 1; i <= m; i++) {
counter.increment();
for (int j = 1; j <= n; j++) {
counter.increment();
char textChar = text.charAt(i - 1);
char patternChar = pattern.charAt(j - 1);

if (patternChar == textChar || patternChar == '?') {
table[i][j] = table[i - 1][j - 1];
} else if (patternChar == '*') {
table[i][j] = table[i - 1][j] || table[i][j - 1];
} else {
table[i][j] = false;
}
}
}
return table[m][n];
}
}

final class AbbreviationMatch implements Match {

@Override
public boolean isMatch(String text, String abb, Counter counter) {
int n = text.length();
int m = abb.length();

boolean[][] table = new boolean[n + 1][m + 1];

table[0][0] = true;

for (int i = 0; i < n; i++) {
counter.increment();
for (int j = 0; j <= m; j++) {
counter.increment();
if (table[i][j]) {
if (j < m && Character.toUpperCase(text.charAt(i)) == abb.charAt(j)) {
table[i + 1][j + 1] = true;
}
if (Character.isLowerCase(text.charAt(i)) || Character.isWhitespace(text.charAt(i))) {
table[i + 1][j] = true;
}
}
}
}
return table[n][m];
}
}
}

0 comments on commit a3ef45d

Please sign in to comment.