Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Wildcardmatching/README.md
Original file line number Diff line number Diff line change
@@ -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.

77 changes: 77 additions & 0 deletions Wildcardmatching/WildCard.java
Original file line number Diff line number Diff line change
@@ -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];
}


}
23 changes: 23 additions & 0 deletions Wildcardmatching/input.txt
Original file line number Diff line number Diff line change
@@ -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