📜  给定琴弦中反双音子弦的计数

📅  最后修改于: 2021-05-06 09:09:11             🧑  作者: Mango

给定字符串S ,任务是计算给定字符串中反向双音子字符串。

例子:

方法 :
该方法是生成给定字符串的所有可能的子字符串,并对每个子字符串执行以下步骤以解决问题:

  • 遍历字符串,对于每个字符,检查下一个字符的ASCII值是否小于当前字符的ASCII值。
  • 如果在任何时候下一个字符的ASCII值都大于当前字符的ASCII值,请从该索引开始遍历,然后从现在开始对于每个字符,请检查下一个字符的ASCII值是否大于ASCII值是否为当前字符。
  • 如果在任何时候到达子字符串的结尾之前,下一个字符的ASCII值小于当前字符的ASCII值,则忽略该子字符串。
  • 如果到达子字符串的末尾,请增加count
  • 对所有子字符串完成上述所有步骤之后,打印count的最终值

下面是上述方法的实现:

C++
// C++ Program to implement 
// the above approach 
#include  
using namespace std; 
  
// Function to calculate the number 
// of reverse bitonic substrings 
int CountsubString(char str[], int n) 
{ 
    // Stores the count 
    int c = 0; 
  
    // All possible lengths of substrings 
    for (int len = 1; len <= n; len++) { 
  
        // Starting point of a substring 
        for (int i = 0; i <= n - len; i++) { 
  
            // Ending point of a substring 
            int j = i + len - 1; 
  
            char temp = str[i], f = 0; 
  
            // Condition for reverse 
            // bitonic substrings of 
            // length 1 
            if (j == i) { 
  
                c++; 
                continue; 
            } 
  
            int k = i + 1; 
  
            // Check for decreasing sequence 
            while (temp > str[k] && k <= j) { 
  
                temp = str[k]; 
  
                k++; 
            } 
  
            // If end of substring 
            // is reached 
            if (k > j) { 
                c++; 
                f = 2; 
            } 
  
            // For increasing sequence 
            while (temp < str[k] && k <= j 
                && f != 2) { 
  
                temp = str[k]; 
  
                k++; 
            } 
  
            // If end of substring 
            // is reached 
            if (k > j && f != 2) { 
                c++; 
                f = 0; 
            } 
        } 
    } 
  
    // Return the number 
    // of bitonic substrings 
    return c; 
} 
  
// Driver Code 
int main() 
{ 
    char str[] = "bade"; 
    cout << CountsubString(str, strlen(str)); 
  
    return 0; 
}


Java
// Java program to implement 
// the above approach 
class GFG{
      
// Function to calculate the number 
// of reverse bitonic substrings 
public static int CountsubString(char[] str,
                                 int n) 
{ 
      
    // Stores the count 
    int c = 0; 
  
    // All possible lengths of substrings 
    for(int len = 1; len <= n; len++)
    { 
          
        // Starting point of a substring 
        for(int i = 0; i <= n - len; i++)
        { 
              
            // Ending point of a substring 
            int j = i + len - 1; 
  
            char temp = str[i], f = 0; 
  
            // Condition for reverse 
            // bitonic substrings of 
            // length 1 
            if (j == i)
            { 
                c++; 
                continue; 
            } 
  
            int k = i + 1; 
  
            // Check for decreasing sequence 
            while (temp > str[k] && k <= j)
            { 
                temp = str[k]; 
                k++; 
            } 
  
            // If end of substring 
            // is reached 
            if (k > j)
            { 
                c++; 
                f = 2; 
            } 
  
            // For increasing sequence 
            while (k <= j && temp < str[k] &&
                   f != 2) 
            { 
                temp = str[k]; 
                k++; 
            } 
  
            // If end of substring 
            // is reached 
            if (k > j && f != 2)
            { 
                c++; 
                f = 0; 
            } 
        } 
    } 
  
    // Return the number 
    // of bitonic substrings 
    return c; 
} 
  
// Driver code
public static void main(String[] args)
{
    char str[] = { 'b', 'a', 'd', 'e' }; 
      
    System.out.println(CountsubString(
                       str, str.length));
}
}
  
// This cdoe is contributed by divyeshrabadiya07


Python3
# Python3 program to implement 
# the above approach 
  
# Function to calculate the number 
# of reverse bitonic substrrings 
def CountsubString(strr, n): 
      
    # Stores the count 
    c = 0
  
    # All possible lengths of substrrings 
    for len in range(n + 1): 
  
        # Starting poof a substrring 
        for i in range(n - len): 
  
            # Ending poof a substrring 
            j = i + len - 1
  
            temp = strr[i] 
            f = 0
  
            # Condition for reverse 
            # bitonic substrrings of 
            # length 1 
            if (j == i): 
                c += 1
                continue
  
            k = i + 1
  
            # Check for decreasing sequence 
            while (k <= j and temp > strr[k]): 
                temp = strr[k] 
                k += 1
  
            # If end of substrring 
            # is reache 
            if (k > j): 
                c += 1
                f = 2
  
            # For increasing sequence 
            while (k <= j and f != 2 and
                temp < strr[k]): 
                temp = strr[k] 
                k += 1
  
            # If end of substrring 
            # is reached 
            if (k > j and f != 2): 
                c += 1
                f = 0
  
    # Return the number 
    # of bitonic substrrings 
    return c 
  
# Driver Code 
if __name__ == '__main__': 
      
    strr = "bade"
    print(CountsubString(strr, len(strr))) 
      
# This code is contributed by mohit kumar 29


C#
// C# program to implement 
// the above approach 
using System;
  
class GFG{
      
// Function to calculate the number 
// of reverse bitonic substrings 
public static int CountsubString(char[] str,
                                 int n) 
{ 
      
    // Stores the count 
    int c = 0; 
  
    // All possible lengths of substrings 
    for(int len = 1; len <= n; len++)
    { 
        
        // Starting point of a substring 
        for(int i = 0; i <= n - len; i++)
        { 
              
            // Ending point of a substring 
            int j = i + len - 1; 
  
            char temp = str[i], f = '0'; 
  
            // Condition for reverse 
            // bitonic substrings of 
            // length 1 
            if (j == i)
            { 
                c++; 
                continue; 
            } 
  
            int k = i + 1; 
  
            // Check for decreasing sequence 
            while (temp > str[k] && k <= j)
            { 
                temp = str[k]; 
                k++; 
            } 
  
            // If end of substring 
            // is reached 
            if (k > j)
            { 
                c++; 
                f = '2'; 
            } 
  
            // For increasing sequence 
            while (k <= j && temp < str[k] &&
                   f != '2') 
            { 
                temp = str[k]; 
                k++; 
            } 
  
            // If end of substring 
            // is reached 
            if (k > j && f != 2)
            { 
                c++; 
                f = '0'; 
            } 
        } 
    } 
  
    // Return the number 
    // of bitonic substrings 
    return c; 
} 
  
// Driver code
public static void Main(String[] args)
{
    char []str = { 'b', 'a', 'd', 'e' }; 
      
    Console.WriteLine(CountsubString(
                      str, str.Length) - 1);
}
}
  
// This code is contributed by amal kumar choubey


输出:

10

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