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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that checks whether
// the string is in alphabetical
// order or not
bool 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 code
int main()
{
    string s = "aabbbcc";
  
    // check whether the string is
    // in alphabetical order or not
    if (isAlphabaticOrder(s))
       cout << "Yes";
    else
       cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach 
  
// import  Arrays class
import 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
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of above approach
  
# Function that checks whether
# the string is in alphabetical
# order or not
def 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 code
if __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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"); 
          
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function that checks whether
// the string is in alphabetical
// order or not
Function 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 not
if (isAlphabaticOrder($s))
    echo "Yes";
else
    echo "No";
      
// This Code is contributed
// by Shivi_Aggarwal
?>

chevron_right


Output:

Yes

Time Complexity: O(N*log(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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that checks whether
// the string is in alphabetical
// order or not
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
int main()
{
    string s = "aabbbcc";
  
    // check whether the string is
    // in alphabetical order or not
    if (isAlphabaticOrder(s))
       cout << "Yes";
    else
       cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach 
public 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of above approach
  
# Function that checks whether
# the string is in alphabetical
# order or not
def 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 code
if __name__ == "__main__":
  
    s = "aabbbcc"
  
    # check whether the string is
    # in alphabetical order or not
    if (isAlphabaticOrder(s)):
        print("Yes")
    else:
        print("No")

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach 
using System;
  
public class GFG{
      
// Function that checks whether 
// the string is in alphabetical 
// order or not 
static 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"); 
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function that checks whether
// the string is in alphabetical
// order or not
function 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 not
if (isAlphabaticOrder($s))
    echo "Yes";
else
    echo "No";
  
// This code is contributed 
// by Sach_Code
?>

chevron_right


Output:

Yes

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.