Panalphabetic window in a string
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:
C++
#include<bits/stdc++.h>
using namespace std;
bool isPanalphabeticWindow( char s[], int n)
{
char ch = 'a' ;
for ( int i = 0; i < n; i++)
{
if (s[i] == ch)
ch++;
if (ch == 'z' + 1)
return true ;
}
return false ;
}
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
class GFG
{
static boolean isPanalphabeticWindow(String s,
int n)
{
char ch = 'a' ;
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == ch)
ch++;
if (ch == 'z' + 1 )
return true ;
}
return false ;
}
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" );
}
}
|
Python3
def isPanalphabeticWindow(s, n) :
ch = 'a'
for i in range ( 0 , n) :
if (s[i] = = ch) :
ch = chr ( ord (ch) + 1 )
if (ch = = 'z' ) :
return True
return False
s = "abujm zvcd acefc deghf gijkle m n o p pafqrstuvwxyzfap"
n = len (s)
if (isPanalphabeticWindow(s, n)) :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool isPanalphabeticWindow( string s,
int n)
{
char ch = 'a' ;
for ( int i = 0; i < n; i++)
{
if (s[i] == ch)
ch++;
if (ch == 'z' + 1)
return true ;
}
return false ;
}
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" );
}
}
|
PHP
<?php
function isPanalphabeticWindow( $s , $n )
{
$ch = 'a' ;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] == $ch )
$ch ++;
if ( $ch == 'z' )
return true;
}
return false;
}
$s = "abujm zvcd acefc deghf gijkle" .
" m n o p pafqrstuvwxyzfap" ;
$n = strlen ( $s );
if (isPanalphabeticWindow( $s , $n ))
echo ( "YES" );
else
echo ( "NO" );
?>
|
Javascript
<script>
function isPanalphabeticWindow(s, n)
{
var ch = 'a' ;
for ( var i = 0; i < n; i++)
{
if (s[i] == ch)
ch = String.fromCharCode(ch.charCodeAt(0) + 1);
if (ch == String.fromCharCode( 'z' .charCodeAt(0) + 1))
return true ;
}
return false ;
}
var s = "abujm zvcd acefc deghf"
+ " gijklem n o p pafqrstuvwxyzfap" ;
var n = (s.length);
(isPanalphabeticWindow(s, n))?(document.write( "YES" )):
(document.write( "NO" ));
</script>
|
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.
Last Updated :
16 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...