Check if the string contains consecutive letters and each letter occurs exactly once | Java 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
import java . util . * ; class GfG { static boolean check ( char s [ ] ) { int l = s . length ; Arrays . sort ( s ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( s [ i ] - s [ i - 1 ] != 1 ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " dcef " ; if ( check ( str . toCharArray ( ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str1 = " xyza " ; if ( check ( str1 . toCharArray ( ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }