给定一个由N个小写字母组成的字符串S ,任务是检查是否可以通过将给定字符串的任意两个字符递增和递减1来使字符串S 的所有字符相等。如果可以使所有字符都相同,则打印“Yes” 。否则,打印“否” 。
Incrementing or decrementing any character means that change the characters to its next or previous character in the English alphabet respectively. If the characters are ‘a’ and ‘z’ then they cannot be changed further.
例子:
Input: S = “beb”
Output: Yes
Explanation:
The characters of the given string S can be made equal by performing the below operations:
- For string “beb” increment ‘b’ by 1 and decrement ‘e’ by 1, then the string becomes “cdb”.
- For string “cdb” decrement ‘d’ by 1 and increment ‘b’ by 1, then the string becomes “ccc”.
Input: S = “geeks”
Output: No
方法:可以根据以下观察解决给定的问题:
- 任意字符同时递增和递减1时,操作前后字符串的 ASCII 值总和保持不变。
- 假设任何字符的 ASCII 值是X 。如果所有字符都相等,则字符串的总和 ASCII 值是N*X 。因此,可以说总和可被N整除。因此,ASCII 值的初始总和需要被N整除才能使所有字符相等。
请按照以下步骤解决此问题:
- 初始化一个变量sum ,它存储给定字符串的 ASCII 值的总和。
- 遍历给定的字符串S并对每个字符S[i]将(S[i] – ‘a’ + 1)的值添加到sum 。
- 完成上述步骤后,如果sum的值可以被N整除,则可以使给定字符串的所有字符相等。因此,打印“是” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if it is
// possible to make all characters
// of string S same or not
void canMakeEqual(string S)
{
// Length of string
int N = S.size();
// Stores the sum of ASCII value
int weightOfString = 0;
// Traverse the string S
for (int i = 0; i < N; i++) {
// Update the weightOfString
weightOfString += S[i] - 'a' + 1;
}
// If the sum is divisible by N
// then print "Yes"
if (weightOfString % N == 0)
cout << "Yes";
// Otherwise print "No"
else
cout << "No";
}
// Driver Code
int main()
{
string S = "beb";
canMakeEqual(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to check if it is
// possible to make all characters
// of string S same or not
static void canMakeEqual(String S){
// Length of string
int N = S.length();
// Stores the sum of ASCII value
int weightOfString = 0;
// Traverse the string S
for (int i = 0; i < N; i++) {
// Update the weightOfString
weightOfString += S.charAt(i) - 'a' + 1;
}
// If the sum is divisible by N
// then print "Yes"
if (weightOfString % N == 0)
System.out.println("Yes");
// Otherwise print "No"
else
System.out.println("No");
}
// Driver Code
public static void main (String[] args) {
String S = "beb";
canMakeEqual(S);
}
}
// This code is contributed by aadityaburujwale
Python3
# Python3 program for the above approach
# Function to check if it is
# possible to make all characters
# of string S same or not
def canMakeEqual(S):
# Length of string
N = len(S)
# Stores the sum of ASCII value
weightOfString = 0
# Traverse the string S
for i in range(N):
# Update the weightOfString
weightOfString += ord(S[i]) - ord('a') + 1
# If the sum is divisible by N
# then pr "Yes"
if (weightOfString % N == 0):
print("Yes")
# Otherwise pr "No"
else:
print("No")
# Driver Code
S = "beb"
canMakeEqual(S)
# This code is contributed by susmitakundugoaldanga
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if it is
// possible to make all characters
// of string S same or not
static void canMakeEqual(String S)
{
// Length of string
int N = S.Length;
// Stores the sum of ASCII value
int weightOfString = 0;
// Traverse the string S
for(int i = 0; i < N; i++)
{
// Update the weightOfString
weightOfString += S[i] - 'a' + 1;
}
// If the sum is divisible by N
// then print "Yes"
if (weightOfString % N == 0)
Console.WriteLine("Yes");
// Otherwise print "No"
else
Console.WriteLine("No");
}
// Driver Code
public static void Main(String[] args)
{
String S = "beb";
canMakeEqual(S);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live