Check if the string contains consecutive letters and each letter occurs exactly once | C # program to implement the above approach ; for all the characters of the string ; find the ascii value of the character ; check if if its a valid character , if not then return false ; calculate sum of all the characters ascii values ; find minimum ascii value from the string ; find maximum ascii value from the string ; To get the previous element of the minimum ASCII value ; take the expected sum from the above equation ; check if the expected sum is equals to the calculated sum or not ; Driver code ; 1 st example ; 2 nd example
using System ; class GFG { static bool check ( string str ) { int min = Int32 . MaxValue ; int max = Int32 . MinValue ; int sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { int ascii = ( int ) str [ i ] ; if ( ascii < 96 ascii > 122 ) return false ; sum += ascii ; if ( min > ascii ) min = ascii ; if ( max < ascii ) max = ascii ; } min -= 1 ; int eSum = ( ( max * ( max + 1 ) ) / 2 ) - ( ( min * ( min + 1 ) ) / 2 ) ; return sum == eSum ; } static void Main ( ) { string str = " dcef " ; if ( check ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; string str1 = " xyza " ; if ( check ( str1 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }