Check if string follows order of characters defined by a pattern or not | Set 1

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


Output:

false

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

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Rajput-Ji, princiraj1992



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.