Minimize splits to generate monotonous Substrings from given String
Last Updated :
19 May, 2021
Given a string str, the task is to find the minimum numbers of substrings that the given string S can be split into, such that each substring is monotonously increasing or decreasing.
Examples:
Input: str = “abcdcba”
Output: 2
Explanation:
The string can be split into a minimum of 2 monotonous substrings {“abcd”(increasing), “cba”(decreasing)}
Input: str = “aeccdhba”
Output: 3
Explanation:
The generated substrings are {“ae”, “ccdh”, “ba”}
Approach: Follow the steps below to solve the problem:
- Initialize a variable ongoing = ‘N’ to keep track of order of current sequence.
- Iterate over the string and for each character, follow the steps below:
- If ongoing == ‘N’:
- If curr_character < prev_character then update ongoing with D(Non-Increasing).
- Otherwise, if curr_character > prev_character, then update ongoing with I(Non-Decreasing).
- Otherwise, update ongoing with N(neither Non-Increasing nor Non-Decreasing).
- Otherwise, if ongoing == ‘I’:
- If curr_character > prev_character then update ongoing with I.
- Otherwise, if curr_character < prev_character then update ongoing with N and increment answer.
- Otherwise, update ongoing with I.
- else do the following steps:
- If curr_character < prev_character then update ongoing with D.
- Otherwise, if curr_character > prev_character then update ongoing with N and increment answer.
- Otherwise, update ongoing with D.
- Finally, print answer+1 is the required answer.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int minReqSubstring(string s, int n)
{
char ongoing = 'N' ;
int count = 0, l = s.size();
for ( int i = 1; i < l; i++)
{
if (ongoing == 'N' )
{
if (s[i] < s[i - 1])
{
ongoing = 'D' ;
}
else if (s[i] == s[i - 1])
{
ongoing = 'N' ;
}
else
{
ongoing = 'I' ;
}
}
else if (ongoing == 'I' )
{
if (s[i] > s[i - 1])
{
ongoing = 'I' ;
}
else if (s[i] == s[i - 1])
{
ongoing = 'I' ;
}
else
{
ongoing = 'N' ;
count += 1;
}
}
else
{
if (s[i] < s[i - 1])
{
ongoing = 'D' ;
}
else if (s[i] == s[i - 1])
{
ongoing = 'D' ;
}
else
{
ongoing = 'N' ;
count += 1;
}
}
}
return count + 1;
}
int main()
{
string S = "aeccdhba" ;
int n = S.size();
cout << (minReqSubstring(S, n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minReqSubstring(String s, int n)
{
char ongoing = 'N' ;
int count = 0 , l = s.length();
for ( int i = 1 ; i < l; i++) {
if (ongoing == 'N' ) {
if (s.charAt(i) < s.charAt(i - 1 )) {
ongoing = 'D' ;
}
else if (s.charAt(i) == s.charAt(i - 1 )) {
ongoing = 'N' ;
}
else {
ongoing = 'I' ;
}
}
else if (ongoing == 'I' ) {
if (s.charAt(i) > s.charAt(i - 1 )) {
ongoing = 'I' ;
}
else if (s.charAt(i) == s.charAt(i - 1 )) {
ongoing = 'I' ;
}
else {
ongoing = 'N' ;
count += 1 ;
}
}
else {
if (s.charAt(i) < s.charAt(i - 1 )) {
ongoing = 'D' ;
}
else if (s.charAt(i) == s.charAt(i - 1 )) {
ongoing = 'D' ;
}
else {
ongoing = 'N' ;
count += 1 ;
}
}
}
return count + 1 ;
}
public static void main(String[] args)
{
String S = "aeccdhba" ;
int n = S.length();
System.out.print(
minReqSubstring(S, n));
}
}
|
Python3
def minReqSubstring(s, n):
ongoing = 'N'
count, l = 0 , len (s)
for i in range ( 1 , l):
if ongoing = = 'N' :
if s[i] < s[i - 1 ]:
ongoing = 'D'
elif s[i] = = s[i - 1 ]:
ongoing = 'N'
else :
ongoing = 'I'
elif ongoing = = 'I' :
if s[i] > s[i - 1 ]:
ongoing = 'I'
elif s[i] = = s[i - 1 ]:
ongoing = 'I'
else :
ongoing = 'N'
count + = 1
else :
if s[i] < s[i - 1 ]:
ongoing = 'D'
elif s[i] = = s[i - 1 ]:
ongoing = 'D'
else :
ongoing = 'N'
count + = 1
return count + 1
S = "aeccdhba"
n = len (S)
print (minReqSubstring(S, n))
|
C#
using System;
class GFG{
static int minReqSubstring(String s, int n)
{
char ongoing = 'N' ;
int count = 0, l = s.Length;
for ( int i = 1; i < l; i++)
{
if (ongoing == 'N' )
{
if (s[i] < s[i - 1])
{
ongoing = 'D' ;
}
else if (s[i] == s[i - 1])
{
ongoing = 'N' ;
}
else
{
ongoing = 'I' ;
}
}
else if (ongoing == 'I' )
{
if (s[i] > s[i - 1])
{
ongoing = 'I' ;
}
else if (s[i] == s[i - 1])
{
ongoing = 'I' ;
}
else
{
ongoing = 'N' ;
count += 1;
}
}
else
{
if (s[i] < s[i - 1])
{
ongoing = 'D' ;
}
else if (s[i] == s[i - 1])
{
ongoing = 'D' ;
}
else
{
ongoing = 'N' ;
count += 1;
}
}
}
return count + 1;
}
public static void Main(String[] args)
{
String S = "aeccdhba" ;
int n = S.Length;
Console.Write(minReqSubstring(S, n));
}
}
|
Javascript
<script>
function minReqSubstring(s, n)
{
let ongoing = 'N' ;
let count = 0, l = s.length;
for (let i = 1; i < l; i++) {
if (ongoing == 'N' ) {
if (s[i] < s[i - 1]) {
ongoing = 'D' ;
}
else if (s[i] == s[i - 1]) {
ongoing = 'N' ;
}
else {
ongoing = 'I' ;
}
}
else if (ongoing == 'I' ) {
if (s[i] > s[i - 1]) {
ongoing = 'I' ;
}
else if (s[i] == s[i - 1]) {
ongoing = 'I' ;
}
else {
ongoing = 'N' ;
count += 1;
}
}
else {
if (s[i] < s[i - 1]) {
ongoing = 'D' ;
}
else if (s[i] == s[i - 1]) {
ongoing = 'D' ;
}
else {
ongoing = 'N' ;
count += 1;
}
}
}
return count + 1;
}
let S = "aeccdhba" ;
let n = S.length;
document.write(
minReqSubstring(S, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...