Good String | Practice | GeeksforGeeks

Given a string s of length N, you have to tell whether it is good or not. A good string is one where the distance between every two adjacent character is exactly 1. Here distance is defined by minimum distance between two character when alphabets from 'a' to 'z' are put in cyclic manner. For example distance between 'a' to 'c' is 2 and distance between 'a' to 'y' is also 2. The task is to print "YES" or "NO" (without quotes) depending on whether the given string is Good or not.
 
Example 1:
Input: s = "aaa"
Output: NO
Explanation: distance between 'a' and 'a' is not 1.
 
Example 2:
Input: s = "cbc"
Output: YES
Explanation: distance between 'b' and 'c' is 1.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function isGoodString() which accepts a string as input parameter and returns "YES" or "NO" (without quotes) accordingly.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
String contains only lower case english alphabets.
1 <= N <= 105
0
gurankitbehal781141 week ago
string isGoodString(string s){string a="YES";string b="NO";int j=0;   for(int i=0;i<s.length();i++){       if(s[i+1]-s[i]==1 || s[i+1]-s[i]==25 ||s[i+1]-s[i]==-25){           j++;       }   }   if(j==s.length()-1){       cout<<a;   }       else       cout<<b;}
0
saikirannagarjuna0072 months ago
class Solution {    String isGoodString(String s)    {        // code here       for(int i=1;i<s.length();i++){           if(s.charAt(i)==s.charAt(i-1)){               return "NO";           }       }       return "YES";   }} 
0
rayalravi20013 months ago
c++ solution
 int n = s.length();   for(int i=1; i<n; i++){       if((s[i]=='a' && s[i-1]=='z') ||(s[i]=='z' && s[i-1]=='a')){          continue;       }       if(abs(s[i]-s[i-1]) != 1){          return "NO";          }   }   return "YES";
0
jacksparrowxsr3 months ago
string isGoodString(string s)
{
   int n = s.size();
   for(int i=0;i<n-1;i++) if(abs(s[i]-s[i+1])%24 != 1) return "NO";
   return "YES";
}
0
jacksparrowxsr
This comment was deleted.
+1
ashumishra8425 months ago
for i in range(len(s)-1):           if abs(ord(s[i])-ord(s[i+1]))!=1 and abs(ord(s[i])-ord(s[i+1]))!=25:               return "NO"       return "YES"
+1
badgujarsachin835 months ago
string isGoodString(string s)
{
    //code here.
    for(int i=0;i<s.size()-1;i++){
        if(s[i]==s[i+1]){
            return "NO";
        }
       if(s[i]!='a' && s[i+1]!='z'&&s[i]!='a'&& s[s.size()-1]!='z'&&s[i]!='z' && s[i+1]!='a'&&s[i]!='z'&& s[s.size()-1]!='a'){
            
       if(abs((s[i]-'0')-(s[i+1]-'0'))!=1){
            return "NO";
       }
       }
       
    }
    return "YES";
}
0
Sai Manaswini Reddy P3 years ago
Sai Manaswini Reddy P
https://uploads.disquscdn.c...
0
Dheeraj Varshney5 years ago
Dheeraj Varshney
not able to understand what is wrong with this code http://code.geeksforgeeks.o...gives error - output producing less number of line
0
ayush2277365 years ago
ayush227736
Wrong !! The first test case where your code failed:
Input:dcbabcbcdcdcbabcdcbcdcbazyzyxwxw
Its Correct output is:YES
And Your Output is:NO
how can this be possible!
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.