text: Check if the string contains consecutive letters and each letter occurs exactly once | Java 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
code: public class GFG { public static boolean check ( String str ) { int min = Integer . MAX_VALUE ; int max = Integer . MIN_VALUE ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int ascii = ( int ) str . charAt ( 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 ; } public static void main ( String [ ] args ) { String str = " dcef " ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str1 = " xyza " ; if ( check ( str1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }