Check if string follows order of characters defined by a pattern or not | Set 1
Last Updated :
20 Mar, 2023
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++
// C++ program check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
#include <iostream>
using namespace std;
// Function to check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
bool checkPattern(string str, string pattern)
{
// len stores length of the given pattern
int len = pattern.length();
// if length of pattern is more than length of
// input string, return false;
if (str.length() < len)
return false;
for (int i = 0; i < len - 1; i++)
{
// x, y are two adjacent characters in pattern
char x = pattern[i];
char y = pattern[i + 1];
// find index of last occurrence of character x
// in the input string
size_t last = str.find_last_of(x);
// find index of first occurrence of character y
// in the input string
size_t first = str.find_first_of(y);
// return false if x or y are not present in the
// input string OR last occurrence of x is after
// the first occurrence of y in the input string
if (last == string::npos || first ==
string::npos || last > first)
return false;
}
// return true if string matches the pattern
return true;
}
// Driver code
int main()
{
string str = "engineers rock";
string pattern = "gsr";
cout << boolalpha << checkPattern(str, pattern);
return 0;
}
Java
// Java program check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
class GFG
{
// Function to check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
static boolean checkPattern(String str, String pattern)
{
// len stores length of the given pattern
int len = pattern.length();
// if length of pattern is more than length of
// input string, return false;
if (str.length() < len)
{
return false;
}
for (int i = 0; i < len - 1; i++)
{
// x, y are two adjacent characters in pattern
char x = pattern.charAt(i);
char y = pattern.charAt(i + 1);
// find index of last occurrence of character x
// in the input string
int last = str.lastIndexOf(x);
// find index of first occurrence of character y
// in the input string
int first = str.indexOf(y);
// return false if x or y are not present in the
// input string OR last occurrence of x is after
// the first occurrence of y in the input string
if (last == -1 || first
== -1 || last > first)
{
return false;
}
}
// return true if string matches the pattern
return true;
}
// Driver code
public static void main(String[] args)
{
String str = "engineers rock";
String pattern = "gsr";
System.out.println(checkPattern(str, pattern));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program check if characters in
# the input string follows the same order
# as determined by characters present in
# the given pattern
# Function to check if characters in the
# input string follows the same order as
# determined by characters present
# in the given pattern
def checkPattern(string, pattern):
# len stores length of the given pattern
l = len(pattern)
# if length of pattern is more than length
# of input string, return false;
if len(string) < l:
return False
for i in range(l - 1):
# x, y are two adjacent characters in pattern
x = pattern[i]
y = pattern[i + 1]
# find index of last occurrence of
# character x in the input string
last = string.rindex(x)
# find index of first occurrence of
# character y in the input string
first = string.index(y)
# return false if x or y are not present
# in the input string OR last occurrence of
# x is after the first occurrence of y
# in the input string
if last == -1 or first == -1 or last > first:
return False
# return true if
# string matches the pattern
return True
# Driver Code
if __name__ == "__main__":
string = "engineers rock"
pattern = "gsr"
print(checkPattern(string, pattern))
# This code is contributed by
# sanjeev2552
C#
// C# program check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
using System;
class GFG
{
// Function to check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
static Boolean checkPattern(String str, String pattern)
{
// len stores length of the given pattern
int len = pattern.Length;
// if length of pattern is more than length of
// input string, return false;
if (str.Length < len)
{
return false;
}
for (int i = 0; i < len - 1; i++)
{
// x, y are two adjacent characters in pattern
char x = pattern[i];
char y = pattern[i+1];
// find index of last occurrence of character x
// in the input string
int last = str.LastIndexOf(x);
// find index of first occurrence of character y
// in the input string
int first = str.IndexOf(y);
// return false if x or y are not present in the
// input string OR last occurrence of x is after
// the first occurrence of y in the input string
if (last == -1 || first
== -1 || last > first)
{
return false;
}
}
// return true if string matches the pattern
return true;
}
// Driver code
public static void Main(String[] args)
{
String str = "engineers rock";
String pattern = "gsr";
Console.WriteLine(checkPattern(str, pattern));
}
}
/* This code contributed by PrinciRaj1992 */
JavaScript
<script>
// JavaScript program check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
// Function to check if characters in the input string
// follows the same order as determined by characters
// present in the given pattern
function checkPattern(str, pattern) {
// len stores length of the given pattern
var len = pattern.length;
// if length of pattern is more than length of
// input string, return false;
if (str.length < len) {
return false;
}
for (var i = 0; i < len - 1; i++) {
// x, y are two adjacent characters in pattern
var x = pattern[i];
var y = pattern[i + 1];
// find index of last occurrence of character x
// in the input string
var last = str.lastIndexOf(x);
// find index of first occurrence of character y
// in the input string
var first = str.indexOf(y);
// return false if x or y are not present in the
// input string OR last occurrence of x is after
// the first occurrence of y in the input string
if (last === -1 || first === -1 || last > first) {
return false;
}
}
// return true if string matches the pattern
return true;
}
// Driver code
var str = "engineers rock";
var pattern = "gsr";
document.write(checkPattern(str, pattern));
// This code is contributed by rdtank.
</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
Similar Reads
Check if string follows order of characters defined by a pattern or not | Set 3
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 t
7 min read
Check if string follows order of characters defined by a pattern or not | Set 2
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.Another solution to the same problem is posted here.Examples: Input: string = "enginee
8 min read
Check if a string follows a^nb^n pattern or not
Given string str, return true string follows pattern anbn, i.e., it has a's followed by b's such that the number of a's and b's are same. Examples: Input : str = "aabb" Output : Yes Input : str = "abab" Output : No Input : str = "aabbb" Output : No The idea is to first count a's. If number of a's is
8 min read
Check if characters of a string can be made non-decreasing by replacing '?'s
Given a string S of length N consisting of lowercase English alphabets and '?', the task is to check if it is possible to make the string non-decreasing by replacing all the '?' characters with English alphabets. If found to be true, print "Yes". Otherwise, print "No". Examples: Input: S = "abb?xy?"
6 min read
Check if strings are rotations of each other or not | Set 2
Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABA Output : True Input : GEEKS, EKSGE Output : True We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (long
6 min read
Check if String formed by first and last X characters of a String is a Palindrome
Given a string str and an integer X. The task is to find whether the first X characters of both string str and reversed string str are same or not. If it is equal then print true, otherwise print false. Examples: Input: str = abcdefba, X = 2Output: trueExplanation: First 2 characters of both string
5 min read
Check if a string can be split into substrings starting with N followed by N characters
Given a string str, the task is to check if it can be split into substrings such that each substring starts with a numeric value followed by a number of characters represented by that numeric integer. Examples: Input: str = "4g12y6hunter" Output: Yes Explanation: Substrings "4g12y" and "6hunter" sat
5 min read
Check if the characters in a string form a Palindrome in O(1) extra space
Given string str. The string may contain lower-case letters, special characters, digits, or even white spaces. The task is to check whether only the letters present in the string are forming a Palindromic combination or not without using any extra space. Note: It is not allowed to use extra space to
10 min read
Check if both halves of the string have same set of characters in Python
Given a string of lowercase characters only, the task is to check if it is possible to split a string from middle which will gives two halves having the same characters and same frequency of each character. If the length of the given string is ODD then ignore the middle element and check for the res
3 min read
Python | Check order of character in string using OrderedDict( )
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: trueExplanation: All
3 min read