Given an input string and a pattern, check if characters in the input string follows the same order as determined by characters present in the pattern. Assume there won’t be any duplicate characters in the pattern.
Examples:
Input: string = "engineers rock", pattern = "er";
Output: true
All 'e' in the input string are before all 'r'.
Input: string = "engineers rock", pattern = "egr";
Output: false
There are two 'e' after 'g' in the input string.
Input: string = "engineers rock", pattern = "gsr";
Output: false
There are one 'r' before 's' in the input string.
We have discussed two approaches to solve this problem.
Check if string follows order of characters defined by a pattern or not | Set 1
Check if string follows order of characters defined by a pattern or not | Set 2
In this approach we first assign a label (or order) to characters of pattern. The labels are assigned in increasing order.
For example, the pattern “gsr” is labeled as following
"g" => 1
"s" => 2
"r" => 3
It means ‘g’ will come first, then ‘s’, then ‘r’
After assigning labels to pattern characters, we iterate through string characters. While traversing, we keep track of label (or order) of last visited character. If label of current character is less than previous character, we return false. Otherwise we update last label. If all characters follow order, we return true.
Below is the implementation
C++
#include <bits/stdc++.h>
using namespace std;
const int CHAR_SIZE = 256;
bool checkPattern(string str, string pat)
{
vector< int > label(CHAR_SIZE, -1);
int order = 1;
for ( int i = 0; i < pat.length() ; i++)
{
label[pat[i]] = order;
order++;
}
int last_order = -1;
for ( int i = 0; i < str.length(); i++)
{
if (label[str[i]] != -1)
{
if (label[str[i]] < last_order)
return false ;
last_order = label[str[i]];
}
}
return true ;
}
int main()
{
string str = "engineers rock" ;
string pattern = "gsr" ;
cout << boolalpha << checkPattern(str, pattern);
return 0;
}
|
Java
class GFG
{
static int CHAR_SIZE = 256 ;
static boolean checkPattern(String str,
String pat)
{
int [] label = new int [CHAR_SIZE];
for ( int i = 0 ; i < CHAR_SIZE; i++)
label[i] = - 1 ;
int order = 1 ;
for ( int i = 0 ; i < pat.length(); i++)
{
label[pat.charAt(i)] = order;
order++;
}
int last_order = - 1 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (label[str.charAt(i)] != - 1 )
{
if (label[str.charAt(i)] < last_order)
return false ;
last_order = label[str.charAt(i)];
}
}
return true ;
}
public static void main(String[] args)
{
String str = "engineers rock" ;
String pattern = "gsr" ;
System.out.println(checkPattern(str, pattern));
}
}
|
Python3
CHAR_SIZE = 256
def checkPattern( Str , pat):
label = [ - 1 ] * CHAR_SIZE
order = 1
for i in range ( len (pat)):
label[ ord (pat[i])] = order
order + = 1
last_order = - 1
for i in range ( len ( Str )):
if (label[ ord ( Str [i])] ! = - 1 ):
if (label[ ord ( Str [i])] < last_order):
return False
last_order = label[ ord ( Str [i])]
return True
if __name__ = = '__main__' :
Str = "engineers rock"
pattern = "gsr"
print (checkPattern( Str , pattern))
|
C#
using System;
class GFG
{
static int CHAR_SIZE = 256;
static bool checkPattern(String str,
String pat)
{
int [] label = new int [CHAR_SIZE];
for ( int i = 0; i < CHAR_SIZE; i++)
label[i] = -1;
int order = 1;
for ( int i = 0; i < pat.Length; i++)
{
label[pat[i]] = order;
order++;
}
int last_order = -1;
for ( int i = 0; i < str.Length; i++)
{
if (label[str[i]] != -1)
{
if (label[str[i]] < last_order)
return false ;
last_order = label[str[i]];
}
}
return true ;
}
public static void Main(String[] args)
{
String str = "engineers rock" ;
String pattern = "gsr" ;
Console.WriteLine(checkPattern(str, pattern));
}
}
|
Javascript
<script>
let CHAR_SIZE = 256;
function checkPattern(str,pat)
{
let label = new Array(CHAR_SIZE);
for (let i = 0; i < CHAR_SIZE; i++)
label[i] = -1;
let order = 1;
for (let i = 0; i < pat.length; i++)
{
label[pat[i].charCodeAt(0)] = order;
order++;
}
let last_order = -1;
for (let i = 0; i < str.length; i++)
{
if (label[str[i].charCodeAt(0)] != -1)
{
if (label[str[i].charCodeAt(0)] < last_order)
return false ;
last_order = label[str[i].charCodeAt(0)];
}
}
return true ;
}
let str = "engineers rock" ;
let pattern = "gsr" ;
document.write(checkPattern(str, pattern));
</script>
|
Time Complexity of this program is O(n) with constant extra space (the array label is of constant size, 256).
Auxiliary Space: O(256).
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Mar, 2023
Like Article
Save Article