📌  相关文章
📜  最小化拆分以根据给定的字符串生成单调的子字符串

📅  最后修改于: 2021-04-29 10:56:23             🧑  作者: Mango

给定字符串str ,任务是找到给定字符串S可拆分为的最小子字符串数,以使每个子字符串单调递增或递减。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个进行中的变量=’N’以跟踪当前序列的顺序。
  • 遍历字符串,对于每个字符,请按照以下步骤操作:
  • 如果正在进行==’N’
    • 如果curr_character 则使用D (不增加)进行更新。
    • 否则,如果curr_character> prev_character ,则使用I (非递减)进行更新。
    • 否则,请使用N进行更新(既不增加也不减少)。
  • 否则,如果正在进行==’I’
    • 如果curr_character> prev_character然后更新正在进行中
    • 否则,如果curr_character 然后更新N和增量答案正在进行中
    • 否则,请使用I进行更新。
  • 否则请执行以下步骤:
    • 如果curr_character 然后更新与d正在进行
    • 否则,如果curr_character> prev_character然后更新N和增量答案正在进行中
    • 否则,请使用D进行更新。
  • 最后,打印答案+1是必需的答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
  
// Function to return final result
int minReqSubstring(string s, int n)
{
      
    // Initialize variable to keep
    // track of ongoing sequence
    char ongoing = 'N';
  
    int count = 0, l = s.size();
  
    for(int i = 1; i < l; i++)
    {
  
        // If current sequence is neither
        // increasing nor decreasing
        if (ongoing == 'N')
        {
  
            // If prev char is greater
            if (s[i] < s[i - 1]) 
            {
                ongoing = 'D';
            }
  
            // If prev char is same
            else if (s[i] == s[i - 1])
            {
                ongoing = 'N';
            }
  
            // Otherwise
            else 
            {
                ongoing = 'I';
            }
        }
  
        // If current sequence
        // is Non-Decreasing
        else if (ongoing == 'I')
        {
  
            // If prev char is smaller
            if (s[i] > s[i - 1])
            {
                ongoing = 'I';
            }
  
            // If prev char is same
            else if (s[i] == s[i - 1])
            {
                  
                // Update ongoing
                ongoing = 'I';
            }
  
            // Otherwise
            else
            {
  
                // Update ongoing
                ongoing = 'N';
  
                // Increase count
                count += 1;
            }
        }
  
        // If current sequence
        // is Non-Increasing
        else
        {
              
            // If prev char is greater,
            // then update ongoing with D
            if (s[i] < s[i - 1])
            {
                ongoing = 'D';
            }
  
            // If prev char is equal, then
            // update current with D
            else if (s[i] == s[i - 1])
            {
                ongoing = 'D';
            }
  
            // Otherwise, update ongoing
            // with N and increment count
            else
            {
                ongoing = 'N';
                count += 1;
            }
        }
    }
  
    // Return count+1
    return count + 1;
}
  
// Driver Code
int main()
{
    string S = "aeccdhba";
    int n = S.size();
      
    cout << (minReqSubstring(S, n));
    return 0;
}
  
// This code is contributed by Amit Katiyar


Java
// Java Program to implement
// the above approach
import java.util.*;
  
class GFG {
  
    // Function to return final result
    static int minReqSubstring(String s, int n)
    {
  
        // Initialize variable to keep
        // track of ongoing sequence
        char ongoing = 'N';
  
        int count = 0, l = s.length();
  
        for (int i = 1; i < l; i++) {
  
            // If current sequence is neither
            // increasing nor decreasing
            if (ongoing == 'N') {
  
                // If prev char is greater
                if (s.charAt(i) < s.charAt(i - 1)) {
                    ongoing = 'D';
                }
  
                // If prev char is same
                else if (s.charAt(i) == s.charAt(i - 1)) {
                    ongoing = 'N';
                }
  
                // Otherwise
                else {
                    ongoing = 'I';
                }
            }
  
            // If current sequence
            // is Non-Decreasing
            else if (ongoing == 'I') {
  
                // If prev char is smaller
                if (s.charAt(i) > s.charAt(i - 1)) {
                    ongoing = 'I';
                }
  
                // If prev char is same
                else if (s.charAt(i) == s.charAt(i - 1)) {
  
                    // Update ongoing
                    ongoing = 'I';
                }
  
                // Otherwise
                else {
  
                    // Update ongoing
                    ongoing = 'N';
  
                    // Increase count
                    count += 1;
                }
            }
  
            // If current sequence
            // is Non-Increasing
            else {
  
                // If prev char is greater,
                // then update ongoing with D
                if (s.charAt(i) < s.charAt(i - 1)) {
                    ongoing = 'D';
                }
  
                // If prev char is equal, then
                // update current with D
                else if (s.charAt(i) == s.charAt(i - 1)) {
                    ongoing = 'D';
                }
  
                // Otherwise, update ongoing
                // with N and increment count
                else {
                    ongoing = 'N';
                    count += 1;
                }
            }
        }
  
        // Return count+1
        return count + 1;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String S = "aeccdhba";
        int n = S.length();
        System.out.print(
            minReqSubstring(S, n));
    }
}


Python3
# Python3 program to implement 
# the above approach 
  
# Function to return final result 
def minReqSubstring(s, n):
      
    # Initialize variable to keep 
    # track of ongoing sequence 
    ongoing = 'N'
    count, l = 0, len(s)
      
    for i in range(1, l):
          
        # If current sequence is neither 
        # increasing nor decreasing 
        if ongoing == 'N':
              
            # If prev char is greater 
            if s[i] < s[i - 1]:
                ongoing = 'D'
                  
            # If prev char is same
            elif s[i] == s[i - 1]:
                ongoing = 'N'
                  
            # Otherwise 
            else:
                ongoing = 'I'
                  
        # If current sequence 
        # is Non-Decreasing 
        elif ongoing == 'I':
              
            # If prev char is smaller
            if s[i] > s[i - 1]:
                ongoing = 'I'
                  
            # If prev char is same 
            elif s[i] == s[i - 1]:
                  
                # Update ongoing
                ongoing = 'I'
              
            # Otherwise
            else:
                  
                # Update ongoing 
                ongoing = 'N'
                  
                # Increase count
                count += 1
              
        # If current sequence 
        # is Non-Increasing 
        else:
              
            # If prev char is greater, 
            # then update ongoing with D
            if s[i] < s[i - 1]:
                ongoing = 'D'
                  
            # If prev char is equal, then 
            # update current with D 
            elif s[i] == s[i - 1]:
                ongoing = 'D'
                  
            # Otherwise, update ongoing 
            # with N and increment count 
            else:
                ongoing = 'N'
                count += 1
                  
    # Return count + 1 
    return count + 1
  
# Driver code
S = "aeccdhba"
n = len(S)
  
print(minReqSubstring(S, n))
  
# This code is contributed by Stuti Pathak


C#
// C# Program to implement
// the above approach
using System;
class GFG{
  
  // Function to return readonly result
  static int minReqSubstring(String s, int n)
  {
  
    // Initialize variable to keep
    // track of ongoing sequence
    char ongoing = 'N';
  
    int count = 0, l = s.Length;
  
    for (int i = 1; i < l; i++) 
    {
  
      // If current sequence is neither
      // increasing nor decreasing
      if (ongoing == 'N')
      {
  
        // If prev char is greater
        if (s[i] < s[i - 1])
        {
          ongoing = 'D';
        }
  
        // If prev char is same
        else if (s[i] == s[i - 1]) 
        {
          ongoing = 'N';
        }
  
        // Otherwise
        else
        {
          ongoing = 'I';
        }
      }
  
      // If current sequence
      // is Non-Decreasing
      else if (ongoing == 'I') 
      {
  
        // If prev char is smaller
        if (s[i] > s[i - 1]) 
        {
          ongoing = 'I';
        }
  
        // If prev char is same
        else if (s[i] == s[i - 1])
        {
  
          // Update ongoing
          ongoing = 'I';
        }
  
        // Otherwise
        else 
        {
  
          // Update ongoing
          ongoing = 'N';
  
          // Increase count
          count += 1;
        }
      }
  
      // If current sequence
      // is Non-Increasing
      else 
      {
  
        // If prev char is greater,
        // then update ongoing with D
        if (s[i] < s[i - 1]) 
        {
          ongoing = 'D';
        }
  
        // If prev char is equal, then
        // update current with D
        else if (s[i] == s[i - 1]) 
        {
          ongoing = 'D';
        }
  
        // Otherwise, update ongoing
        // with N and increment count
        else 
        {
          ongoing = 'N';
          count += 1;
        }
      }
    }
  
    // Return count+1
    return count + 1;
  }
  
  // Driver Code
  public static void Main(String[] args)
  {
    String S = "aeccdhba";
    int n = S.Length;
    Console.Write(minReqSubstring(S, n));
  }
}
  
// This code is contributed by Rohit_ranjan


输出:
3

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