在Byteland国家,一个字符串S超ASCII字符串表示当且仅当该字符串中的每个字符的数量等于其ASCII值。在 Byteland 国家, ‘a’ 的ASCII 码是 1,’b’ 是 2,……,’z’ 是 26 。任务是找出给定的字符串是否是超级 ASCII字符串。如果为真,则打印“是”,否则打印“否” 。
例子:
Input: S = “bba”
Output: Yes
Explanation:
The count of character ‘b’ is 2 and the ASCII value of ‘b’ is also 2.
The count of character ‘a’ is 1. and the ASCII value of ‘a’ is also 1.
Hence, string “bba” is super ASCII String.
Input: S = “ssba”
Output: No
Explanation:
The count of character ‘s’ is 2 and the ASCII value of ‘s’ is 19.
The count of character ‘b’ is 1. and the ASCII value of ‘b’ is 2.
Hence, string “ssba” is not a super ASCII String.
方法: Byteland中一个字符’ ch ‘的ASCII值可以通过以下公式计算:
The ASCII value of ch = integer equivalent of ch – integer equivalent of ‘a'(97) + 1
现在,使用此公式,可以将字符串中每个字符的频率计数与其 ASCII 值进行比较。请按照以下步骤解决问题:
- 初始化一个数组来存储字符串中每个字符的频率计数。
- 遍历字符串S并将每个字符的频率计数增加1 。
- 再次遍历字符串S并检查是否有任何字符具有非零频率且不等于其 ASCII 值,然后打印“No” 。
- 完成上述步骤后,如果上述步骤中没有任何此类字符,则打印“是” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to check whether the
// string is super ASCII or not
void checkSuperASCII(char s[])
{
// Stores the frequency count
// of characters 'a' to 'z'
int b[26] = {0};
// Traverse the string
for(int i = 0; i < strlen(s); i++)
{
// AscASCIIii value of the
// current character
int index = (int)s[i] - 97 + 1;
// Count frequency of each
// character in the string
b[index - 1]++;
}
// Traverse the string
for(int i = 0; i < strlen(s); i++)
{
// ASCII value of the current
// character
int index = (int)s[i] - 97 + 1;
// Check if the frequency of
// each character in string
// is same as ASCII code or not
if (b[index - 1] != index)
{
cout << "No";
return;
}
}
// Else print "Yes"
cout << "Yes";
}
// Driver Code
int main()
{
// Given string S
char s[] = "bba";
// Function Call
checkSuperASCII(s);
return 0;
}
// This code is contributed by khushboogoyal499
C
// C program for the above approach
#include
#include
// Function to check whether the
// string is super ASCII or not
void checkSuperASCII(char s[])
{
// Stores the frequency count
// of characters 'a' to 'z'
int b[26] = { 0 };
// Traverse the string
for (int i = 0; i < strlen(s); i++) {
// AscASCIIii value of the
// current character
int index = (int)s[i] - 97 + 1;
// Count frequency of each
// character in the string
b[index - 1]++;
}
// Traverse the string
for (int i = 0; i < strlen(s); i++) {
// ASCII value of the current
// character
int index = (int)s[i] - 97 + 1;
// Check if the frequency of
// each character in string
// is same as ASCII code or not
if (b[index - 1] != index) {
printf("No");
return;
}
}
// Else print "Yes"
printf("Yes");
}
// Driver Code
int main()
{
// Given string S
char s[] = "bba";
// Function Call
checkSuperASCII(s);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to check whether the
// string is super ASCII or not
public static void checkSuperASCII(String s)
{
// Stores the frequency count
// of characters 'a' to 'z'
int b[] = new int[26];
// Traverse the string
for(int i = 0; i < s.length(); i++)
{
// AscASCIIii value of the
// current character
int index = (int)s.charAt(i) - 97 + 1;
// Count frequency of each
// character in the string
b[index - 1]++;
}
// Traverse the string
for(int i = 0; i < s.length(); i++)
{
// ASCII value of the current
// character
int index = (int)s.charAt(i) - 97 + 1;
// Check if the frequency of
// each character in string
// is same as ASCII code or not
if (b[index - 1] != index)
{
System.out.println("No");
return;
}
}
// Else print "Yes"
System.out.println("Yes");
}
// Driver Code
public static void main(String args[])
{
// Given string S
String s = "bba";
// Function Call
checkSuperASCII(s);
}
}
// This code is contributed by Md Shahbaz Alam
Python3
# Python3 program for the above approach
# Function to check whether the
# string is super ASCII or not
def checkSuperASCII(s):
# Stores the frequency count
# of characters 'a' to 'z'
b = [0 for i in range(26)]
# Traverse the string
for i in range(len(s)):
# AscASCIIii value of the
# current character
index = ord(s[i]) - 97 + 1;
# Count frequency of each
# character in the string
b[index - 1] += 1
# Traverse the string
for i in range(len(s)):
# ASCII value of the current
# character
index = ord(s[i]) - 97 + 1
# Check if the frequency of
# each character in string
# is same as ASCII code or not
if (b[index - 1] != index):
print("No")
return
# Else print "Yes"
print("Yes")
# Driver Code
if __name__ == '__main__':
# Given string S
s = "bba"
# Function Call
checkSuperASCII(s)
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program for the above approach
using System;
class GFG {
// Function to check whether the
// string is super ASCII or not
static void checkSuperASCII(string s)
{
// Stores the frequency count
// of characters 'a' to 'z'
int[] b = new int[26];
// Traverse the string
for(int i = 0; i < s.Length; i++)
{
// AscASCIIii value of the
// current character
int index = (int)s[i] - 97 + 1;
// Count frequency of each
// character in the string
b[index - 1]++;
}
// Traverse the string
for(int i = 0; i < s.Length; i++)
{
// ASCII value of the current
// character
int index = (int)s[i] - 97 + 1;
// Check if the frequency of
// each character in string
// is same as ASCII code or not
if (b[index - 1] != index)
{
Console.WriteLine("No");
return;
}
}
// Else print "Yes"
Console.WriteLine("Yes");
}
// Driver code
static void Main()
{
// Given string S
string s = "bba";
// Function Call
checkSuperASCII(s);
}
}
// This code is contributed by divyeshrabadiya07.
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live