
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Word Pattern II in C++
Suppose we have a pattern and a string called str, we have to check whether str follows the same pattern or not. Here pattern follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.
So, if the input is like pattern is "abaa", str is "orangegreenorangeorange", then the output will be true
To solve this, we will follow these steps −
Define a function solve(), this will take i, j, ptr, s, a map m, one set called used,
if i >= size of s and j >= size of ptr, then −
return true
if i >= size of s or j >= size of ptr, then −
return false
if ptr[j] is in m, then −
req := m[ptr[j]]
len := size of req
if len > size of s, then −
return false
if substring of s from index (i to len-1) is same as req and solve(i + len, j + 1, ptr, s, m, used), then −
return true
return false
Otherwise
x := ptr[j]
for initialize k := i, when k < size of s, update (increase k by 1), do −
temp := substring of s from index (i to k - i)
if temp is in used, then −
Ignore following part, skip to the next iteration
m[x] := temp
insert temp into used
if solve(k + 1, j + 1, ptr, s, m, used), then −
return true
delete x from m
delete temp from used
return false
From the main method do the following −
Define one map m
Define one set used
return solve(0, 0, ptr, s, m, used)
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: bool solve(int i, int j, string ptr, string s, map <char, string>& m, set<string>& used){ if (i >= s.size() && j >= ptr.size()) { return true; } if (i >= s.size() || j >= ptr.size()) return false; if (m.count(ptr[j])) { string req = m[ptr[j]]; int len = req.size(); if (len > s.size() - i) return false; if ((s.substr(i, len) == req) && solve(i + len, j + 1, ptr, s, m, used)) return true; return false; } else { char x = ptr[j]; for (int k = i; k < s.size(); k++) { string temp = s.substr(i, k - i + 1); ; if (used.count(temp)) continue; m[x] = temp; used.insert(temp); if (solve(k + 1, j + 1, ptr, s, m, used)) return true; m.erase(x); used.erase(temp); } } return false; } bool wordPatternMatch(string ptr, string s) { map<char, string> m; set<string> used; return solve(0, 0, ptr, s, m, used); } }; main(){ Solution ob; cout << (ob.wordPatternMatch("abaa", "orangegreenorangeorange")); }
Input
"abaa" "orangegreenorangeorange"
Output
1
- Related Articles
- Word Pattern in C++
- Word Break II in Python
- Word Search II in Python
- Shortest Word Distance II in C++
- How to match a particular word in a string using Pattern class in Java?
- Pattern pattern() method in Java with examples
- Fill in the blanks using the correct word given in brackets: (ii) All squares are ___________. (similar, congruent)
- ORDERBY word in MySQL?
- Tribonacci Word in C++
- Word Ladder in C++
- Word Break in Python
- Word Search in Python
- Word Squares in C++
- Word Abbreviation in C++
- Answer in a word:
