Check if the characters of a given string are in alphabetical order | Java implementation of above approach ; Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver Code ; check whether the string is in alphabetical order or not
import java . util . Arrays ; public class GFG { static boolean isAlphabaticOrder ( String s ) { int n = s . length ( ) ; char c [ ] = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = s . charAt ( i ) ; } Arrays . sort ( c ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c [ i ] != s . charAt ( i ) ) return false ; return true ; } public static void main ( String args [ ] ) { String s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }