text: Check if the string contains consecutive letters and each letter occurs exactly once | C ++ program to implement the above approach ; Function to check if the condition holds ; Get the length of the string ; sort the given string ; Iterate for every index and check for the condition ; If are not consecutive ; Driver code ; 1 st example ; 2 nd example
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s ) { int l = s . length ( ) ; sort ( s . begin ( ) , s . end ( ) ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( s [ i ] - s [ i - 1 ] != 1 ) return false ; } return true ; } int main ( ) { string str = " dcef " ; if ( check ( str ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; str = " xyza " ; if ( check ( str ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }