给定小写字母字符串str ,任务是检查字符串中每个不同字符的频率是否等于它在英文字母表中的位置。如果有效,则打印“是” ,否则打印“否” 。
例子:
Input: str = “abbcccdddd”
Output: Yes
Explanation:
Since frequency of each distinct character is equals to its position in English Alphabet, i.e.
F(a) = 1,
F(b) = 2,
F(c) = 3, and
F(d) = 4
Hence the output is Yes.
Input: str = “geeksforgeeks”
Output: No
方法:
- 将每个字符的频率存储在一个 26 的数组中,用于散列目的。
- 现在遍历哈希数组并检查索引 i 处每个字符的频率是否等于 (i + 1)。
- 如果是,则打印“是” ,否则打印“否”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
bool checkValidString(string str)
{
// Initialise frequency array
int freq[26] = { 0 };
// Traverse the string
for (int i = 0; str[i]; i++) {
// Update the frequency
freq[str[i] - 'a']++;
}
// Check for valid string
for (int i = 0; i < 26; i++) {
// If frequency is non-zero
if (freq[i] != 0) {
// If freq is not equals
// to (i+1), then return
// false
if (freq[i] != i + 1) {
return false;
}
}
}
// Return true;
return true;
}
// Driver Code
int main()
{
// Given string str
string str = "abbcccdddd";
if (checkValidString(str))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
class GFG{
static boolean checkValidString(String str)
{
// Initialise frequency array
int freq[] = new int[26];
// Traverse the String
for(int i = 0; i < str.length(); i++)
{
// Update the frequency
freq[str.charAt(i) - 'a']++;
}
// Check for valid String
for(int i = 0; i < 26; i++)
{
// If frequency is non-zero
if (freq[i] != 0)
{
// If freq is not equals
// to (i+1), then return
// false
if (freq[i] != i + 1)
{
return false;
}
}
}
// Return true;
return true;
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "abbcccdddd";
if (checkValidString(str))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program for the
# above approach
def checkValidString(str):
# Initialise frequency array
freq = [0 for i in range(26)]
# Traverse the string
for i in range(len(str)):
# Update the frequency
freq[ord(str[i]) - ord('a')] += 1
# Check for valid string
for i in range(26):
# If frequency is non-zero
if(freq[i] != 0):
# If freq is not equals
# to (i+1), then return
# false
if(freq[i] != i + 1):
return False
# Return true
return True
# Driver Code
# Given string str
str = "abbcccdddd"
if(checkValidString(str)):
print("Yes")
else:
print("No")
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
class GFG{
static bool checkValidString(String str)
{
// Initialise frequency array
int []freq = new int[26];
// Traverse the String
for(int i = 0; i < str.Length; i++)
{
// Update the frequency
freq[str[i] - 'a']++;
}
// Check for valid String
for(int i = 0; i < 26; i++)
{
// If frequency is non-zero
if (freq[i] != 0)
{
// If freq is not equals
// to (i+1), then return
// false
if (freq[i] != i + 1)
{
return false;
}
}
}
// Return true;
return true;
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "abbcccdddd";
if (checkValidString(str))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
Yes
时间复杂度: O(N) ,其中 N 是字符串的长度。
辅助空间: O(26)