Minimize splits to generate monotonous Substrings from given String | Function to return final result ; Initialize variable to keep track of ongoing sequence ; If current sequence is neither increasing nor decreasing ; If prev char is greater ; If prev char is same ; Otherwise ; If current sequence is Non - Decreasing ; If prev char is smaller ; If prev char is same ; Update ongoing ; Otherwise ; Update ongoing ; Increase count ; If current sequence is Non - Increasing ; If prev char is greater , then update ongoing with D ; If prev char is equal , then update current with D ; Otherwise , update ongoing with N and increment count ; Return count + 1 ; Driver Code
function minReqSubstring ( s , n ) { let ongoing = ' ' ; let count = 0 , l = s . length ; for ( let i = 1 ; i < l ; i ++ ) { if ( ongoing == ' ' ) { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' ' ; } else { ongoing = ' ' ; } } else if ( ongoing == ' ' ) { if ( s [ i ] > s [ i - 1 ] ) { ongoing = ' ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' ' ; } else { ongoing = ' ' ; count += 1 ; } } else { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' ' ; } else { ongoing = ' ' ; count += 1 ; } } } return count + 1 ; } let S = " " ; let n = S . length ; document . write ( minReqSubstring ( S , n ) ) ;