Subsequence matching

PROBLEM :

Given an all upper case string, check if it is a combination of one of the following:
1) R
2) RY
3) RYY

Input:

First line contains an integer T denoting the number of test cases. Each of the following T lines will contain an upper case string.

Output:

Print YES if the sequence is correct, NO if not correct.

Constraints:

1<=T<=1000
1<=10^5<= Size of String

Example:

Input:

3
RY
RWR
RRYY

Output:

YES
NO
YES
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
int Subsequence_matching(char *) ;
int main()
 {
int t,ans ;
char *str ;
str=(char*)malloc(100000*sizeof(char)) ;
cin>>t ;
while(t--)
{
   cin>>str ;
   ans=Subsequence_matching(str) ;
   if(ans)
       cout<<"YES" ;
   else
       cout<<"NO" ;
   cout<<endl ;
}
return 0;
}

int Subsequence_matching(char *str)
{
    int l,i ;
    l=strlen(str) ;
   
    for(i=0;i<l;i++)
    {
        if(str[i]=='R'&&str[i+1]=='Y'&&str[i+2]=='Y')
        {
            i+=2 ;
            continue ;
        }
       
        else if(str[i]=='R'&&str[i+1]=='Y')
        {
            i+=1 ;
            continue ;
        }
       
        else if(str[i]=='R')
            continue ;
       
        else
            break ;
    }
   
    if(i==l)
        return 1 ;
    else
        return 0 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )