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++
#include <bits/stdc++.h>
using namespace std;
bool isAlphabaticOrder(string s)
{
int n = s.length();
char c[n];
for ( int i = 0; i < n; i++) {
c[i] = s[i];
}
sort(c, c + n);
for ( int i = 0; i < n; i++)
if (c[i] != s[i])
return false ;
return true ;
}
int main()
{
string s = "aabbbcc" ;
if (isAlphabaticOrder(s))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
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" );
}
}
|
Python3
def isAlphabaticOrder(s):
n = len (s)
c = [s[i] for i in range ( len (s))]
c.sort(reverse = False )
for i in range (n):
if (c[i] ! = s[i]):
return False
return True
if __name__ = = '__main__' :
s = "aabbbcc"
if (isAlphabaticOrder(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG{
static bool isAlphabaticOrder(String s)
{
int n = s.Length;
char []c = new char [n];
for ( int i = 0; i < n; i++) {
c[i] = s[i];
}
Array.Sort(c);
for ( int i = 0; i < n; i++)
if (c[i] != s[i])
return false ;
return true ;
}
static public void Main (){
String s = "aabbbcc" ;
if (isAlphabaticOrder(s))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
Function isAlphabaticOrder( $s )
{
$n = strlen ( $s );
$c = array ();
for ( $i = 0; $i < $n ; $i ++)
{
$c [ $i ] = $s [ $i ];
}
sort( $c );
for ( $i = 0; $i < $n ; $i ++)
if ( $c [ $i ] != $s [ $i ])
return false;
return true;
}
$s = "aabbbcc" ;
if (isAlphabaticOrder( $s ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isAlphabaticOrder(s)
{
var n = s.length;
var c = new Array(n);
for ( var i = 0; i < n; i++) {
c[i] = s[i];
}
c.sort();
for ( var i = 0; i < n; i++)
if (c[i] != s[i])
return false ;
return true ;
}
s = "aabbbcc" ;
if (isAlphabaticOrder(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- 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++
#include <bits/stdc++.h>
using namespace std;
bool isAlphabaticOrder(string s)
{
int n = s.length();
for ( int i = 1; i < n; i++) {
if (s[i] < s[i - 1])
return false ;
}
return true ;
}
int main()
{
string s = "aabbbcc" ;
if (isAlphabaticOrder(s))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
public class GFG {
static boolean isAlphabaticOrder(String s) {
int n = s.length();
for ( int i = 1 ; i < n; i++) {
if (s.charAt(i) < s.charAt(i - 1 )) {
return false ;
}
}
return true ;
}
static public void main(String[] args) {
String s = "aabbbcc" ;
if (isAlphabaticOrder(s)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python 3
def isAlphabaticOrder(s):
n = len (s)
for i in range ( 1 , n):
if (s[i] < s[i - 1 ]) :
return False
return True
if __name__ = = "__main__" :
s = "aabbbcc"
if (isAlphabaticOrder(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG{
static bool isAlphabaticOrder( string s)
{
int n = s.Length;
for ( int i = 1; i < n; i++) {
if (s[i] < s[i - 1])
return false ;
}
return true ;
}
static public void Main (){
string s = "aabbbcc" ;
if (isAlphabaticOrder(s))
Console.WriteLine ( "Yes" );
else
Console.WriteLine ( "No" );
}
}
|
PHP
<?php
function isAlphabaticOrder( $s )
{
$n = strlen ( $s );
for ( $i = 1; $i < $n ; $i ++)
{
if ( $s [ $i ] < $s [ $i - 1])
return false;
}
return true;
}
$s = "aabbbcc" ;
if (isAlphabaticOrder( $s ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isAlphabaticOrder( s){
let n = s.length;
for (let i = 1; i < n; i++) {
if (s[i] < s[i - 1])
return false ;
}
return true ;
}
let s = "aabbbcc" ;
if (isAlphabaticOrder(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Another efficient approach:
The steps involved are as follows:
- Iterate through the given string and store the frequency count of each alphabet.
- Then iterate through each alphabet in lexicographically increasing order(from a to z) and let the count of any alphabet be x. Then check if exactly x elements are present in the given string in continuous order or not.
- If it matches with all alphabets then return true otherwise return false.
Illustrative example:
Let Str=”aabbccb”
Then count of each alphabet is as follows { a=2, b= 3 and c=2}.
Now we will iterate through each alphabets and check for the condition.
As a=2, so first 2 elements in the string need to be ‘a’. Since 2 elements with ‘a’ is there in given string we will move further.
Now b=3, so next 3 elements need to be ‘b’ in the string, which is not there as 5th element in given string is not ‘b’. Hence the string is not in alphabetical order.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isAlphabaticOrder(string s)
{
int arr[26]={0};
int n = s.length();
for ( int i=0;i<n;i++)
{
arr[s[i]- 'a' ]++;
}
int ind=0;
for ( int i=0;i<26;i++)
{
while (arr[i]--)
{
char c= char (97+i);
if (c!=s[ind])
{
return false ;
}
else
{
ind++;
}
}
}
return true ;
}
int main()
{
string s = "aabbbcc" ;
if (isAlphabaticOrder(s))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean isAlphabaticOrder(String s)
{
int [] arr = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++) {
arr[i] = 0 ;
}
int n = s.length();
for ( int i = 0 ; i < n; i++) {
arr[s.charAt(i) - 'a' ]++;
}
int ind = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
while (arr[i] > 0 ) {
char c = ( char )( 97 + i);
if (c != s.charAt(ind)) {
return false ;
}
else {
ind++;
}
arr[i]--;
}
}
return true ;
}
public static void main(String args[])
{
String s = "aabbbcc" ;
if (isAlphabaticOrder(s) == true )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isAlphabaticOrder(s):
arr = [ 0 ] * 26 ;
n = len (s);
for i in range ( 0 ,n):
arr[ ord (s[i]) - ord ( 'a' )] + = 1 ;
ind = 0 ;
for i in range ( 0 , 26 ):
while (arr[i]> 0 ):
c = chr ( 97 + i);
if (c! = s[ind]):
return False ;
else :
ind + = 1 ;
arr[i] - = 1 ;
return True ;
s = "aabbbcc" ;
if (isAlphabaticOrder(s)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG {
static bool isAlphabaticOrder( string s)
{
int [] arr = new int [26];
for ( int i = 0; i < 26; i++) {
arr[i] = 0;
}
int n = s.Length;
for ( int i = 0; i < n; i++) {
arr[s[i] - 'a' ]++;
}
int ind = 0;
for ( int i = 0; i < 26; i++) {
while (arr[i] > 0) {
char c = ( char )(97 + i);
if (c != s[ind]) {
return false ;
}
else {
ind++;
}
arr[i]--;
}
}
return true ;
}
public static void Main()
{
string s = "aabbbcc" ;
if (isAlphabaticOrder(s) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
function isAlphabaticOrder(s)
{
let arr= new Array(26).fill(0);
let n = s.length;
for (let i=0;i<n;i++)
{
let x=s[i].charCodeAt( '0' )- 'a' .charCodeAt( '0' );
arr[x]++;
}
let ind=0;
for (let i=0;i<26;i++)
{
while (arr[i]--)
{
let c = String.fromCharCode(i+97)
if (c!=s[ind])
{
return false ;
}
else
{
ind++;
}
}
}
return true ;
}
let s = "aabbbcc" ;
if (isAlphabaticOrder(s))
document.write( "Yes" );
else
document.write( "No" );
|
Complexity Analysis:
- Time Complexity: O(N)
Auxiliary Space: O(1) as the array used to count the frequency of alphabets is of constant size.