字符串中的泛字母窗口
给定一个大小为n的字符串S。任务是检查给定的字符串是否包含泛字母窗口。 Panalphabetic 窗口是一段文本,其中包含按顺序排列的所有字母。
例子:
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
这个想法是初始化一个变量,比如ch,为'a'。从头开始遍历给定的字符串,如果我们找到一个等于 ch 的字符,则将变量 ch 加 1,否则移动到下一个索引。当字符串结束时检查 ch 是否等于 'z' + 1,如果是,则返回 true,否则返回 false。
下面是这种方法的实现:
C++
// CPP Program to check whether given string contain
// panalphabetic window or not
#include
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
Javascript
输出:
YES
https://youtu.be/HQY36
-伊玛格