📌  相关文章
📜  检查字符串是否包含长度为K的字谜,其中不包含字符X

📅  最后修改于: 2021-04-22 00:53:47             🧑  作者: Mango

给定字符串S ,任务是检查S是否包含一对长度为K的子字符串,这两个子字符串彼此相似,并且不包含字符X。如果不存在这样的子字符串,则打印-1

例子:

方法:
这个想法是在字符的基础上产生前缀和。请按照以下步骤解决问题:

  • 使用前缀和数组迭代子字符串的字符串和生成频率。
  • 如果HashMap中已经存在具有相同字符频率的子字符串。
  • 否则,存储与在HashMap中的当前子串的子串的字符的频率,如果字符X的子串的频率是0。

下面是上述方法的实现:

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 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);
    }
}


输出:
rot tor

时间复杂度: O(N * 26)
辅助空间: O(N * 26)