Given a string **S**, the task is to check if S contains a pair of substrings of length **K** which are anagrams of each other and doesn’t contain the character **X** in them. If no such substring exists, print **-1**.

**Examples:**

Input:S = “geeksforgeeks”, X = ‘f’, K = 5

Output:geeks geeks

Explanation:

Substrings “geeks” and “geeks” are anagrams of each other and does not contain ‘f’.

Input:S = “rotator”, X = ‘a’, K = 3

Output:rot tor

Explanation:

Substrings “rot” and “tor” are anagrams of each other and does not contain ‘a’.

**Approach: **

The idea is to generate **prefix sum** on the basis of characters. Follow the steps below to solve the problem:

- Iterate over the string and genertae frequencies of substrings by using the
**prefix sum array**. - If a substring with same frequency of characters is already present in the HashMap.
- Otherwise, store the frequency of characters of the substring with the current substring in the
**HashMap**, if the frequency of the character**X**in the substring is**0**.

Below is the implementation of the above approach:

## Java

`// Java Program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `// Class to represent a Substring ` `// in terms of frequency of ` `// characters present in it ` `class` `Substring { ` ` ` `int` `MOD = ` `1000000007` `; ` ` ` ` ` `// Store count of characters ` ` ` `int` `count[]; ` ` ` `Substring() { count = ` `new` `int` `[` `26` `]; } ` ` ` ` ` `public` `int` `hashCode() ` ` ` `{ ` ` ` `int` `hash = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) { ` ` ` `hash += (i + ` `1` `) * count[i]; ` ` ` `hash %= MOD; ` ` ` `} ` ` ` `return` `hash; ` ` ` `} ` ` ` ` ` `public` `boolean` `equals(Object o) ` ` ` `{ ` ` ` `if` `(o == ` `this` `) ` ` ` `return` `true` `; ` ` ` `if` `(!(o ` `instanceof` `Substring)) ` ` ` `return` `false` `; ` ` ` `Substring ob = (Substring)o; ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) { ` ` ` `if` `(ob.count[i] != count[i]) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `return` `true` `; ` ` ` `} ` `} ` `class` `GFG { ` ` ` ` ` `// Function to check anagrams ` ` ` `static` `void` `checkForAnagrams(String s, ` `int` `n, ` ` ` `char` `X, ` `int` `k) ` ` ` `{ ` ` ` `boolean` `found = ` `false` `; ` ` ` ` ` `// Prefix array to store frequencies ` ` ` `// of characters ` ` ` `int` `prefix[][] = ` `new` `int` `[n + ` `1` `][` `26` `]; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `prefix[i][s.charAt(i) - ` `97` `]++; ` ` ` `} ` ` ` ` ` `// Generate prefix sum ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) { ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `26` `; j++) ` ` ` `prefix[i][j] += prefix[i - ` `1` `][j]; ` ` ` `} ` ` ` ` ` `// Map to store frequencies ` ` ` `HashMap<Substring, Integer> map ` ` ` `= ` `new` `HashMap<>(); ` ` ` ` ` `// Check for anagrams ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(i + k > n) ` ` ` `break` `; ` ` ` ` ` `// Generate frequencies of characters ` ` ` `// of substring starting from i ` ` ` `Substring sub = ` `new` `Substring(); ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `26` `; j++) { ` ` ` `sub.count[j] ` ` ` `= prefix[i + k - ` `1` `][j] ` ` ` `- (i - ` `1` `>= ` `0` ` ` `? prefix[i - ` `1` `][j] ` ` ` `: ` `0` `); ` ` ` `} ` ` ` ` ` `// Check if forbidden character is ` ` ` `// present, then continue ` ` ` `if` `(sub.count[X - ` `97` `] != ` `0` `) ` ` ` `continue` `; ` ` ` ` ` `// If already present in HashMap ` ` ` `if` `(map.containsKey(sub)) { ` ` ` ` ` `found = ` `true` `; ` ` ` ` ` `// Print the substrings ` ` ` `System.out.println( ` ` ` `s.substring(map.get(sub), ` ` ` `map.get(sub) + k) ` ` ` `+ ` `" "` `+ s.substring(i, i + k)); ` ` ` `break` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `map.put(sub, i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If no such substring is found ` ` ` `if` `(!found) ` ` ` `System.out.println(` `"-1"` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String s = ` `"rotator"` `; ` ` ` `int` `n = s.length(); ` ` ` `char` `X = ` `'a'` `; ` ` ` `int` `k = ` `3` `; ` ` ` `checkForAnagrams(s, n, X, k); ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output:**

rot tor

**Time Complexity:** O(N*26)

**Auxiliary Space:** O(N*26)

## Recommended Posts:

- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Print a closest string that does not contain adjacent duplicates
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Next word that does not contain a palindrome and has characters from first k
- DFA that begins with 'a' but does not contain substring 'aab'
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Count subsequences in first string which are anagrams of the second string
- Check if two strings are k-anagrams or not
- Check if all levels of two trees are anagrams or not
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Pairs of strings which on concatenating contains each character of "string"
- Check if a string contains a palindromic sub-string of even length
- Length of the longest substring that do not contain any palindrome
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Largest number from the longest set of anagrams possible from all perfect squares of length K
- Count of substrings which contains a given character K times
- Minimum K such that every substring of length atleast K contains a character c
- Check if a binary string contains all permutations of length k
- Check if a binary string contains consecutive same or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.