Check if given String is K-periodic for all K in range [1, N]


The aim of this article is to implement a program to check if a given String is K-periodic for all K in range [1, N].

The aim is to determine whether the supplied string is K-periodic provided the string s and the integer K. If a string repeats the sub-string str[0... k-1], it is said to be k-periodic; for example, the string "ababab" is 2 periods long. Print Yes if the supplied string is k-periodic; otherwise, print No.

If a character string can be created by concatenating at least one repeats from another string of length k, it is said to have period k. For instance, because the character "xyz" is repeated four times, the string "xyzxyzxyzxyz" contains a period of three. Periods 6 (two repeats of "xyzxyz") and 12 (one recurrence of "xyzxyzxyzxyz") are also included.

Example

Input: N = 7 and the string S = "xyzxyzx"
Output: 3 6 7

Explanation

  • Take the prefix "x," which has a length of 1. If we repeat it to create a string with a length of 7, we obtain "xxxxxx," which is not the same as the original string.

  • If we repeat a prefix of a length of two (in this case, "xy") to create a string with length 7, we obtain "xyxyxyx," which is not the same as the original string.

  • Assume a prefix with length 3 (such as "xyz"), then repeat it to create a string with length 7 (resulting in "xyzxyzx"). This string is identical to the initial string.

  • Let's take a prefix with length 4 (i.e., "xyzx") and repeat it to create a string with length 7 (i.e., "xyzxxyz"). This string isn't identical to the initial string.

  • Let's take a prefix with length 5 (i.e., "xyzxy") and repeat it to create a string with length 7 (i.e., "xyzxyxy"). This string is not identical to the initial string.

  • Let's take a prefix with length 6 (i.e., "xyzxyz") and repeat it to create a string with length 7 (i.e., "xyzxyzx"). This string is identical to the initial string.

  • If we repeat the prefix "xyzxyzx" to create a string with length 7, we obtain "xyzxyzx" which is identical to the initial string.

Problem Statement

Implement a program to check if a given String is K-periodic for all K in range [1, N].

Approach

Let us dive deep into what exactly is the Z algorithm or the Algorithm for searching patterns in linear time.

In linear time, this technique locates every instance of a pattern inside a text. If the length of the text is n and the length of the pattern is m, the total time required has a linear space complexity of O(m + n). Now it is clear that the KMP method shares the same time and space complexity, but it is easier to comprehend.

In this algorithm, a Z array is created.

Now let us describe the Z Array.

Z array has the identical length as string str[0...n-1] for that string. The length of the largest substring beginning with str[i], that additionally possesses a prefix of str[0...n-1], is stored in element Z[i] of the Z array. Since a whole string is always a prefix of itself, the first entry in the Z array has little significance.

Algorithm

The algorithm to obtain a program to check if a given String is K-periodic for all K in range [1, N] given below

  • Step 1 − Let's build a Z array with the supplied string. (0-indexed)

  • Step 2 − If 'i' is one of the string's periods, then a suffix with length (N-i) corresponds precisely with a prefix of length (n-i), therefore if 'i' is a period, (N-i) ought to be equivalent to the z value at position i.

  • Step 3 − Finally, add N (the length of the given string) to the solution; the N number is straightforward, but the approach outlined before fails to locate it.

  • Step 4 − Print the output.

Example: C Program

Here is the C program implementation of the above written algorithm to check if a given String is K-periodic for all K in range [1, N]

#include <stdio.h>
#include <string.h>

void getZarr(char *str, int Z[]);
void findPeriod(char *text) {
   int l = strlen(text);
   int Z[l];
   getZarr(text, Z);
   for (int i = 1; i < l; ++i) {
      if (Z[i] == (l - i))
         printf("%d ", i);
   }
   printf("%d\n", l);
}
void getZarr(char *str, int Z[]) {
   int n = strlen(str);
   int L, R, k;
   L = R = 0;
   for (int i = 1; i < n; ++i) {
      if (i > R) {
         L = R = i;
         while (R < n && str[R - L] == str[R])
            R++;
         Z[i] = R - L;
         R--;
      } else {
         k = i - L;
         if (Z[k] < R - i + 1)
            Z[i] = Z[k];
         else {
            L = i;
            while (R < n && str[R - L] == str[R])
               R++;
            Z[i] = R - L;
            R--;
         }
      }
   }
}
int main() {
   char text[] = "xyzxyzx";
   findPeriod(text);
   return 0;
}

Output

3 6 7

Conclusion

Likewise, we can obtain a program to check if a given String is K-periodic for all K in range [1, N]. The challenge of obtaining the program to check if a given String is K-periodic for all K in range [1, N] is resolved in this article.

Here C programming code as well as the algorithm and the methodology to implement the C program to Check if a given String is K-periodic for all K in range [1, N] are provided.

Updated on: 30-Oct-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements