Home >Backend Development >C++ >Check whether the characters of each word can be rearranged to form an arithmetic sequence (AP)

Check whether the characters of each word can be rearranged to form an arithmetic sequence (AP)

王林
王林forward
2023-09-08 20:53:081230browse

Check whether the characters of each word can be rearranged to form an arithmetic sequence (AP)

In this article, we will discuss how to check whether the characters of each word in a given string can be rearranged to form an arithmetic sequence (AP). We'll also implement the solution in C and provide an example to illustrate how the code works.

Arithmetic sequence (AP)

Arithmetic sequence (AP) is a sequence of numbers in which each term is obtained by adding a constant d to the previous term. The constant d is called the tolerance.

For example, the sequence 1, 3, 5, 7, 9 is an arithmetic sequence with a tolerance of 2.

method

To check if the characters of each word in a given string can be rearranged to form an arithmetic sequence, we will do it as follows −

  • We will split the given string into individual words.

  • For each word, we will sort the characters alphabetically.

  • We will calculate the tolerance of adjacent characters in the sorted words.

  • If the tolerance is the same for all pairs of adjacent characters, then the characters of the word can be rearranged to form an arithmetic sequence.

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

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

Example

Let us implement the above method 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 accepts a string s as input and returns true if the characters of each word in the string can be rearranged to form an arithmetic sequence. The main function calls the canFormAP function, takes the string "hello world" as input, and prints the corresponding 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 a string can be rearranged to form an arithmetic sequence. For example, the word "quick" can be rearranged into the arithmetic sequence "cikqu" with a tolerance of 2. As we discussed, the word "lazy" can be rearranged into the arithmetic sequence "alzy" with a tolerance of 11.

So, in this example, the characters of each word in the given string can be rearranged to form an arithmetic sequence, and the output of the code is "The characters can be rearranged to form an arithmetic sequence."

in conclusion

In this article, we discussed how to check whether the characters of each word in a given string can be rearranged to form an arithmetic sequence (AP). We adopted a simple approach of sorting the characters of each word and checking whether the tolerances between adjacent pairs of characters were the same. We also provide an example of implementing the solution in C and explain it with a sample test case.

This problem can have various practical applications. For example, in cryptography, rearranging the characters of a string can be used to encrypt the original message, and checking whether the characters can be rearranged to form an arithmetic sequence can be used as a verification step in the decryption process.

The above is the detailed content of Check whether the characters of each word can be rearranged to form an arithmetic sequence (AP). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete