Sumit's String

Given a string 'S' , u need to tell whether it is 'sumit's string or not'.

A string is called 'Sumit's String' , if the distance between the adjacent character is 1.

Consider that the alphabets are arranged in a cyclic manner from 'a' to 'z'. distance between any character 'x' and 'y' will be defined as a minimum number of steps it takes 'x' to reach 'y'. Here, character 'x' can start moving clockwise or anti-clockwise in order to reach at the position where the character 'y' is placed.

Print 'YES' if it is Sumit's string else print 'NO'.

Example:

Input:  s = "bcd"
Output: YES

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void distanceString(string s)
{
    int i = 0;
    int flag = 0;
    while (i < s.size() - 1)
    {
        if (s[i] == s[i + 1])
        {
            flag = 1;
            break;
        }
        if (s[i] == 'z')
        {
            if (s[i + 1] != 'a' && s[i + 1] != 'y')
            {
                flag = 1;
                break;
            }
        }
        else if (s[i + 1] == 'z')
        {
            if (s[i] != 'a' && s[i] != 'y')
            {
                flag = 1;
                break;
            }
        }
        else if (abs(s[i] - s[i + 1]) != 1)
        {
            flag = 1;
            break;
        }
        i++;
    }
    if (flag)
        cout << "NO\n";
    else
        cout << "YES\n";
}
int main()
{

    string s = "bcd";

    distanceString(s);

    return 0;
}


No comments:

Post a Comment