Check if characters of each word can be rearranged to form an Arithmetic Progression (AP)


In this article, we will discuss how to check if the characters of each word in a given string can be rearranged to form an Arithmetic Progression (AP). We will also implement the solution in C++ and provide an example to illustrate the working of the code.

Arithmetic Progression (AP)

An Arithmetic Progression (AP) is a sequence of numbers in which each term after the first is obtained by adding a constant d to the preceding term. The constant d is called the common difference.

For example, the sequence 1, 3, 5, 7, 9 is an Arithmetic Progression with common difference 2.

Approach

To check if the characters of each word in a given string can be rearranged to form an Arithmetic Progression, we will follow the below approach −

  • We will split the given string into individual words.

  • For each word, we will sort the characters in alphabetical order.

  • We will calculate the common difference between each pair of adjacent characters in the sorted word.

  • If the common difference is the same for all pairs of adjacent characters, then the characters of the word can be rearranged to form an Arithmetic Progression.

  • We will repeat steps 2-4 for all words in the given string.

  • If all words can be rearranged to form an Arithmetic Progression, then we return true. Otherwise, we return false.

Example

Let us implement the above approach in C++ −

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

bool canFormAP(string s) {
   vector<string> words;
   string word = "";
   for(char c : s) {
      if(c == ' ') {
         words.push_back(word);
         word = "";
      } else {
         word += c;
      }
   }
   words.push_back(word);
   for(string w : words) {
      sort(w.begin(), w.end());
      int n = w.length();
      if(n <= 2) {
         continue;
      }
      int d = w[1] - w[0];
      for(int i = 2; i < n; i++) {
         if(w[i] - w[i-1] != d) {
            return false;
         }
      }
   }
   return true;
}

int main() {
   string s = "tutorialspoint";
   if(canFormAP(s)) {
      cout << "Characters of each word can be rearranged to form an Arithmetic Progression\n";
   } else {
      cout << "Characters of each word cannot be rearranged to form an Arithmetic Progression\n";
   }
   return 0;
}

Output

Characters of each word cannot be rearranged to form an Arithmetic Progression

In the above code, the canFormAP function takes a string s as input and returns true if the characters of each word in the string can be rearranged to form an Arithmetic Progression. The main function calls the canFormAP function with the string "hello world" as input and prints the appropriate message based on the return value of the function.

Example Test Case

Let us consider an example test case to understand how the above code works −

string s = "the quick brown fox jumps over the lazy dog";

In this example, the given string is "the quick brown fox jumps over the lazy dog". Each word in the string can be rearranged to form an Arithmetic Progression. For example, the word "quick" can be rearranged to form the sequence "cikqu" which is an Arithmetic Progression with common difference 2. As we were discussing, the word "lazy" can be rearranged to form the sequence "alzy" which is an Arithmetic Progression with common difference 11.

Therefore, in this example, the characters of each word in the given string can be rearranged to form an Arithmetic Progression, and the output of the code is "Characters of each word can be rearranged to form an Arithmetic Progression".

Conclusion

In this article, we discussed how to check if the characters of each word in a given string can be rearranged to form an Arithmetic Progression (AP). We followed a simple approach, which involved sorting the characters of each word and checking if the common difference between each pair of adjacent characters is the same. We also provided an implementation of the solution in C++ and explained it with an example test case.

This problem can have various real-life applications. For example, in cryptography, rearranging the characters of a string can be used to encrypt the original message, and checking if the characters can be rearranged to form an AP can be used as a verification step in the decryption process.

Updated on: 17-May-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements