Given a string S of size n. The task is to check whether the given string contain panalphabetic window. A Panalphabetic window is a stretch of text that contains all the letters of the alphabet in order.
Examples:
Input : S = "abujm zvcd acefc deghf gijkle m n o p pafqrstuvwxyzfap" Output : YES Panalphabetic Window is in Bold: abujm zvcd acefc deghf gijkle m n o p pafqrstuvwxyzfap Input : S = "geeksforgeeks" Output : NO
The idea is to initialise a variable, say ch, to ‘a’. Start traversing the given string from the beginning and increment the variable ch by 1, if we find a character equal to ch else move to next index. When the string is over check whether ch is equal to ‘z’ + 1, if yes, return true, else return false.
Below is implementation of this approach:
// CPP Program to check whether given string contain // panalphabetic window or not #include<bits/stdc++.h> using namespace std;
// Return if given string contain panalphabetic window. bool isPanalphabeticWindow( char s[], int n)
{ char ch = 'a' ;
// traversing the string
for ( int i = 0; i < n; i++)
{
// if character of string is equal to ch,
// increment ch.
if (s[i] == ch)
ch++;
// if all characters are found, return true.
if (ch == 'z' + 1)
return true ;
}
return false ;
} // Driven Program int main()
{ char s[] = "abujm zvcd acefc deghf gijkle"
" m n o p pafqrstuvwxyzfap" ;
int n = strlen (s);
(isPanalphabeticWindow(s, n))?(cout << "YES" ):
(cout << "NO" );
return 0;
} |
// Java Program to check whether given // string contain panalphabetic // window or not class GFG
{ // Return if given string contain
// panalphabetic window.
static boolean isPanalphabeticWindow(String s,
int n)
{
char ch = 'a' ;
// traversing the string
for ( int i = 0 ; i < n; i++)
{
// if character of string is equal
// to ch, increment ch.
if (s.charAt(i) == ch)
ch++;
// if all characters are
// found, return true.
if (ch == 'z' + 1 )
return true ;
}
return false ;
} // Driver code public static void main (String[] args)
{ String s = "abujm zvcd acefc deghf"
+ " gijklem n o p pafqrstuvwxyzfap" ;
int n = s.length();
if (isPanalphabeticWindow(s, n))
System.out.print( "YES" );
else
System.out.print( "NO" );
} } // This code is contributed by // Anant Agarwal. |
# Python Program to check # whether given string # contain panalphabetic # window or not # Return if given string # contain panalphabetic window. def isPanalphabeticWindow(s, n) :
ch = 'a'
# traversing the string
for i in range ( 0 , n) :
# if character of string
# is equal to ch, increment ch.
if (s[i] = = ch) :
ch = chr ( ord (ch) + 1 )
# if all characters are
# found, return true.
if (ch = = 'z' ) :
return True
return False
# Driver Code s = "abujm zvcd acefc deghf gijkle m n o p pafqrstuvwxyzfap"
n = len (s)
if (isPanalphabeticWindow(s, n)) :
print ( "YES" )
else :
print ( "NO" )
# This code is contributed by # Manish Shaw(manishshaw1) |
// C# Program to check whether given // string contain panalphabetic // window or not using System;
class GFG
{ // Return if given string contain
// panalphabetic window.
static bool isPanalphabeticWindow( string s,
int n)
{
char ch = 'a' ;
// traversing the string
for ( int i = 0; i < n; i++)
{
// if character of string is equal
// to ch, increment ch.
if (s[i] == ch)
ch++;
// if all characters are
// found, return true.
if (ch == 'z' + 1)
return true ;
}
return false ;
} // Driver code
public static void Main ()
{
string s = "abujm zvcd acefc deghf"
+ " gijklem n o p pafqrstuvwxyzfap" ;
int n = s.Length;
if (isPanalphabeticWindow(s, n))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
} // This code is contributed by // Vt_m. |
<?php // PHP Program to check whether // given string contain // panalphabetic window or not // Return if given string // contain panalphabetic window. function isPanalphabeticWindow( $s , $n )
{ $ch = 'a' ;
// traversing the string
for ( $i = 0; $i < $n ; $i ++)
{
// if character of string
// is equal to ch, increment ch.
if ( $s [ $i ] == $ch )
$ch ++;
// if all characters are
// found, return true.
if ( $ch == 'z' )
return true;
}
return false;
} // Driver Code $s = "abujm zvcd acefc deghf gijkle" .
" m n o p pafqrstuvwxyzfap" ;
$n = strlen ( $s );
if (isPanalphabeticWindow( $s , $n ))
echo ( "YES" );
else echo ( "NO" );
// This code is contributed by // Manish Shaw(manishshaw1) ?> |
<script> // Javascript Program to check whether given string contain // panalphabetic window or not // Return if given string contain panalphabetic window. function isPanalphabeticWindow(s, n)
{ var ch = 'a' ;
// traversing the string
for ( var i = 0; i < n; i++)
{
// if character of string is equal to ch,
// increment ch.
if (s[i] == ch)
ch = String.fromCharCode(ch.charCodeAt(0) + 1);
// if all characters are found, return true.
if (ch == String.fromCharCode( 'z' .charCodeAt(0) + 1))
return true ;
}
return false ;
} // Driven Program var s = "abujm zvcd acefc deghf"
+ " gijklem n o p pafqrstuvwxyzfap" ;
var n = (s.length);
(isPanalphabeticWindow(s, n))?(document.write( "YES" )):
(document.write( "NO" ));
// This code is contributed by itsok. </script> |
YES
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.