📌  相关文章
📜  计算二进制字符串的子串,使得每个字符属于大小大于 1 的回文

📅  最后修改于: 2021-10-26 05:37:51             🧑  作者: Mango

给定二进制字符串str ,任务是计算给定字符串str的子串数,使得子串的每个字符都属于长度至少为 2 的回文子串。
例子:

方法:想法是计算每个字符不属于回文子串的子串,然后从大小大于 1 的字符串的可能子串总数中减去这个计数。 下面是步骤说明:

  • 可以观察到,如果我们取子串a 2 a 3 ….a k-1 (即没有开始和结束字符),那么它的每个字符可能属于某个回文。证明:
    If ai == ai-1 or ai == ai+1,
    Then it belongs to a palindrome of length 2.
    
    Otherwise, If ai != ai-1,
    ai != ai+1 and ai+1 == ai-1,
    Then, It belongs to a palindrome of size 3.
  • 因此,有四种子串模式,其中每个字符都不属于回文:
    1. “0111….11”
    2. “100……00”
    3. “111….110”
    4. “000….001”
  • 最后,从可能的长度大于 1 的子串总数中减去这个计数。

下面是上述方法的实现:

C++
// C++ implementation to find the
// substrings in binary string
// such that every character
// belongs to a palindrome
  
#include 
using namespace std;
  
// Function to to find the
// substrings in binary string
// such that every character
// belongs to a palindrome
int countSubstrings(string s)
{
    int n = s.length();
  
    // Total substrings
    int answer = (n * (n - 1)) / 2;
  
    int cnt = 1;
    vector v;
  
    // Loop to store the count of
    // continious characters in
    // the given string
    for (int i = 1; i < n; i++) {
  
        if (s[i] == s[i - 1])
            cnt++;
        else {
            v.push_back(cnt);
            cnt = 1;
        }
    }
  
    if (cnt > 0)
        v.push_back(cnt);
  
    // Subtract non special
    // strings from answer
    for (int i = 0;
         i < v.size() - 1; i++) {
        answer -= (v[i]
                   + v[i + 1]
                   - 1);
    }
  
    return answer;
}
  
// Driver Code
int main()
{
    // Given string s
    string s = "00111";
  
    // Function Call
    cout << countSubstrings(s);
    return 0;
}


Java
// Java implementation to find the     
// substrings in binary string     
// such that every character         
// belongs to a palindrome     
import java.util.*;
  
class GFG{
      
// Function to to find the         
// substrings in binary string         
// such that every character     
// belongs to a palindrome         
public static int countSubstrings(String s)         
{     
    int n = s.length();         
              
    // Total substrings         
    int answer = (n * (n - 1)) / 2;         
              
    int cnt = 1;     
    Vector v = new Vector();     
              
    // Loop to store the count of     
    // continious characters in         
    // the given string         
    for(int i = 1; i < n; i++)
    {     
        if (s.charAt(i) == s.charAt(i - 1))         
            cnt++;     
        else 
        {         
            v.add(cnt);         
            cnt = 1;     
        }     
    }     
    if (cnt > 0)         
        v.add(cnt);         
              
    // Subtract non special         
    // strings from answer         
    for(int i = 0; i < v.size() - 1; i++)
    {         
        answer -= (v.get(i) + 
                   v.get(i + 1) - 1);     
    }     
    return answer;         
} 
  
// Driver code
public static void main(String[] args)
{     
      
    // Given string s     
    String s = "00111";         
              
    // Function call     
    System.out.print(countSubstrings(s));     
}     
}     
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to find the
# substrings in binary string
# such that every character
# belongs to a palindrome
  
# Function to find the substrings in 
# binary string such that every 
# character belongs to a palindrome
def countSubstrings (s):
  
    n = len(s)
  
    # Total substrings
    answer = (n * (n - 1)) // 2
  
    cnt = 1
    v = []
  
    # Loop to store the count 
    # of continuous characters
    # in the given string
    for i in range(1, n):
        if (s[i] == s[i - 1]):
            cnt += 1
              
        else:
            v.append(cnt)
            cnt = 1
  
    if (cnt > 0):
        v.append(cnt)
  
    # Subtract non special strings
    # from answer
    for i in range(len(v) - 1):
        answer -= (v[i] + v[i + 1] - 1)
  
    return answer
  
# Driver Code
if __name__ == '__main__':
      
    # Given string 
    s = "00111"
  
    # Function call
    print(countSubstrings(s))
  
# This code is contributed by himanshu77


C#
// C# implementation to find the     
// substrings in binary string     
// such that every character         
// belongs to a palindrome     
using System;
using System.Collections.Generic;
  
class GFG{
      
// Function to to find the         
// substrings in binary string         
// such that every character     
// belongs to a palindrome         
public static int countSubstrings(String s)         
{     
    int n = s.Length;         
              
    // Total substrings         
    int answer = (n * (n - 1)) / 2;         
              
    int cnt = 1;     
    List v = new List();     
              
    // Loop to store the count of     
    // continious characters in         
    // the given string         
    for(int i = 1; i < n; i++)
    {     
        if (s[i] == s[i-1])         
            cnt++;     
        else 
        {         
            v.Add(cnt);         
            cnt = 1;     
        }     
    }     
    if (cnt > 0)         
        v.Add(cnt);         
              
    // Subtract non special         
    // strings from answer         
    for(int i = 0; i < v.Count - 1; i++)
    {         
        answer -= (v[i] + 
                   v[i + 1] - 1);     
    }     
    return answer;         
} 
  
// Driver code
public static void Main(String[] args)
{     
      
    // Given string s     
    String s = "00111";         
              
    // Function call     
    Console.Write(countSubstrings(s));     
}     
}     
  
// This code contributed by sapnasingh4991


Javascript


输出:
6

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程