📌  相关文章
📜  按字母顺序查找子字符串的计数

📅  最后修改于: 2022-05-13 01:57:08.381000             🧑  作者: Mango

按字母顺序查找子字符串的计数

给定一个长度的字符串N  由小写字母组成。任务是找出其字符按字母顺序出现的此类子字符串的数量。子字符串的最小允许长度为 2。
例子

Input : str = "refjhlmnbv"
Output : 2
Substrings are: "ef", "mn"

Input : str = "qwertyuiopasdfghjklzxcvbnm"
Output : 3

对于按字母顺序排列的子字符串,它的字符与它们在英文字母中出现的顺序相同。此外,此类子字符串中连续字符的 ASCII 值恰好相差 1。为了查找按字母顺序排列的子字符串的总数,遍历给定字符串并比较两个相邻字符,如果它们按字母顺序递增结果,然后找到字符串中与前一个字符不按字母顺序排列的下一个字符。
算法 :
迭代字符串长度:

  • 如果 str[i]+1 == str[i+1] -> 将结果增加 1 并迭代字符串直到下一个不按字母顺序排列的字符
  • 否则继续

下面是上述方法的实现:

C++
// CPP to find the number of substrings
// in alphabetical order
 
#include 
using namespace std;
 
// Function to find number of substrings
int findSubstringCount(string str)
{
    int result = 0;
    int n = str.size();
 
    // Iterate over string length
    for (int i = 0; i < n - 1; i++) {
        // if any two chars are in alphabetic order
        if (str[i] + 1 == str[i + 1]) {
            result++;
            // find next char not in order
            while (str[i] + 1 == str[i + 1]) {
                i++;
            }
        }
    }
 
    // return the result
    return result;
}
 
// Driver function
int main()
{
    string str = "alphabet";
 
    cout << findSubstringCount(str) << endl;
 
    return 0;
}


Java
// Java to find the number of substrings
// in alphabetical order
import java.util.*;
class Solution
{
   
// Function to find number of substrings
static int findSubstringCount(String str)
{
    int result = 0;
    int n = str.length();
   
    // Iterate over string length
    for (int i = 0; i < n - 1; i++) {
        // if any two chars are in alphabetic order
        if (str.charAt(i) + 1 == str.charAt(i+1)) {
            result++;
            // find next char not in order
            while (str.charAt(i) + 1 == str.charAt(i+1)) {
                i++;
            }
        }
    }
   
    // return the result
    return result;
}
   
// Driver function
public static void main(String args[])
{
    String str = "alphabet";
   
    System.out.println(findSubstringCount(str));
   
}
 
}
//contributed by Arnab Kundu


Python3
# Python3 to find the number of substrings
# in alphabetical order
 
# Function to find number of substrings
def findSubstringCount(str):
 
    result = 0
    n = len (str)
 
    # Iterate over string length
    for i in range (n - 1) :
         
        # if any two chars are in alphabetic order
        if (ord(str[i]) + 1 == ord(str[i + 1])) :
            result += 1
             
            # find next char not in order
            while (ord(str[i]) + 1 == ord(str[i + 1])) :
                i += 1
 
    # return the result
    return result
 
# Driver Code
if __name__ == "__main__":
 
    str = "alphabet"
 
    print(findSubstringCount(str))
 
# This code is contributed by ChitraNayal


C#
using System;
 
// C# to find the number of substrings 
// in alphabetical order 
public class Solution
{
 
// Function to find number of substrings 
public static int findSubstringCount(string str)
{
    int result = 0;
    int n = str.Length;
 
    // Iterate over string length 
    for (int i = 0; i < n - 1; i++)
    {
        // if any two chars are in alphabetic order 
        if ((char)(str[i] + 1) == str[i + 1])
        {
            result++;
            // find next char not in order 
            while ((char)(str[i] + 1) == str[i + 1])
            {
                i++;
            }
        }
    }
 
    // return the result 
    return result;
}
 
// Driver function 
public static void Main(string[] args)
{
    string str = "alphabet";
 
    Console.WriteLine(findSubstringCount(str));
 
}
 
}
 
// This code is contributed by Shrikant13


Javascript


输出:
1