C++ code to check string is diverse or not

Suppose we have a string S with n lowercase letters. A string is called diverse if it has consecutive letters of the English alphabet and each letter occurs exactly once. (letters 'a' and 'z' are not adjacent). We have to check whether it is diverse or not.

So, if the input is like S = "fced", then the output will be True.

Steps

To solve this, we will follow these steps −

sort the array S
flag := 1
for initialize i := 1, when i 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
bool solve(string S){
   sort(S.begin(), S.end());
   int flag = 1;
   for (int i = 1; i 

Input

"fced"

Output

1
Updated on: 2022-03-15T05:12:34+05:30

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements