- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if Words in given Sentence Occur based on the given Pattern
In this problem, we need to check whether the string follows the given pattern. We can solve the problem by mapping each character to the word. If any character of the pattern maps more than one word, we can say that the string is not following the pattern.
Problem statement – We have given a ‘pat’ string containing N characters and an ‘alpha’ string containing N words. The given task is to check whether the ‘alpha’ string follows the pattern of the ‘pat’ string.
Note – We can say that the ‘alpha’ string follows the pattern when the word matches the character of ‘pat’ in order.
Sample examples
Input
pat = "ana", alpha = "Hello hi Hello";
Output
‘Yes’
Explanation – The given string follows the pattern because a -> ‘Hello’, b -> ‘hi’.
Input
pat = "aba"; alpha = "Welcome to Tutorialspoint!";
Output
‘No’
Explanation – Let’s look at the character mappings with strings’ words.
a - > ‘Welcome’
b -> ‘to’
a -> ‘Tutorialspoint’
Here, ‘a’ is mapped to more than one word, so we can say that string is not following the pattern.
Input
pat = "bbb"; alpha = "orange orange orange";
Output
‘Yes’
Explanation – The string follows the pattern
Approach 1
In this approach, we need to find the pattern followed by the ‘pat’ and ‘alpha’ strings. After that, we can compare both patterns and ensure whether the pattern of the ‘pat’ and ‘alpha’ string matches.
Algorithm
Step 1 – Initialize the ‘words’ map to map the number value to the string words, ‘charPointer’ to point the string characters, ‘tempStr’ to store the word, and ‘str’ to store the word pattern in the string.
Step 2 – Start traversing the ‘alpha’ string.
Step 3 – If the current character is space, follow the below steps. Else, append a character to the ‘tempStr’ string.
Step 4 – If the ‘tempStr’ string is not empty and does not exist in the map, insert the ‘tempStr’ and ‘charPointer’ values to the map after incrementing it by 1.
Step 5 – If the ‘tempStr’ exists in the map, take the mapped number value and append the related character value to the ‘str’ string.
Step 6 – Also, reinitialize the ‘tempStr’ with an empty string.
Step 7 – Handle the last word of the string when iterations are completed.
Step 8 – Initialize the ‘patternMap’ map to store the pattern of the ‘pat’ string’s characters and ‘final_pat’ to store the pattern.
Step 9 – Iterate the ‘pat’ string. If the string character doesn’t exist in the map, insert the character with the ‘charPointer’ value.
Step 10 – Next, get the mapped value related to the character from the map and append it to the ‘final_pat’ string.
Step 11 – At last, return true if final_pat and str string match. Otherwise, return false.
Example
#include <bits/stdc++.h> using namespace std; bool isPatternExists(string pat, string alpha) { // To store words of the string map<string, int> words; int charPointer = -1; string tempStr = ""; string str = ""; // Start traversing the string for (int a = 0; a < alpha.length(); a++) { // If we get the space, the word is completed if (alpha[a] == ' ') { // If a word does not exist in the map, add it if (!tempStr.empty() && words.find(tempStr) == words.end()) { words.insert({tempStr, ++charPointer}); } if (words.find(tempStr) != words.end()) { // If word exists str += ((char)((words.find(tempStr))->second + 'a')); } // Re-initialization tempStr = ""; } else { // Get word tempStr += alpha[a]; } } // Handling the last word if (!tempStr.empty() && words.find(tempStr) == words.end()) words.insert({tempStr, ++charPointer}); if (words.find(tempStr) != words.end()) str += ((char)((words.find(tempStr))->second + 'a')); map<char, int> patternMap; charPointer = -1; string final_pat = ""; // Create the mapping for the pattern for (int a = 0; a < pat.length(); a++) { // Insert char to map if (patternMap.find(pat[a]) == patternMap.end()) patternMap.insert({pat[a], ++charPointer}); // If character already exists if (patternMap.find(pat[a]) != patternMap.end()) final_pat += ((char)((patternMap.find(pat[a]))->second + 'a')); } return final_pat == str; } int main() { string pat = "ana"; string alpha = "Hello hi Hello"; if (isPatternExists(pat, alpha)) { cout << "The string follows the given pattern"; } else { cout << "The string does not follow the given pattern"; } return 0; }
Output
The string follows the given pattern
Time complexity – O(NlogN), as we search in the map.
Space complexity – O(N), as we use the map data structure.
In the solution, we have created and matched a generalized pattern of both strings. However, programmers may solve the problem without creating the generalized pattern but need to cross-check that a single character should not be mapped to different words, and a single word should not be mapped to the different characters.