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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if a string follows order
// defined by a given pattern.
#include <bits/stdc++.h>
using namespace std;
  
const int CHAR_SIZE = 256;
  
// Returns true if characters of str follow
// order defined by a given ptr.
bool checkPattern(string str, string pat)
{
    // Initialize all orders as -1
    vector<int> label(CHAR_SIZE, -1);
  
    // Assign an order to pattern characters
    // according to their appearance in pattern
    int order = 1;
    for (int i = 0; i < pat.length() ; i++)
    {
        // give the pattern characters order
        label[pat[i]] = order;
  
        // increment the order
        order++;
    }
  
    //  Now one by check if string characters
    // follow above order
    int last_order = -1;
    for (int i = 0; i < str.length(); i++)
    {
        if (label[str[i]] != -1)
        {
            // If order of this character is less
            // than order of previous, return false.
            if (label[str[i]] < last_order)
                return false;
  
            // Update last_order for next iteration
            last_order =  label[str[i]];
        }
    }
  
    // return that str followed pat
    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 to find if a string follows order 
// defined by a given pattern.
class GFG 
{
    static int CHAR_SIZE = 256;
  
    // Returns true if characters of str follow
    // order defined by a given ptr.
    static boolean checkPattern(String str,
                                String pat)
    {
        int[] label = new int[CHAR_SIZE];
  
        // Initialize all orders as -1
        for (int i = 0; i < CHAR_SIZE; i++)
            label[i] = -1;
  
        // Assign an order to pattern characters
        // according to their appearance in pattern
        int order = 1;
        for (int i = 0; i < pat.length(); i++) 
        {
  
            // give the pattern characters order
            label[pat.charAt(i)] = order;
  
            // increment the order
            order++;
        }
  
        // Now one by check if string characters
        // follow above order
        int last_order = -1;
        for (int i = 0; i < str.length(); i++)
        {
            if (label[str.charAt(i)] != -1)
            {
  
                // If order of this character is less
                // than order of previous, return false.
                if (label[str.charAt(i)] < last_order)
                    return false;
  
                // Update last_order for next iteration
                last_order = label[str.charAt(i)];
            }
        }
  
        // return that str followed pat
        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 is contributed by
// sanjeev2552

chevron_right


Output:

false

Time Complexity of this program is O(n) with constant extra space (the array label is of constant size, 256).

This article is contributed by Mohammed Iftekharul Islam Rupam. 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 : sanjeev2552



Article Tags :
Practice Tags :


1


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