Match a pattern and String without using regular expressions
A string and a pattern are given to you. You have to match the pattern and string without using regular expression.
Input:
Test Case T.
T no. of lines with the strings and patterns.
E.g.
3
abcbbabc
aba
Includehelp
aa
GreenRedYellowRed
GRYR
Output:
Print the character with their respectively representation.
Example
T= 3
Input:
String : abcbbabc
Pattern: aba
Output:
a → abc
b → bb
Input:
String : Includehelp
Pattern: aa
Output:
Matching not possible.
Input:
String : GreenRedYellowRed
Pattern: GRYR
Output:
G → Green
R → Red
Y → Yellow
Match a string with a pattern is a problem of combination and we will solve this problem with the backtracking process.
To solve this problem, we will follow this algorithm,
match(string ,pattern ,string_pointer,patter_pointer,map,set): if(string_pointer equals to the null pointer of the string and pattern_pointer equals to the null pointer of the pattern) Then return true end if if(string_pointer equals to the null pointer of the string or pattern_pointer equals to the null pointer of the pattern) Then return false end if for i-> string_pointer+1 to string_length taking a substring of the original from string_pointer to i if(the substring is not in map)Then if(pattern is already present in the set) Then continue end if insert the pair of substring and the corresponding pattern into the map insert the pattern into the set end if else if(the substring is already map) Then if(substring's corresponding pattern is not matched with the pattern which is at the patter_pointer) Then continue; end if end if if(call the pattern for string_pointer+1 and patter_pointer+1) return true; end for return false;C++ implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer