From 174eb498654c85e8de457e09e237074f195a3e40 Mon Sep 17 00:00:00 2001 From: priya Date: Sun, 18 Oct 2020 16:02:40 +0530 Subject: [PATCH 1/3] added wild to Algorithms --- Wildcardmatching/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Wildcardmatching/README.md diff --git a/Wildcardmatching/README.md b/Wildcardmatching/README.md new file mode 100644 index 0000000..a011d33 --- /dev/null +++ b/Wildcardmatching/README.md @@ -0,0 +1,34 @@ +# Readme + +***Wild card matching using Dynamic Programming*** + +This is a dynamic programing approach to match the given string with a given pattern. + +The pattern can be empty and can contain only lowercase letters a-z, and characters like ? or *. +The string can be empty and can contain only lowercase letters a-z. + +'?' Matches any single character. +'*' Matches any sequence of characters (including the empty sequence). + +The matching will cover the entire input string (not partial). + +***Example 1:*** + +```Input:``` +s = "aa" +p = "a" + +```Output:``` aa doesn't match with the given pattern: a? + +```Explanation:``` "a" does not match the entire string "aa". + +***Example 2:*** + +```Input:``` +s = "aa" +p = "*" + +```Output:``` aa matches the pattern: *? + +```Explanation:``` '*' matches any sequence. + From 755d83892df4f815b026fbed339973da87d9d6ca Mon Sep 17 00:00:00 2001 From: priya Date: Sun, 18 Oct 2020 16:05:43 +0530 Subject: [PATCH 2/3] added wildcard to Algorithms --- Wildcardmatching/input.txt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Wildcardmatching/input.txt diff --git a/Wildcardmatching/input.txt b/Wildcardmatching/input.txt new file mode 100644 index 0000000..6cf9f77 --- /dev/null +++ b/Wildcardmatching/input.txt @@ -0,0 +1,23 @@ +Enter a pattern: a*b? +Enter a string (enter q to quit): +abcdefbh +abcdefbh matches the pattern: a*b? + +Enter a string (enter q to quit): +abf +abf matches the pattern: a*b? + +Enter a string (enter q to quit): +acdb +acdb doesn't match with the given pattern: a*b? + +Enter a string (enter q to quit): +ab +ab doesn't match with the given pattern: a*b? + +Enter a string (enter q to quit): +abfbfbfbf +abfbfbfbf matches the pattern: a*b? + +Enter a string (enter q to quit): +q From 9cdea43a2f1ea347a7acd8822a748a0b608ad980 Mon Sep 17 00:00:00 2001 From: priya Date: Sun, 18 Oct 2020 16:08:44 +0530 Subject: [PATCH 3/3] added wildcards to Algorithms --- Wildcardmatching/WildCard.java | 77 ++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Wildcardmatching/WildCard.java diff --git a/Wildcardmatching/WildCard.java b/Wildcardmatching/WildCard.java new file mode 100644 index 0000000..5e3b1ee --- /dev/null +++ b/Wildcardmatching/WildCard.java @@ -0,0 +1,77 @@ + +import java.util.Scanner; +public class WildCard +{ + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + System.out.print("Enter a pattern: "); + String p=sc.nextLine(); + while(true) + { + System.out.println("Enter a string (enter q to quit): "); + String string =sc.nextLine(); + if(string.equals("q")) + break; + if(new WildCard().match(string, p)) + System.out.println(string +" matches the pattern: "+ p+"\n"); + + else + System.out.println(string+ " doesn't match with the given pattern: "+p+"\n"); + } + sc.close(); + } + + public boolean match(String S, String P) + { + char[] string = S.toCharArray(); + char[] pattern = P.toCharArray(); + int m = string.length; + int n = pattern.length; + int pos = 0; + boolean isFirst = true; + + for ( int i = 0 ; i < pattern.length; i++) //Replacing multiple * with single * + { + if (pattern[i] == '*') + { + if (isFirst) + { + pattern[pos++] = pattern[i]; + isFirst = false; + } + } + else + { + pattern[pos++] = pattern[i]; + isFirst = true; + } + } + + boolean T[][] = new boolean[m + 1][n + 1]; + + if (pos > 0 && pattern[0] == '*') + T[0][1] = true; + + T[0][0] = true; + + for (int i = 1; i < T.length; i++) + { + for (int j = 1; j < T[0].length; j++) + { + if (pattern[j-1] == '?' || string[i-1] == pattern[j-1]) + { + T[i][j] = T[i-1][j-1]; + } + else if (pattern[j-1] == '*') + { + T[i][j] = T[i-1][j] || T[i][j-1]; + } + } + } + + return T[m][pos]; + } + + +}