The Pattern Matcher | Practice | GeeksforGeeks

All right! Let's implement some pattern-matching using CPP strings.
You are given a string s of x and y. You need to verify whether the string follows the pattern xnyn. That is the string is valid only if equal number of ys follow equal number of xs.As an example: xxyyxxyy is valid. xy is valid. xxyyx is invalid. xxxyyyxxyyxy is valid.
Example 1:
Input:s = xxyyOutput:1
Example 2:
Input:s = xyxOutput:0
Your Task:Since this is a function problem, you don't need to take any input. Just complete the function follPatt(string s) that outputs 1 if string is valid, else it outputs 0. In a new line, print 1 if the string is valid else print 0.Expected Time Complexity:O(|s|)Expected Auxilary Space:O(1)
Constraints:1 <= |s| <=100
0
hgaur7013 weeks ago
TIME TAKEN : 0.1 , TIME COMPLEXITY : O(n);  AUXILARY CASE : O(1) , USING STACK CONTAINER.
 
void follPatt(string s)
    {
        stack<char> s1;
        int n = s.length();
        int ans = 1, i = 0;

        if (n % 2 != 0)
        {
            cout << 0 << endl;
            return;
        }
        else if (s.at(0) == 'y')
        {
            cout << 0 << endl;
            return;
        }

        while (i < n)
        {

            while (s.at(i) == 'x')
            {
                s1.push(s.at(i));
                i++;
                if (i == n)
                {
                    cout << 0 << endl;
                    return;
                }
            }
            while (s.at(i) == 'y')
            {
                if (s1.empty() == true)
                {
                    cout << 0 << endl;
                    return;
                }
                s1.pop();
                i++;

                if (i == n)
                {
                    break;
                }
            }
            if (s1.empty() != true)
            {
                ans = 0;
            }
        }
        cout << ans << endl;
        return;
    }
+2
badgujarsachin833 months ago
  void follPatt(string s)
    {
       //Your code here
       int n=s.size();
       int count=0,i=0,ans=1;
       while(i<n){
           while(s[i]=='x'){
               count++;
               i++;
           }
           while(s[i]=='y'){
               count--;
               i++;
           }
           if(count!=0){
               ans=0;
           }
       }
       cout<<ans<<endl;
    }
0
mayank20213 months ago
C++void follPatt(string s)   {       if (s.size()&1)         {             cout<<"0"<<endl;          return;         }              int countx=0, county=0, flag=0;           for(int i=0; i<s.size(); i++)      {             if( flag==1 && (countx == county))                {                    countx=0;                    county=0;                    flag=0;                }                else if(flag==1)                {                    cout<<"0"<<endl;                    return;                }                    if(s[i]=='x')           {                countx++;           }          else if(s[i]=='y')          {              county++;              if( (i==s.size()-1) && (countx != county))               {                   cout<<"0"<<endl;                    return;                                  }               else if (s[i+1] !='y')                {                     flag=1;                }          }      }            cout<<"1"<<endl;   }
0
darshitagupta3 months ago
void follPatt(string s)   {      //Your code here      int count=0;      int n=s.size();      int i=0;      int ans =1;              while(i<n)      {         while(s[i]=='x')          {          count ++;          i++;          }         while(s[i]=='y')          {              count--;              i++;          }          if(count!=0)          ans=0;      }      cout<<ans;            cout<<endl;   }
0
_luffy_3 months ago
O(|s|) solution.
 
int countX=0;
       int ans = 1;
       int i=0;
       while(i<s.size())
       {
           while(s[i] == 'x')
           {
                countX++;
                i++;
           }
            while(s[i] == 'y')
            {
                countX--;
                i++;
            }
            if(countX!=0)
                ans = 0;
       }
       cout<<ans;
0
Rishi Verma10 months ago
Rishi Verma
Correct Answer. Execution Time:0.01https://uploads.disquscdn.c...
0
Aditya Sharan
This comment was deleted.
0
Aditya Sharan
This comment was deleted.
0
Aditya Sharan
This comment was deleted.
0
Aditya Sharan
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.