📌  相关文章
📜  检查字符串可以拆分为以 N 开头后跟 N 个字符的子字符串

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

给定一个字符串str ,任务是检查它是否可以拆分为子字符串,以便每个子字符串以一个数值开头,后跟由该数字整数表示的多个字符。

例子:

方法:

    请按照以下步骤解决问题:
    • 检查无法拆分时的条件:
    • 如果给定的字符串不以数字开头。
    • 如果子串开头的整数大于剩余子串中后续字符的总数。
    • 如果不满足以上两个条件,答案肯定是可能的。因此递归地找到子串。

    下面是上述方法的实现:

    C++
    // C++ Program to implement the
    // above approach
    #include 
    using namespace std;
      
    // Function to check if the given
    // can be split into desired
    // substrings
    bool helper(string& s, int pos)
    {
        // Length of the string
        int len = s.size();
      
        if (pos >= len)
            return true;
      
        if (!isdigit(s[pos]))
            return false;
      
        int num = 0;
      
        // Traverse the string
        for (int i = pos; i < len; i++) {
      
            // Extract the digit
            num = num * 10 + s[pos] - '0';
      
            // Check if the extracted number
            // does not exceed the remaining
            // length
            if (i + 1 + num > len)
                return false;
      
            // Check for the remaining
            // string
            if (helper(s, i + 1 + num))
                return true;
        }
      
        // If generating desired
        // substrings is not possible
        return false;
    }
      
    // Driver Code
    int main()
    {
        string s = "123abc4db1c";
        if (helper(s, 0))
            cout << "Yes";
        else
            cout << "No";
    }


    Java
    // Java program to implement the 
    // above approach 
    import java.util.*;
      
    class GFG{
          
    // Function to check if the given 
    // can be split into desired 
    // substrings 
    public static boolean helper(String s, int pos) 
    { 
          
        // Length of the string 
        int len = s.length(); 
          
        if (pos >= len) 
            return true; 
        if (!Character.isDigit(s.charAt(pos)))
            return false; 
          
        int num = 0; 
          
        // Traverse the string 
        for(int i = pos; i < len; i++)
        {
              
           // Extract the digit 
           num = num * 10 + s.charAt(pos) - '0'; 
             
           // Check if the extracted number 
           // does not exceed the remaining 
           // length 
           if (i + 1 + num > len) 
               return false; 
             
           // Check for the remaining 
           // string 
           if (helper(s, i + 1 + num)) 
               return true; 
        } 
          
        // If generating desired 
        // substrings is not possible 
        return false; 
    } 
      
    // Driver code
    public static void main (String[] args)
    {
        String s = "123abc4db1c"; 
          
        if (helper(s, 0)) 
            System.out.print("Yes");
        else
            System.out.print("No");
    }
    }
      
    // This code is contributed by divyeshrabadiya07


    Python3
    # Python3 program to implement the
    # above approach
      
    # Function to check if the given
    # can be split into desired
    # substrings
    def helper(s, pos):
          
        # Length of the string
        size = len(s)
      
        if(pos >= size):
            return True
      
        if(s[pos].isdigit() == False):
            return False
      
        num = 0
      
        # Traverse the string
        for i in range(pos, size):
              
            # Extract the digit
            num = num * 10 + ord(s[pos]) - 48
      
            # Check if the extracted number
            # does not exceed the remaining
            # length
            if (i + 1 + num > size):
                return False
      
            # Check for the remaining
            # string
            if (helper(s, i + 1 + num)):
                return True
      
        # If generating desired
        # substrings is not possible
        return False
      
    # Driver Code
    s = "123abc4db1c";
      
    if (helper(s, 0)):
        print("Yes")
    else:
        print("No")
      
    # This code is contributed by Sanjit_Prasad


    C#
    // C# program to implement the 
    // above approach 
    using System;
      
    class GFG{
           
    // Function to check if the given 
    // can be split into desired 
    // substrings 
    public static bool helper(String s, int pos) 
    { 
           
        // Length of the string 
        int len = s.Length; 
           
        if (pos >= len) 
            return true; 
        if (!char.IsDigit(s[pos]))
            return false; 
           
        int num = 0; 
           
        // Traverse the string 
        for(int i = pos; i < len; i++)
        {
               
           // Extract the digit 
           num = num * 10 + s[pos] - '0'; 
              
           // Check if the extracted number 
           // does not exceed the remaining 
           // length 
           if (i + 1 + num > len) 
               return false; 
              
           // Check for the remaining 
           // string 
           if (helper(s, i + 1 + num)) 
               return true; 
        } 
           
        // If generating desired 
        // substrings is not possible 
        return false; 
    } 
       
    // Driver code
    public static void Main(String[] args)
    {
        String s = "123abc4db1c"; 
           
        if (helper(s, 0)) 
            Console.Write("Yes");
        else
            Console.Write("No");
    }
    }
      
    // This code is contributed by amal kumar choubey


    输出:
    Yes
    


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

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