Input: str = “ace yzx fbd”
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”
Approach: The idea is to sort each word of the given string and check if the difference between adjacent characters in all the words are equal or not. If found to be true, then print Yes. Otherwise, print No. Follow the steps below to solve the problem.
- Iterate over string str, and split each word of str by a space delimiter.
- Sort each word of the given string in ascending order.
- Check if the difference between all the adjacent characters of the words is equal.
- If found to be true, print Yes. Otherwise, print No.
Below is the implementation of the above approach:
Time Complexity: O(N log2N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Check if characters of a given string can be rearranged to form a palindrome
- Longest substring whose characters can be rearranged to form a Palindrome
- Check if a string can be rearranged to form special palindrome
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Check whether Arithmetic Progression can be formed from the given array
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Longest Arithmetic Progression | DP-35
- Count of AP (Arithmetic Progression) Subsequences in an array
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Program for N-th term of Arithmetic Progression series
- Program to print Arithmetic Progression series
- PHP program to print an arithmetic progression series using inbuilt functions
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Longest arithmetic progression with the given common difference
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Convert given array to Arithmetic Progression by adding an element
- Arithmetic Progression
- Change one element in the given array to make it an Arithmetic Progression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.