Given string str, the task is to check if it is possible to rearrange the string such that the characters of each word of the given string are in Arithmetic Progression.
Examples:
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
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:
C++
#include <bits/stdc++.h>
using namespace std;
int checkWordAP(string str)
{
stringstream ss(str);
string temp;
while (getline(ss, temp, ' ' )) {
sort(temp.begin(), temp.end());
for ( int i = 2; i < temp.length();
i++) {
int diff = temp[1] - temp[0];
if (diff != temp[i] - temp[i - 1]) {
return false ;
}
}
}
return true ;
}
int main()
{
string str = "ace yzx fbd" ;
if (checkWordAP(str)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
|
Java
import java.util.*;
class GFG{
static boolean checkWordAP(String s)
{
String str[] = s.split( " " );
for (String temp : str )
{
temp = sort(temp);
for ( int i = 2 ; i < temp.length(); i++)
{
int diff = temp.charAt( 1 ) - temp.charAt( 0 );
if (diff != temp.charAt(i) - temp.charAt(i - 1 ))
{
return false ;
}
}
}
return true ;
}
static String sort(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args)
{
String str = "ace yzx fbd" ;
if (checkWordAP(str))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
def checkWordAP(st):
st = st.split( " " )
for temp in st:
temp = sorted (temp)
for i in range ( 2 , len (temp)):
diff = ord (temp[ 1 ]) - ord (temp[ 0 ])
if (diff ! = ord (temp[i]) -
ord (temp[i - 1 ])):
return False
return True
if __name__ = = '__main__' :
st = "ace yzx fbd"
if (checkWordAP(st)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkWordAP(String s)
{
String []str = s.Split( ' ' );
String temp = "" ;
foreach (String temp1 in str )
{
temp = sort(temp1);
for ( int i = 2; i < temp.Length; i++)
{
int diff = temp[1] - temp[0];
if (diff != temp[i] - temp[i - 1])
{
return false ;
}
}
}
return true ;
}
static String sort(String inputString)
{
char []tempArray = inputString.ToCharArray();
Array.Sort(tempArray);
return new String(tempArray);
}
public static void Main(String[] args)
{
String str = "ace yzx fbd" ;
if (checkWordAP(str))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function checkWordAP(s)
{
let str = s.split( " " );
for (let temp = 0; temp < str.length; temp++ )
{
str[temp] = sort(str[temp]);
for (let i = 2; i < str[temp].length; i++)
{
let diff = str[temp][1].charCodeAt(0) - str[temp][0].charCodeAt(0);
if (diff != str[temp][i].charCodeAt(0) - str[temp][i-1].charCodeAt(0))
{
return false ;
}
}
}
return true ;
}
function sort(inputString)
{
let tempArray = inputString.split( "" );
(tempArray).sort();
return (tempArray).join( "" );
}
let str = "ace yzx fbd" ;
if (checkWordAP(str))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(N log2N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Jun, 2022
Like Article
Save Article