# 29. Check if a string is repetition of its substring of k-length

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/check-if-a-string-is-repetition-of-its-substring-of-k-length3302/1)

![](https://badgen.net/badge/Level/Medium/yellow)

## My Approach

This GFG question is a little unclear. In simple terms, it's asking if we can turn a given string into a repeating one by replacing it just once at positions `i` and `j` so that all substrings of length k become repetitive.

```
Example:
s = "ababcdabab"
k = 2
so in this we can replace 0th index substring "ab" with 4th index substring "cd"
```

To solve this question here are the steps:

* First check if the length of the string is divisible by `k`.
  * If not, it's impossible to form substrings of length k evenly.
  * If it is divisible, we iterate through the string and maintain a count of each substring of length `k` using an unordered\_map.
  * After counting, we iterate through the map and count how many substrings occur more than once.
  * The final check is whether the size of the map is less than or equal to 2 (indicating one or two unique substrings) and the count of repeated substrings is at most 1.

## Time and Auxiliary Space Complexity

* **Time Complexity** : `O(n)` where n is the length of the input string. We iterate through the string once.
* **Auxiliary Space Complexity** : `O(n/k)` for the unordered\_set, where k is the length of the substring.

## Code (C++)

```cpp
class Solution {
public:
    int kSubstrConcat(int n, string s, int k) {
        if (n % k > 0)
            return 0;

        unordered_map<string,int> mp;
        for (int i = 0; i < n / k; ++i)
            ++mp[s.substr(i * k, k)];
        
        int cnt = 0;
        for(auto i : mp)
            cnt += i.second > 1;

        return (mp.size() <= 2 && cnt <= 1);
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2023/12-2023-dec-28/29-check-if-a-string-is-repetition-of-its-substring-of-k-length.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
