📌  相关文章
📜  每个字符出现偶数次的子字符串数

📅  最后修改于: 2021-05-07 00:32:11             🧑  作者: Mango

给定一个包含N个小写字母的字符串S ,任务是计算每个字符的频率为偶数的子字符串的数量。

例子:

天真的方法:解决给定问题的最简单方法是生成给定字符串的所有可能的子字符串 并计算每个字符出现频率均匀的子串。检查所有子字符串后,打印获得的总数。

下面是上述方法的实现:

Python3
# Python program for the above approach
  
# Function to count substrings having
# even frequency of each character
def subString(s, n):
  
    # Stores the total
    # count of substrings
    count = 0
  
    # Traverse the range [0, N]:
    for i in range(n):
        
        # Traverse the range [i + 1, N]
        for len in range(i + 1, n + 1):
            
            # Stores the substring over
            # the range of indices [i, len]
            test_str = (s[i: len])
  
            # Stores the frequency of characters
            res = {}
  
            # Count frequency of each character
            for keys in test_str:
                res[keys] = res.get(keys, 0) + 1
                  
            flag = 0
              
            # Traverse the dictionary
            for keys in res:
                
                # If any of the keys
                # have odd count
                if res[keys] % 2 != 0:
  
                    flag = 1
                    break
                      
            # Otherwise
            if flag == 0:
                count += 1
  
    # Return count
    return count
  
  
# Driver Code
  
S = "abbaa"
N = len(S)
print(subString(S, N))


Python3
# Python program for the above approach
  
# Function to count substrings having
# even frequency of each character
def subString(s, n):
  
    # Stores the count of a character
    hash = {0: 1}
  
    # Stores bitmask
    pre = 0
  
    # Stores the count of substrings
    # with even count of each character
    count = 0
  
    # Traverse the string S
    for i in s:
        
        # Flip the ord(i)-97 bits in pre
        pre ^= (1 << ord(i) - 97)
  
        # Increment the count by hash[pre]
        count += hash.get(pre, 0)
  
        # Increment count of pre in hash
        hash[pre] = hash.get(pre, 0) + 1
          
    # Return the total count obtained
    return count
  
  
# Driver Code
  
S = "abbaa"
N = len(S)
print(subString(S, N))


输出:
4

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

高效方法:可以通过使用位掩码的概念来优化上述方法 字典。请按照以下步骤解决问题:

  • 初始化一个字典,说一个哈希来存储一个字符的数量。
  • 初始化两个变量,例如count0pre0,以存储每个字符具有偶数的子字符串的总数,并存储该子字符串中包含的字符掩码。
  • 遍历给定的字符串并执行以下步骤:
    • 将变量pre中的(S [i] –’a ‘)位翻转。
    • 通过递增散列[预]计数预先哈希计数。
  • 完成上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

Python3

# Python program for the above approach
  
# Function to count substrings having
# even frequency of each character
def subString(s, n):
  
    # Stores the count of a character
    hash = {0: 1}
  
    # Stores bitmask
    pre = 0
  
    # Stores the count of substrings
    # with even count of each character
    count = 0
  
    # Traverse the string S
    for i in s:
        
        # Flip the ord(i)-97 bits in pre
        pre ^= (1 << ord(i) - 97)
  
        # Increment the count by hash[pre]
        count += hash.get(pre, 0)
  
        # Increment count of pre in hash
        hash[pre] = hash.get(pre, 0) + 1
          
    # Return the total count obtained
    return count
  
  
# Driver Code
  
S = "abbaa"
N = len(S)
print(subString(S, N))
输出:
4

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