📌  相关文章
📜  使用范围外的字符可以形成范围 [L, R] 中的子字符串的方式数

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

给定一个字符串S和一个范围[L, R] 。的任务是找到的,其中所述子串在范围S [L,R]可以用所存在的字符串中,但不处在范围S [L,R]的字符被构造的方式的数目。
例子:

方法:该问题可以使用哈希表和组合来解决。可以按照以下步骤解决上述问题:

  • 计算哈希表中不在 L 和 R 范围内的每个字符的频率(比如频率)。
  • 分别从L到R迭代,计算路数。
  • 有效范围内的L和R的每一个字符,的方式的数量是由频率乘以[S [I] – “A”],并降低频率的值 1 [S [I]“A”]。
  • 如果freq[s[i]-‘a’]值为 0,则我们没有任何字符来填充该位置,因此路数将为 0。
  • 最后,整体乘法将是我们的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the number of
// ways to form the sub-string
int calculateWays(string s, int n, int l, int r)
{
 
    // Initialize a hash-table
    // with 0
    int freq[26];
    memset(freq, 0, sizeof freq);
 
    // Iterate in the string and count
    // the frequency of characters that
    // do not lie in the range L and R
    for (int i = 0; i < n; i++) {
 
        // Out of range characters
        if (i < l || i > r)
            freq[s[i] - 'a']++;
    }
 
    // Stores the final number of ways
    int ways = 1;
 
    // Iterate for the sub-string in the range
    // L and R
    for (int i = l; i <= r; i++) {
 
        // If exists then mulitply
        // the number of ways and
        // decrement the frequency
        if (freq[s[i] - 'a']) {
            ways = ways * freq[s[i] - 'a'];
            freq[s[i] - 'a']--;
        }
 
        // If does not exist
        // the sub-string cannot be formed
        else {
            ways = 0;
            break;
        }
    }
 
    // Return the answer
    return ways;
}
 
// Driver code
int main()
{
    string s = "cabcaab";
    int n = s.length();
 
    int l = 1, r = 3;
    cout << calculateWays(s, n, l, r);
 
    return 0;
}


Java
// Java implementation of the approach
class GfG {
 
// Function to return the number of
// ways to form the sub-string
static int calculateWays(String s, int n, int l, int r)
{
 
    // Initialize a hash-table
    // with 0
    int freq[] = new int[26];
 
    // Iterate in the string and count
    // the frequency of characters that
    // do not lie in the range L and R
    for (int i = 0; i < n; i++) {
 
        // Out of range characters
        if (i < l || i > r)
            freq[s.charAt(i)-'a']++;
    }
 
    // Stores the final number of ways
    int ways = 1;
 
    // Iterate for the sub-string in the range
    // L and R
    for (int i = l; i <= r; i++) {
 
        // If exists then mulitply
        // the number of ways and
        // decrement the frequency
        if (freq[s.charAt(i) - 'a'] != 0) {
            ways = ways * freq[s.charAt(i) - 'a'];
            freq[s.charAt(i) - 'a']--;
        }
 
        // If does not exist
        // the sub-string cannot be formed
        else {
            ways = 0;
            break;
        }
    }
 
    // Return the answer
    return ways;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "cabcaab";
    int n = s.length();
 
    int l = 1, r = 3;
    System.out.println(calculateWays(s, n, l, r));
 
}
}


Python3
# Python 3 implementation of the approach
 
# Function to return the number of
# ways to form the sub-string
def calculateWays(s, n, l, r):
     
    # Initialize a hash-table
    # with 0
    freq = [0 for i in range(26)]
 
    # Iterate in the string and count
    # the frequency of characters that
    # do not lie in the range L and R
    for i in range(n):
         
        # Out of range characters
        if (i < l or i > r):
            freq[ord(s[i]) - ord('a')] += 1
 
    # Stores the final number of ways
    ways = 1
 
    # Iterate for the sub-string in the range
    # L and R
    for i in range(l, r + 1, 1):
         
        # If exists then mulitply
        # the number of ways and
        # decrement the frequency
        if (freq[ord(s[i]) - ord('a')]):
            ways = ways * freq[ord(s[i]) - ord('a')]
            freq[ord(s[i]) - ord('a')] -= 1
 
        # If does not exist
        # the sub-string cannot be formed
        else:
            ways = 0
            break
 
    # Return the answer
    return ways
 
# Driver code
if __name__ == '__main__':
    s = "cabcaab"
    n = len(s)
 
    l = 1
    r = 3
    print(calculateWays(s, n, l, r))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GfG
{
 
// Function to return the number of
// ways to form the sub-string
static int calculateWays(String s, int n, int l, int r)
{
 
    // Initialize a hash-table
    // with 0
    int []freq = new int[26];
 
    // Iterate in the string and count
    // the frequency of characters that
    // do not lie in the range L and R
    for (int i = 0; i < n; i++)
    {
 
        // Out of range characters
        if (i < l || i > r)
            freq[s[i]-'a']++;
    }
 
    // Stores the final number of ways
    int ways = 1;
 
    // Iterate for the sub-string in the range
    // L and R
    for (int i = l; i <= r; i++)
    {
 
        // If exists then mulitply
        // the number of ways and
        // decrement the frequency
        if (freq[s[i] - 'a'] != 0) {
            ways = ways * freq[s[i] - 'a'];
            freq[s[i] - 'a']--;
        }
 
        // If does not exist
        // the sub-string cannot be formed
        else {
            ways = 0;
            break;
        }
    }
 
    // Return the answer
    return ways;
}
 
// Driver code
public static void Main()
{
    String s = "cabcaab";
    int n = s.Length;
 
    int l = 1, r = 3;
    Console.WriteLine(calculateWays(s, n, l, r));
 
}
}
 
/* This code contributed by PrinciRaj1992 */


PHP
 $r)
            $freq[ord($s[$i]) - 97]++;
    }
 
    // Stores the final number of ways
    $ways = 1;
 
    // Iterate for the sub-string in the range
    // L and R
    for ($i = $l; $i <= $r; $i++)
    {
 
        // If exists then mulitply
        // the number of ways and
        // decrement the frequency
        if ($freq[ord($s[$i]) - 97])
        {
            $ways = $ways * $freq[ord($s[$i]) - 97];
            $freq[ord($s[$i]) - 97]--;
        }
 
        // If does not exist
        // the sub-string cannot be formed
        else
        {
            $ways = 0;
            break;
        }
    }
 
    // Return the answer
    return $ways;
}
 
// Driver code
$s = "cabcaab";
$n = strlen($s);
 
$l = 1;
$r = 3;
echo calculateWays($s, $n, $l, $r);
 
// This code is contributed by ihritik
?>


Javascript


输出:
2

时间复杂度: O(N),其中 N 是字符串的长度。
辅助空间: O(1)

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