Given a string str, the task is to count all the bitonic substrings of the given string.
A bitonic substring is a substring of the given string in which elements are either strictly increasing or strictly decreasing, or first increasing and then decreasing.
Examples:
Input: str = “bade”
Output: 8
Explanation:
Substrings of length 1 are always bitonic, “b”, “a”, “d”, “e”
Substrings of length 2 are “ba”, “ad”, “de” and these all are also bitonic because they are increasing or decreasing only.
Substrings of length 3 are “bad “and “ade” in which “ade” is bitonic.
Substring of length 4 “bade” is not bitonic because it decreases and then increases.
So total 8 substrings are bitonic.
Input: str = “abc”
Output: 6
Explanation:
The given string is increasing, so all it’s substrings are also increasing and hence are bitonic. So total = 6.
Approach: The idea is to generate all possible substring of the given string and check if each substring is Bitonic or not. If the substring is Bitonic then increment the count for the answer. Finally, print the count of all the bitonic subarrays.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void subString( char str[], int n)
{
int c = 0;
for ( int len = 1; len <= n; len++) {
for ( int i = 0; i <= n - len; i++) {
int j = i + len - 1;
char temp = str[i], f = 0;
if (j == i) {
c++;
continue ;
}
int k = i + 1;
while (temp < str[k] && k <= j) {
temp = str[k];
k++;
f = 2;
}
if (k > j) {
c++;
f = 2;
}
while (temp > str[k]
&& k <= j
&& f != 2) {
k++;
f = 0;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
cout << c << endl;
}
int main()
{
char str[] = "bade" ;
subString(str, strlen (str));
return 0;
}
|
Java
import java.util.*;
class GFG{
static void subString( char str[], int n)
{
int c = 0 ;
for ( int len = 1 ; len <= n; len++)
{
for ( int i = 0 ; i <= n - len; i++)
{
int j = i + len - 1 ;
char temp = str[i], f = 0 ;
if (j == i)
{
c++;
continue ;
}
int k = i + 1 ;
while (k < n && temp < str[k])
{
temp = str[k];
k++;
f = 2 ;
}
if (k > j)
{
c++;
f = 2 ;
}
while (k < n && temp > str[k] &&
f != 2 )
{
k++;
f = 0 ;
}
if (k > j && f != 2 )
{
c++;
f = 0 ;
}
}
}
System.out.print(c + "\n" );
}
public static void main(String[] args)
{
char str[] = "bade" .toCharArray();
subString(str, str.length);
}
}
|
Python3
def subString( str , n):
c = 0 ;
for len in range ( 1 , n + 1 ):
for i in range ( 0 , n - len + 1 ):
j = i + len - 1 ;
temp = str [i]
f = 0 ;
if (j = = i):
c + = 1
continue ;
k = i + 1 ;
while (k < = j and temp < str [k]):
temp = str [k];
k + = 1 ;
f = 2 ;
if (k > j):
c + = 1 ;
f = 2 ;
while (k < = j and temp > str [k] and
f ! = 2 ):
k + = 1 ;
f = 0 ;
if (k > j and f ! = 2 ):
c + = 1 ;
f = 0 ;
print (c)
str = "bade" ;
subString( str , len ( str ))
|
C#
using System;
class GFG{
static void subString( char []str, int n)
{
int c = 0;
for ( int len = 1; len <= n; len++)
{
for ( int i = 0; i <= n - len; i++)
{
int j = i + len - 1;
char temp = str[i], f = ( char )0;
if (j == i)
{
c++;
continue ;
}
int k = i + 1;
while (k < n && temp < str[k])
{
temp = str[k];
k++;
f = ( char )2;
}
if (k > j)
{
c++;
f = ( char )2;
}
while (k < n && temp > str[k] &&
f != 2)
{
k++;
f = ( char )0;
}
if (k > j && f != 2)
{
c++;
f = ( char )0;
}
}
}
Console.Write(c + "\n" );
}
public static void Main(String[] args)
{
char []str = "bade" .ToCharArray();
subString(str, str.Length);
}
}
|
Javascript
<script>
function subString(str, n)
{
var c = 0;
for ( var len = 1; len <= n; len++) {
for ( var i = 0; i <= n - len; i++) {
var j = i + len - 1;
var temp = str[i], f = 0;
if (j == i) {
c++;
continue ;
}
var k = i + 1;
while (temp < str[k] && k <= j) {
temp = str[k];
k++;
f = 2;
}
if (k > j) {
c++;
f = 2;
}
while (temp > str[k]
&& k <= j
&& f != 2) {
k++;
f = 0;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
document.write( c );
}
var str = "bade" .split( '' );
subString(str, str.length);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)