C++ code to check array can be formed from Equal Not-Equal sequence or not

C++Server Side ProgrammingProgramming

Suppose we have a string S of length . Consider there are n numbers and they are arranged in a circle. We do not know the values of these numbers but if S[i] = 'E' it indicates ith and (i+1)th numbers are same, but if that is 'N' then they are different. From S we have to check whether we can recreate the sequence or not.

So, if the input is like S = "ENNEENE", then the output will be True, because we can assign values like [15,15,4,20,20,20,15].

Steps

To solve this, we will follow these steps −

if S has single 'N', then:
   return false
return true

Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h>
using namespace std;
bool solve(string S){
   if (count(S.begin(), S.end(), 'N') == 1)
      return false;
   return true;
}
int main(){
   string S = "ENNEENE";
   cout << solve(S) << endl;
}

Input

"ENNEENE"

Output

1
raja
Updated on 11-Mar-2022 07:02:20

Advertisements