TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
wildcard_matching.cpp File Reference

Implementation of the Wildcard Matching problem. More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for wildcard_matching.cpp:

Go to the source code of this file.

Namespaces

namespace  backtracking
 for vector container
 
namespace  wildcard_matching
 Functions for the Wildcard Matching problem.
 

Functions

std::vector< std::vector< int64_t > > backtracking::wildcard_matching::dpTable (1000, std::vector< int64_t >(1000, -1))
 The main function implements if pattern can be matched with given string.
 
bool backtracking::wildcard_matching::wildcard_matching (std::string s, std::string p, uint32_t pos1, uint32_t pos2)
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of the Wildcard Matching problem.

Given a matching string and a pattern, implement wildcard pattern matching with support for ? and *. ? matches any single character. * matches any sequence of characters (including the empty sequence). The matching should cover the entire matching string (not partial). The task is to determine if the pattern matches with the matching string

Author
Swastika Gupta

Definition in file wildcard_matching.cpp.

Function Documentation

◆ dpTable()

std::vector< std::vector< int64_t > > backtracking::wildcard_matching::dpTable ( 1000 ,
std::vector< int64_t > 1000, -1 )

The main function implements if pattern can be matched with given string.

Parameters
sis the given matching string
pis the given pattern
pos1is the starting index
pos2is the last index
Returns
1 if pattern matches with matching string otherwise 0

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 153 of file wildcard_matching.cpp.

153 {
154 test(); // run self-test implementations
155 return 0;
156}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 102 of file wildcard_matching.cpp.

102 {
103 // 1st test
104 std::cout << "1st test ";
105 std::string matching1 = "baaabab";
106 std::string pattern1 = "*****ba*****ab";
107 assert(backtracking::wildcard_matching::wildcard_matching(matching1,
108 pattern1, 0, 0) ==
109 1); // here the pattern matches with given string
110 std::cout << "passed" << std::endl;
111
112 // 2nd test
113 std::cout << "2nd test ";
114 std::string matching2 = "baaabab";
115 std::string pattern2 = "ba*****ab";
116 assert(backtracking::wildcard_matching::wildcard_matching(matching2,
117 pattern2, 0, 0) ==
118 1); // here the pattern matches with given string
119 std::cout << "passed" << std::endl;
120
121 // 3rd test
122 std::cout << "3rd test ";
123 std::string matching3 = "baaabab";
124 std::string pattern3 = "ba*ab";
125 assert(backtracking::wildcard_matching::wildcard_matching(matching3,
126 pattern3, 0, 0) ==
127 1); // here the pattern matches with given string
128 std::cout << "passed" << std::endl;
129
130 // 4th test
131 std::cout << "4th test ";
132 std::string matching4 = "baaabab";
133 std::string pattern4 = "a*ab";
134 assert(backtracking::wildcard_matching::wildcard_matching(matching4,
135 pattern4, 0, 0) ==
136 1); // here the pattern matches with given string
137 std::cout << "passed" << std::endl;
138
139 // 5th test
140 std::cout << "5th test ";
141 std::string matching5 = "baaabab";
142 std::string pattern5 = "aa?ab";
143 assert(backtracking::wildcard_matching::wildcard_matching(matching5,
144 pattern5, 0, 0) ==
145 1); // here the pattern matches with given string
146 std::cout << "passed" << std::endl;
147}

◆ wildcard_matching()

bool backtracking::wildcard_matching::wildcard_matching ( std::string s,
std::string p,
uint32_t pos1,
uint32_t pos2 )

Definition at line 40 of file wildcard_matching.cpp.

41 {
42 uint32_t n = s.length();
43 uint32_t m = p.length();
44 // matching is successfull if both strings are done
45 if (pos1 == n && pos2 == m) {
46 return true;
47 }
48
49 // matching is unsuccessfull if pattern is not finished but matching string
50 // is
51 if (pos1 != n && pos2 == m) {
52 return false;
53 }
54
55 // all the remaining characters of patterns must be * inorder to match with
56 // finished string
57 if (pos1 == n && pos2 != m) {
58 while (pos2 < m && p[pos2] == '*') {
59 pos2++;
60 }
61
62 return pos2 == m;
63 }
64
65 // if already calculted for these positions
66 if (dpTable[pos1][pos2] != -1) {
67 return dpTable[pos1][pos2];
68 }
69
70 // if the characters are same just go ahead in both the string
71 if (s[pos1] == p[pos2]) {
72 return dpTable[pos1][pos2] =
73 wildcard_matching(s, p, pos1 + 1, pos2 + 1);
74 }
75
76 else {
77 // can only single character
78 if (p[pos2] == '?') {
79 return dpTable[pos1][pos2] =
80 wildcard_matching(s, p, pos1 + 1, pos2 + 1);
81 }
82 // have choice either to match one or more charcters
83 else if (p[pos2] == '*') {
84 return dpTable[pos1][pos2] =
85 wildcard_matching(s, p, pos1, pos2 + 1) ||
86 wildcard_matching(s, p, pos1 + 1, pos2);
87 }
88 // not possible to match
89 else {
90 return dpTable[pos1][pos2] = 0;
91 }
92 }
93}
Functions for the Wildcard Matching problem.
std::vector< std::vector< int64_t > > dpTable(1000, std::vector< int64_t >(1000, -1))
The main function implements if pattern can be matched with given string.