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 varible, 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++
// 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
// 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. |
Python3
# 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#
// 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 // 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) ?> |
Output:
YES
Recommended Posts:
- Find the smallest window in a string containing all characters of another string
- Smallest window that contains all characters of string itself
- Python | Get the smallest window in a string containing all characters of given pattern
- String slicing in Python to check if a string can become empty by recursive deletion
- Minimal moves to form a string by adding characters or appending string itself
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Find the count of palindromic sub-string of a string in its sorted form
- Find the character in first string that is present at minimum index in second string
- Find length of longest subsequence of one string which is substring of another string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Check if a string can be converted to another string by replacing vowels and consonants
- Minimum number of given operations required to convert a string to another string
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



