📌  相关文章
📜  在两个给定字符串之间按字典顺序存在的相同长度字符串的计数

📅  最后修改于: 2021-09-07 02:35:25             🧑  作者: Mango

给定两个长度为L 的字符串S1S2 ,任务是计算长度为 L 的字符串的数量,它们存在于 S1 和 S2 之间,在字典上大于 S1 但小于 S2。
例子:

方法:

  1. 首先,找出字符串字典序比第一字符串小的数目S1,如:
    Let the String S1 of length L 
    be represented as c0c1c2...cL-1
    where ci is the character in S1 at index i
    
    Therefore, To get the number of strings less than S1,
    we will calculate it as 
    N(S1) = (number of letters less than c0 * 26L-1)
          + (number of letters less than c1 * 26L-2)
          + (number of letters less than c2 * 26L-3)
          +  ... 
          + (number of letters less than cL-2 * 26)
          + (number of letters less than cL-1)

    例如:

    Let S1 = "cbd"
    
    Number of strings less than S1
    N(S1) = (number of letters less than 'c' * 262)
          + (number of letters less than 'b' * 26)
          + (number of letters less than 'd')
    
    N(S1) = (2 * 26 * 26) + (1 * 26) + (3) 
          = 1352 + 26 + 3 = 1381.
  2. 同理,找出字典序小于 S2 的字符串个数。
  3. 然后只要找出以上两个值的差值,就可以得到按字典序大于S1但小于S2的字符串个数。

下面是上述方法的实现:

C++
// C++ program to find the count of
// same length Strings that exists lexicographically
// in between two given Strings
  
  
#include 
using namespace std;
  
// Function to find the count of strings less
// than given string lexicographically
int LexicoLesserStrings(string s)
{
    int count = 0;
    int len;
  
    // Find length of string s
    len = s.size();
  
    // Looping over the string characters and
    // finding strings less than that character
    for (int i = 0; i < len; i++) {
        count += (s[i] - 'a')
                * pow(26, len - i - 1);
    }
  
    return count;
}
  
// Function to find the count of
// same length Strings that exists
// lexicographically in between two given Strings
int countString(string S1, string S2)
{
    int countS1, countS2, totalString;
  
    // Count string less than S1
    countS1 = LexicoLesserStrings(S1);
  
    // Count string less than S2
    countS2 = LexicoLesserStrings(S2);
  
    // Total strings between S1 and S2 would
    // be difference between the counts - 1
    totalString = countS2 - countS1 - 1;
  
    // If S1 is lexicographically greater
    // than S2 then return 0, otherwise return
    // the value of totalString
    return (totalString < 0 ? 0 : totalString);
}
  
// Driver code
int main()
{
    string S1, S2;
    S1 = "cda";
    S2 = "cef";
  
    cout << countString(S1, S2);
    return 0;
}


Java
// Java program to find the count of same length
// Strings that exists lexicographically
// in between two given Strings
import java.util.*;
  
class GFG{
  
// Function to find the count of strings less
// than given string lexicographically
static int LexicoLesserStrings(String s)
{
    int count = 0;
    int len;
  
    // Find length of string s
    len = s.length();
  
    // Looping over the string characters and
    // finding strings less than that character
    for(int i = 0; i < len; i++)
    {
        count += (s.charAt(i) - 'a') *
                Math.pow(26, len - i - 1);
    }
    return count;
}
  
// Function to find the count of
// same length Strings that exists
// lexicographically in between two
// given Strings
static int countString(String S1, String S2)
{
    int countS1, countS2, totalString;
  
    // Count string less than S1
    countS1 = LexicoLesserStrings(S1);
  
    // Count string less than S2
    countS2 = LexicoLesserStrings(S2);
  
    // Total strings between S1 and S2 would
    // be difference between the counts - 1
    totalString = countS2 - countS1 - 1;
  
    // If S1 is lexicographically greater
    // than S2 then return 0, otherwise return
    // the value of totalString
    return (totalString < 0 ? 0 : totalString);
}
  
// Driver code
public static void main(String args[])
{
    String S1, S2;
    S1 = "cda";
    S2 = "cef";
  
    System.out.println(countString(S1, S2));
}
}
  
// This code is contributed by apurva raj


Python3
# Python3 program to find the count of same
# length Strings that exists lexicographically
# in between two given Strings
  
# Function to find the count of strings less
# than given string lexicographically
def LexicoLesserStrings(s):
      
    count = 0
  
    # Find length of string s
    length = len(s)
  
    # Looping over the string characters and
    # finding strings less than that character
    for i in range(length):
        count += ((ord(s[i]) - ord('a')) *
                pow(26, length - i - 1))
                      
    return count
  
# Function to find the count of
# same length Strings that exists
# lexicographically in between two
# given Strings
def countString(S1, S2):
  
    # Count string less than S1
    countS1 = LexicoLesserStrings(S1)
  
    # Count string less than S2
    countS2 = LexicoLesserStrings(S2)
  
    # Total strings between S1 and S2 would
    # be difference between the counts - 1
    totalString = countS2 - countS1 - 1;
  
    # If S1 is lexicographically greater
    # than S2 then return 0, otherwise return
    # the value of totalString
    return (0 if totalString < 0 else totalString)
  
# Driver code
S1 = "cda";
S2 = "cef";
  
print(countString(S1, S2))
  
# This code is contributed by apurva raj


C#
// C# program to find the count of same length
// Strings that exists lexicographically
// in between two given Strings
using System;
  
class GFG{
  
// Function to find the count of strings less
// than given string lexicographically
static int LexicoLesserStrings(String s)
{
    int count = 0;
    int len;
  
    // Find length of string s
    len = s.Length;
  
    // Looping over the string characters and
    // finding strings less than that character
    for(int i = 0; i < len; i++)
    {
        count += ((s[i] - 'a') *
                (int)Math.Pow(26, len - i - 1));
    }
    return count;
}
  
// Function to find the count of
// same length Strings that exists
// lexicographically in between two
// given Strings
static int countString(String S1, String S2)
{
    int countS1, countS2, totalString;
  
    // Count string less than S1
    countS1 = LexicoLesserStrings(S1);
  
    // Count string less than S2
    countS2 = LexicoLesserStrings(S2);
  
    // Total strings between S1 and S2 would
    // be difference between the counts - 1
    totalString = countS2 - countS1 - 1;
  
    // If S1 is lexicographically greater
    // than S2 then return 0, otherwise return
    // the value of totalString
    return (totalString < 0 ? 0 : totalString);
}
  
// Driver code
public static void Main()
{
    String S1, S2;
    S1 = "cda";
    S2 = "cef";
  
    Console.Write(countString(S1, S2));
}
}
  
// This code is contributed by chitranayal


Javascript


输出:
30

性能分析:
时间复杂度:在上述方法中,我们循环遍历两个长度为 N 的字符串,因此需要O(N)时间,其中N是每个字符串的长度。
辅助空间复杂度:由于在上述方法中没有使用额外的空间,因此辅助空间复杂度将为O(1)