Check if the characters of a given string are in alphabetical order - GeeksforGeeks

13 May, 2021
Given a string ‘s’, the task is to find if the characters of the string are in alphabetical order. The string contains only lowercase characters.  
Examples: 
Input: Str = "aabbbcc"
Output: In alphabetical order

Input: Str = "aabbbcca"
Output: Not in alphabetical order
A simple approach: 
Store the string to a character array and sort the array.
If the characters in the sorted array are in the same order as the string then print ‘In alphabetical order ‘.
Print ‘Not in alphabetical order’ otherwise.
Below is the implementation of the above approach :  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function that checks whether// the string is in alphabetical// order or notbool isAlphabaticOrder(string s){    // length of the string    int n = s.length();     // create a character array    // of the length of the string    char c[n];     // assign the string    // to character array    for (int i = 0; i < n; i++) {        c[i] = s[i];    }     // sort the character array    sort(c, c + n);     // check if the character array    // is equal to the string or not    for (int i = 0; i < n; i++)        if (c[i] != s[i])            return false;             return true;   } // Driver codeint main(){    string s = "aabbbcc";     // check whether the string is    // in alphabetical order or not    if (isAlphabaticOrder(s))       cout << "Yes";    else       cout << "No";     return 0;}
// Java implementation of above approach // import  Arrays classimport java.util.Arrays; public class GFG {     // Function that checks whether    // the string is in alphabetical    // order or not    static boolean isAlphabaticOrder(String s)    {        // length of the string        int n = s.length();               // create a character array        // of the length of the string        char c[] = new char [n];               // assign the string        // to character array        for (int i = 0; i < n; i++) {            c[i] = s.charAt(i);        }               // sort the character array        Arrays.sort(c);               // check if the character array        // is equal to the string or not        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";                 // check whether the string is        // in alphabetical order or not        if (isAlphabaticOrder(s))           System.out.println("Yes");        else            System.out.println("No");               }    // This Code is contributed by ANKITRAI1}
# Python 3 implementation of above approach # Function that checks whether# the string is in alphabetical# order or notdef isAlphabaticOrder(s):         # length of the string    n = len(s)     # create a character array    # of the length of the string    c = [s[i] for i in range(len(s))]     # sort the character array    c.sort(reverse = False)     # check if the character array    # is equal to the string or not    for i in range(n):        if (c[i] != s[i]):            return False             return True # Driver codeif __name__ == '__main__':    s = "aabbbcc"     # check whether the string is    # in alphabetical order or not    if (isAlphabaticOrder(s)):        print("Yes")    else:        print("No") # This code is contributed by# Surendra_Gangwar
// C# implementation of above approach// import Arrays class using System; public class GFG{         // Function that checks whether    // the string is in alphabetical    // order or not    static bool isAlphabaticOrder(String s)    {        // length of the string        int n = s.Length;             // create a character array        // of the length of the string        char []c = new char [n];             // assign the string        // to character array        for (int i = 0; i < n; i++) {            c[i] = s[i];        }             // sort the character array        Array.Sort(c);             // check if the character array        // is equal to the string or not        for (int i = 0; i < n; i++)            if (c[i] != s[i])                return false;                     return true;        }         static public void Main (){        String s = "aabbbcc";                 // check whether the string is        // in alphabetical order or not        if (isAlphabaticOrder(s))        Console.WriteLine("Yes");        else            Console.WriteLine("No");             }}
<?php// PHP implementation of above approach // Function that checks whether// the string is in alphabetical// order or notFunction isAlphabaticOrder($s){         // length of the string    $n = strlen($s);     $c = array();         // assign the string    // to character array    for ($i = 0; $i < $n; $i++)    {        $c[$i] = $s[$i];    }     // sort the character array    sort($c);     // check if the character array    // is equal to the string or not    for ($i = 0; $i < $n; $i++)        if ($c[$i] != $s[$i])            return false;             return true;} // Driver code$s = "aabbbcc"; // check whether the string is// in alphabetical order or notif (isAlphabaticOrder($s))    echo "Yes";else    echo "No";     // This Code is contributed// by Shivi_Aggarwal?>
<script>//Javascript implementation of above approach // Function that checks whether// the string is in alphabetical// order or notfunction isAlphabaticOrder(s){    // length of the string    var n = s.length;     // create a character array    // of the length of the string    var c = new Array(n);     // assign the string    // to character array    for (var i = 0; i < n; i++) {        c[i] = s[i];    }     // sort the character array    c.sort();     // check if the character array    // is equal to the string or not    for (var i = 0; i < n; i++)        if (c[i] != s[i])            return false;             return true;   } s = "aabbbcc"; // check whether the string is// in alphabetical order or notif (isAlphabaticOrder(s))  document.write( "Yes");else  document.write( "No"); //This code is contributed by SoumikMondal</script>
Yes
 
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)Efficient approach: 
Run a loop from 1 to (n-1) (where n is the length of the string)
Check whether the element at index ‘i’ is less than the element at index ‘i-1’.
If yes, then print ‘In alphabetical order ‘.
Print ‘Not in alphabetical order’ otherwise.
Below is the implementation of the above approach 
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function that checks whether// the string is in alphabetical// order or notbool isAlphabaticOrder(string s){    int n = s.length();     for (int i = 1; i < n; i++) {         // if element at index 'i' is less        // than the element at index 'i-1'        // then the string is not sorted        if (s[i] < s[i - 1])           return false;    }     return true;} // Driver codeint main(){    string s = "aabbbcc";     // check whether the string is    // in alphabetical order or not    if (isAlphabaticOrder(s))       cout << "Yes";    else       cout << "No";     return 0;}
// Java implementation of above approachpublic class GFG { // Function that checks whether// the string is in alphabetical// order or not    static boolean isAlphabaticOrder(String s) {        int n = s.length();         for (int i = 1; i < n; i++) {             // if element at index 'i' is less            // than the element at index 'i-1'            // then the string is not sorted            if (s.charAt(i) < s.charAt(i - 1)) {                return false;            }        }         return true;    } // Driver code    static public void main(String[] args) {        String s = "aabbbcc";        // check whether the string is        // in alphabetical order or not        if (isAlphabaticOrder(s)) {            System.out.println("Yes");        } else {            System.out.println("No");        }    }}//This code is contributed by PrinciRaj1992
# Python 3 implementation of above approach # Function that checks whether# the string is in alphabetical# order or notdef isAlphabaticOrder(s):     n = len(s)     for i in range(1, n):         # if element at index 'i' is less        # than the element at index 'i-1'        # then the string is not sorted        if (s[i] < s[i - 1]) :            return False     return True # Driver codeif __name__ == "__main__":     s = "aabbbcc"     # check whether the string is    # in alphabetical order or not    if (isAlphabaticOrder(s)):        print("Yes")    else:        print("No")
// C# implementation of above approachusing System; public class GFG{     // Function that checks whether// the string is in alphabetical// order or notstatic bool isAlphabaticOrder(string s){    int n = s.Length;     for (int i = 1; i < n; i++) {         // if element at index 'i' is less        // than the element at index 'i-1'        // then the string is not sorted        if (s[i] < s[i - 1])        return false;    }     return true;} // Driver code    static public void Main (){    string s = "aabbbcc";    // check whether the string is    // in alphabetical order or not    if (isAlphabaticOrder(s))    Console.WriteLine ("Yes");        else    Console.WriteLine  ("No");    }}
<?php// PHP implementation of above approach // Function that checks whether// the string is in alphabetical// order or notfunction isAlphabaticOrder($s){    $n = strlen($s);    for ($i = 1; $i < $n; $i++)    {         // if element at index 'i' is less        // than the element at index 'i-1'        // then the string is not sorted        if ($s[$i] < $s[$i - 1])        return false;    }     return true;} // Driver code$s = "aabbbcc"; // check whether the string is// in alphabetical order or notif (isAlphabaticOrder($s))    echo "Yes";else    echo "No"; // This code is contributed// by Sach_Code?>
<script>// JavaScript implementation of above approach // Function that checks whether// the string is in alphabetical// order or notfunction isAlphabaticOrder( s){    let n = s.length;     for (let i = 1; i < n; i++) {        // if element at index 'i' is less        // than the element at index 'i-1'        // then the string is not sorted        if (s[i] < s[i - 1])           return false;    }    return true;} // Driver codelet s = "aabbbcc";// check whether the string is// in alphabetical order or notif (isAlphabaticOrder(s))    document.write("Yes");else    document.write("No");</script>
Yes
 
Time Complexity: O(N)Auxiliary Space: O(1)
ankthon
Shivi_Aggarwal
Sach_Code
princiraj1992
SURENDRA_GANGWAR
ukasp
subham348
SoumikMondal
rohan07
Competitive Programming
Sorting
Strings
Strings
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Bits manipulation (Important tactics)
Breadth First Traversal ( BFS ) on a 2D array
Remove all occurrences of a character in a string | Recursive approach
String hashing using Polynomial rolling hash function
Sequence Alignment problem