📜  计算环绕字符串存在的字符串S 的唯一子字符串

📅  最后修改于: 2021-10-27 08:06:25             🧑  作者: Mango

给定一个字符串S ,它是字符串“abcdefghijklmnopqrstuvwxyz”的无限环绕字符串,任务是计算s中存在的字符串p的唯一非空子串的数量。

例子:

处理方法:按照以下步骤解决问题

  1. 遍历字符串的每个字符
  2. 初始化大小为26的辅助数组arr[] ,以存储字符串S 中从字符串P 的每个字符开始的子字符串的当前长度。
  3. 初始化一个变量,比如curLen ,如果当前字符不是前一个子字符串的一部分,它存储P 中存在的子字符串的长度,包括当前字符。
  4. 初始化一个变量,比如ans ,以存储S 中存在的p的非空子串的唯一计数。
  5. 遍历字符串并检查以下两种情况的字符:
    • 检查当前字符可以与前一个子字符串相加以形成所需的子字符串。
    • 如果(curLen + 1)大于arr[curr]则将 curLenarr[curr]的差值添加到ans 中,以避免重复子串。
  6. 打印ans的值。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
 
#include 
using namespace std;
 
// Function to find the count of
// non-empty substrings of p present in s
int findSubstringInWraproundString(string p)
{
    // Stores the required answer
    int ans = 0;
 
    // Stores the length of
    // substring present in p
    int curLen = 0;
 
    // Stores the current length
    // of substring that is
    // present in string s starting
    // from each character of p
    int arr[26] = { 0 };
 
    // Iterate over the characters of the string
    for (int i = 0; i < (int)p.length(); i++) {
 
        int curr = p[i] - 'a';
 
        // Check if the current character
        // can be added with previous substring
        // to form the required substring
        if (i > 0
            && (p[i - 1]
                != ((curr + 26 - 1) % 26 + 'a'))) {
            curLen = 0;
        }
 
        // Increment current length
        curLen++;
 
        if (curLen > arr[curr]) {
 
            // To avoid repetition
            ans += (curLen - arr[curr]);
 
            // Update arr[cur]
            arr[curr] = curLen;
        }
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    string p = "zab";
 
    // Function call to find the
    // count of non-empty substrings
    // of p present in s
    findSubstringInWraproundString(p);
 
    return 0;
}


Java
import java.util.*;
class GFG
{
   
    // Function to find the count of
    // non-empty substrings of p present in s
    static void findSubstringInWraproundString(String p)
    {
       
        // Stores the required answer
        int ans = 0;
 
        // Stores the length of
        // substring present in p
        int curLen = 0;
 
        // Stores the current length
        // of substring that is
        // present in string s starting
        // from each character of p
        int arr[] = new int[26];
 
        // Iterate over the characters of the string
        for (int i = 0; i < p.length(); i++)
        {
 
            int curr = p.charAt(i) - 'a';
 
            // Check if the current character
            // can be added with previous substring
            // to form the required substring
            if (i > 0
                && (p.charAt(i - 1)
                    != ((curr + 26 - 1) % 26 + 'a')))
            {
                curLen = 0;
            }
 
            // Increment current length
            curLen++;
            if (curLen > arr[curr])
            {
 
                // To avoid repetition
                ans += (curLen - arr[curr]);
 
                // Update arr[cur]
                arr[curr] = curLen;
            }
        }
 
        // Print the answer
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        String p = "zab";
 
        // Function call to find the
        // count of non-empty substrings
        // of p present in s
        findSubstringInWraproundString(p);
    }
}
 
// This code is contributed by hemanth gadarla


Python3
# Python3 program for
# the above approach
 
# Function to find the count of
# non-empty substrings of p present in s
def findSubstringInWraproundString(p) :
 
    # Stores the required answer
    ans = 0
  
    # Stores the length of
    # substring present in p
    curLen = 0
  
    # Stores the current length
    # of substring that is
    # present in string s starting
    # from each character of p
    arr = [0]*26
  
    # Iterate over the characters of the string
    for i in range(0, len(p)) :
  
        curr = ord(p[i]) - ord('a')
  
        # Check if the current character
        # can be added with previous substring
        # to form the required substring
        if (i > 0 and (ord(p[i - 1]) != ((curr + 26 - 1) % 26 + ord('a')))) :
            curLen = 0
  
        # Increment current length
        curLen += 1
  
        if (curLen > arr[curr]) :
  
            # To avoid repetition
            ans += (curLen - arr[curr])
  
            # Update arr[cur]
            arr[curr] = curLen
  
    # Print the answer
    print(ans)
     
p = "zab"
  
# Function call to find the
# count of non-empty substrings
# of p present in s
findSubstringInWraproundString(p)
 
# This code is contributed by divyeshrabadiya07.


C#
// C# program for
// the above approach
using System;
class GFG
{
 
  // Function to find the count of
  // non-empty substrings of p present in s
  static void findSubstringInWraproundString(string p)
  {
     
    // Stores the required answer
    int ans = 0;
 
    // Stores the length of
    // substring present in p
    int curLen = 0;
 
    // Stores the current length
    // of substring that is
    // present in string s starting
    // from each character of p
    int[] arr = new int[26];
 
    // Iterate over the characters of the string
    for (int i = 0; i < (int)p.Length; i++)
    {
      int curr = p[i] - 'a';
 
      // Check if the current character
      // can be added with previous substring
      // to form the required substring
      if (i > 0 && (p[i - 1] != ((curr + 26 - 1) % 26 + 'a')))
      {
        curLen = 0;
      }
 
      // Increment current length
      curLen++;
      if (curLen > arr[curr])
      {
 
        // To avoid repetition
        ans += (curLen - arr[curr]);
 
        // Update arr[cur]
        arr[curr] = curLen;
      }
    }
 
    // Print the answer
    Console.Write(ans);
  }
 
  // Driver code
  static void Main()
  {
    string p = "zab";
 
    // Function call to find the
    // count of non-empty substrings
    // of p present in s
    findSubstringInWraproundString(p);
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:
6

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

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