A string and a pattern are given to you. You have to match the pattern and string without using regular expression
belongs to collection: interview C++ coding problems/challenges | Backtracking
All Answers
total answers (1)
belongs to collection: interview C++ coding problems/challenges | Backtracking
total answers (1)
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