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++
// 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
// 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} |
Python3
# 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#
// C# implementation of above approach// import Arrays classusing 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// 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?> |
Javascript
<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++
// 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
// 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
# 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#
// 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// 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?> |
Javascript
<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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA


