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
Explanation:
All 'e' in the input string are before all 'r'.
Input:
string = "engineers rock"
pattern = "egr";
Output: false
Explanation:
There are two 'e' after 'g' in the input string.
Input:
string = "engineers rock"
pattern = "gsr";
Output: false
Explanation:
There are one 'r' before 's' in the input string.
The idea is very simple. For every pair (x, y) of consecutive characters in the pattern string, we find the last occurrence of x and first occurrence of y in the input string. If last occurrence of character x is after first occurrence of character y for any pair, we return false. Checking for every pair of consecutive characters in the pattern string will suffice. For example, if we consider three consecutive characters in the pattern say x, y and z, if (x, y) and (y, z) returns true, that implies (x, z) is also true.
Below is the implementation of above idea
C++
#include <iostream>
using namespace std;
bool checkPattern(string str, string pattern)
{
int len = pattern.length();
if (str.length() < len)
return false ;
for ( int i = 0; i < len - 1; i++)
{
char x = pattern[i];
char y = pattern[i + 1];
size_t last = str.find_last_of(x);
size_t first = str.find_first_of(y);
if (last == string::npos || first ==
string::npos || last > first)
return false ;
}
return true ;
}
int main()
{
string str = "engineers rock" ;
string pattern = "gsr" ;
cout << boolalpha << checkPattern(str, pattern);
return 0;
}
|
Java
class GFG
{
static boolean checkPattern(String str, String pattern)
{
int len = pattern.length();
if (str.length() < len)
{
return false ;
}
for ( int i = 0 ; i < len - 1 ; i++)
{
char x = pattern.charAt(i);
char y = pattern.charAt(i + 1 );
int last = str.lastIndexOf(x);
int first = str.indexOf(y);
if (last == - 1 || first
== - 1 || last > first)
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String str = "engineers rock" ;
String pattern = "gsr" ;
System.out.println(checkPattern(str, pattern));
}
}
|
Python3
def checkPattern(string, pattern):
l = len (pattern)
if len (string) < l:
return False
for i in range (l - 1 ):
x = pattern[i]
y = pattern[i + 1 ]
last = string.rindex(x)
first = string.index(y)
if last = = - 1 or first = = - 1 or last > first:
return False
return True
if __name__ = = "__main__" :
string = "engineers rock"
pattern = "gsr"
print (checkPattern(string, pattern))
|
C#
using System;
class GFG
{
static Boolean checkPattern(String str, String pattern)
{
int len = pattern.Length;
if (str.Length < len)
{
return false ;
}
for ( int i = 0; i < len - 1; i++)
{
char x = pattern[i];
char y = pattern[i+1];
int last = str.LastIndexOf(x);
int first = str.IndexOf(y);
if (last == -1 || first
== -1 || last > first)
{
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
String str = "engineers rock" ;
String pattern = "gsr" ;
Console.WriteLine(checkPattern(str, pattern));
}
}
|
Javascript
<script>
function checkPattern(str, pattern) {
var len = pattern.length;
if (str.length < len) {
return false ;
}
for ( var i = 0; i < len - 1; i++) {
var x = pattern[i];
var y = pattern[i + 1];
var last = str.lastIndexOf(x);
var first = str.indexOf(y);
if (last === -1 || first === -1 || last > first) {
return false ;
}
}
return true ;
}
var str = "engineers rock" ;
var pattern = "gsr" ;
document.write(checkPattern(str, pattern));
</script>
|
Time Complexity: O(M * N), here M is length of string pattern and N is length of string str as we used str.find_last_of() whose time complexity isO(N).
Space Complexity: O(1), since we not used any extra space.
We have discussed two more approaches to solve this problem.
Check if string follows order of characters defined by a pattern or not | Set 2
Check if string follows order of characters defined by a pattern or not | Set 3
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