📜  给定字符串中双音子字符串

📅  最后修改于: 2021-09-08 12:34:21             🧑  作者: Mango

给定一个字符串str ,任务是计算给定字符串的所有双音子串。

例子:

方法:这个想法是生成给定字符串的所有可能的子串并检查每个子串是否是双音的。如果子字符串是 Bitonic,则增加答案的计数。最后,打印所有双调子数组的计数。

下面是上述方法的实现:

C++
// C+++ program for the above approach
#include 
using namespace std;
 
// Function to find all the bitonic
// sub strings
void subString(char str[], int n)
{
    // Pick starting point
    int c = 0;
 
    // Iterate till length of the string
    for (int len = 1; len <= n; len++) {
 
        // Pick ending point for string
        for (int i = 0; i <= n - len; i++) {
 
            // Substring from i to j
            // is obtained
            int j = i + len - 1;
            char temp = str[i], f = 0;
 
            // Substrings of length 1
            if (j == i) {
 
                // Increase count
                c++;
                continue;
            }
 
            int k = i + 1;
 
            // For increasing sequence
            while (temp < str[k] && k <= j) {
                temp = str[k];
                k++;
                f = 2;
            }
 
            // Check for strictly increasing
            if (k > j) {
 
                // Increase count
                c++;
                f = 2;
            }
 
            // Check for decreasing sequence
            while (temp > str[k]
                   && k <= j
                   && f != 2) {
                k++;
                f = 0;
            }
 
            if (k > j && f != 2) {
 
                // Increase count
                c++;
                f = 0;
            }
        }
    }
 
    // Print the result
    cout << c << endl;
}
 
// Driver Code
int main()
{
    // Given string
    char str[] = "bade";
 
    // Function Call
    subString(str, strlen(str));
    return 0;
}


Java
// Java+ program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find all the bitonic
// sub Strings
static void subString(char str[], int n)
{
     
    // Pick starting point
    int c = 0;
 
    // Iterate till length of the String
    for(int len = 1; len <= n; len++)
    {
         
        // Pick ending point for String
        for(int i = 0; i <= n - len; i++)
        {
             
            // SubString from i to j
            // is obtained
            int j = i + len - 1;
            char temp = str[i], f = 0;
 
            // SubStrings of length 1
            if (j == i)
            {
                 
                // Increase count
                c++;
                continue;
            }
 
            int k = i + 1;
 
            // For increasing sequence
            while (k < n && temp < str[k])
            {
                temp = str[k];
                k++;
                f = 2;
            }
 
            // Check for strictly increasing
            if (k > j)
            {
 
                // Increase count
                c++;
                f = 2;
            }
 
            // Check for decreasing sequence
            while (k < n && temp > str[k] &&
                   f != 2)
            {
                k++;
                f = 0;
            }
 
            if (k > j && f != 2)
            {
 
                // Increase count
                c++;
                f = 0;
            }
        }
    }
 
    // Print the result
    System.out.print(c + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String
    char str[] = "bade".toCharArray();
 
    // Function call
    subString(str, str.length);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find all the bitonic
# sub strings
def subString(str, n):
 
    # Pick starting po
    c = 0;
 
    # Iterate till length of the string
    for len in range(1, n + 1):
 
        # Pick ending pofor string
        for i in range(0, n - len + 1):
 
            # Substring from i to j
            # is obtained
            j = i + len - 1;
            temp = str[i]
            f = 0;
 
            # Substrings of length 1
            if (j == i):
 
                # Increase count
                c += 1
                continue;
            k = i + 1;
 
            # For increasing sequence
            while (k <= j and temp < str[k]):
                temp = str[k];
                k += 1;
                f = 2;
             
            # Check for strictly increasing
            if (k > j):
 
                # Increase count
                c += 1;
                f = 2;
             
            # Check for decreasing sequence
            while (k <= j and temp > str[k] and
                   f != 2):
                k += 1;
                f = 0;
            if (k > j and f != 2):
 
                # Increase count
                c += 1;
                f = 0;
                 
    # Print the result
    print(c)
 
# Driver code
 
# Given string
str = "bade";
 
# Function Call
subString(str, len(str))
     
# This code is contributed by grand_master


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find all the bitonic
// sub Strings
static void subString(char []str, int n)
{
     
    // Pick starting point
    int c = 0;
 
    // Iterate till length of the String
    for(int len = 1; len <= n; len++)
    {
         
        // Pick ending point for String
        for(int i = 0; i <= n - len; i++)
        {
             
            // SubString from i to j
            // is obtained
            int j = i + len - 1;
            char temp = str[i], f = (char)0;
 
            // SubStrings of length 1
            if (j == i)
            {
                 
                // Increase count
                c++;
                continue;
            }
 
            int k = i + 1;
 
            // For increasing sequence
            while (k < n && temp < str[k])
            {
                temp = str[k];
                k++;
                f = (char)2;
            }
 
            // Check for strictly increasing
            if (k > j)
            {
 
                // Increase count
                c++;
                f = (char)2;
            }
 
            // Check for decreasing sequence
            while (k < n && temp > str[k] &&
                f != 2)
            {
                k++;
                f = (char)0;
            }
 
            if (k > j && f != 2)
            {
 
                // Increase count
                c++;
                f = (char)0;
            }
        }
    }
 
    // Print the result
    Console.Write(c + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String
    char []str = "bade".ToCharArray();
 
    // Function call
    subString(str, str.Length);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
8

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live