定的字符串str,任务是检查是否有可能重新排列字符串,使得定字符串的每个字的字符是算术级数。
例子:
Input: str = “ace yzx fbd”
Output: true
Explanation: Rearrange the given string to “ace xyz bdf”.
All the characters of the word “ace” are in AP with a common difference of 2.
All the characters of the word “xyz” are in AP with a common difference of 1
All the characters of the word “bdf” are in AP with a common difference of 2.
Therefore, the required output is true.
Input: str = “geeks for geeks”
Output: false
方法:想法是对给定字符串的每个单词进行排序,并检查所有单词中相邻字符之间的差异是否相等。如果发现是真的,则打印“是” 。否则,打印No。请按照以下步骤解决问题。
- 遍历字符串str ,并用空格定界符分割str的每个单词。
- 以升序对给定字符串的每个单词进行排序。
- 检查单词的所有相邻字符之间的差异是否相等。
- 如果发现是真的,则打印“是” 。否则,打印No。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check str can be
// rearranged such that characters
// of each word forms an AP
int checkWordAP(string str)
{
// Stores the string
// in stringstream
stringstream ss(str);
// Stores each word of
// the given string
string temp;
while (getline(ss, temp, ' ')) {
// Sort the current word
sort(temp.begin(), temp.end());
// Check if the current word
// of the given string is in AP
for (int i = 2; i < temp.length();
i++) {
// Store the value of difference
// between adjacent characters
int diff = temp[1] - temp[0];
// Check if difference between all
// adjacent characters are equal
if (diff != temp[i] - temp[i - 1]) {
return false;
}
}
}
// If all words are in AP.
return true;
}
// Driver Code
int main()
{
string str = "ace yzx fbd";
// If all words of the given
// string are in AP
if (checkWordAP(str)) {
cout << "Yes";
}
else {
cout << "No";
}
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to check str can be
// rearranged such that characters
// of each word forms an AP
static boolean checkWordAP(String s)
{
// Stores the String
// in Stringstream
String str[] = s.split(" ");
// Stores each word of
// the given String
for (String temp : str )
{
// Sort the current word
temp = sort(temp);
// Check if the current word
// of the given String is in AP
for (int i = 2; i < temp.length(); i++)
{
// Store the value of difference
// between adjacent characters
int diff = temp.charAt(1) - temp.charAt(0);
// Check if difference between all
// adjacent characters are equal
if (diff != temp.charAt(i) - temp.charAt(i - 1))
{
return false;
}
}
}
// If all words are in AP.
return true;
}
static String sort(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
// Driver Code
public static void main(String[] args)
{
String str = "ace yzx fbd";
// If all words of the given
// String are in AP
if (checkWordAP(str))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to implement
# the above approach
# Function to check st can be
# rearranged such that characters
# of each word forms an AP
def checkWordAP(st):
# Stores each word of
# the given sting
st = st.split(" ")
for temp in st:
# Sort the current word
temp = sorted(temp)
# Check if the current word
# of the given is in AP
for i in range(2, len(temp)):
# Store the value of difference
# between adjacent characters
diff = ord(temp[1]) - ord(temp[0])
# Check if difference between all
# adjacent characters are equal
if (diff != ord(temp[i]) -
ord(temp[i - 1])):
return False
# If all words are in AP.
return True
# Driver Code
if __name__ == '__main__':
st = "ace yzx fbd"
# If all words of the given
# are in AP
if (checkWordAP(st)):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check str can be
// rearranged such that characters
// of each word forms an AP
static bool checkWordAP(String s)
{
// Stores the String
// in Stringstream
String []str = s.Split(' ');
// Stores each word of
// the given String
String temp = "";
foreach (String temp1 in str )
{
// Sort the current word
temp = sort(temp1);
// Check if the current word
// of the given String is in AP
for (int i = 2; i < temp.Length; i++)
{
// Store the value of difference
// between adjacent characters
int diff = temp[1] - temp[0];
// Check if difference between all
// adjacent characters are equal
if (diff != temp[i] - temp[i - 1])
{
return false;
}
}
}
// If all words are in AP.
return true;
}
static String sort(String inputString)
{
// convert input string to char array
char []tempArray = inputString.ToCharArray();
// sort tempArray
Array.Sort(tempArray);
// return new sorted string
return new String(tempArray);
}
// Driver Code
public static void Main(String[] args)
{
String str = "ace yzx fbd";
// If all words of the given
// String are in AP
if (checkWordAP(str))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by Princi Singh
输出
Yes
时间复杂度: O(N log 2 N)
辅助空间: O(N)