📜  最小化拆分以从给定的字符串生成单调的子字符串

📅  最后修改于: 2021-10-26 05:47:05             🧑  作者: Mango

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

例子:

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

  • 初始化一个变量正在进行 = ‘N’以跟踪当前序列的顺序。
  • 迭代字符串和每个字符,请按照以下步骤操作:
  • 如果正在进行 == ‘N’
    • 如果curr_character < prev_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进行更新。
  • 最后,打印answer+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


Javascript


输出:
3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程