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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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)
?>

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : manishshaw1, nidhi_biet



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.