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

📅  最后修改于: 2021-04-21 22:00:53             🧑  作者: 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)