Count of Reverse Bitonic Substrings in a given String
Given a string S, the task is to count the number of Reverse Bitonic Substrings in the given string.
Reverse bitonic substring: A string in which the ASCII values of the characters of the string follow any of the following patterns:
- Strictly Increasing
- Strictly decreasing
- Decreasing and then increasing
Examples:
Input: S = “bade”
Output: 10
Explanation:
All possible substrings of length 1, {“b”, “a”, “d”, “e”} are always reverse bitonic.
Substrings of length 2 which are reverse bitonic are {“ba”, “ad”, “de”}.
Substrings of length 3 which are reverse bitonic are {“bad “, “ade”}.
Only substring of length 4 which is reverse bitonic is “bade”.
Therefore, the count of reverse bitonic substrings is 10.
Input: S = “abc”
Output: 6
Approach :
The approach is to generate all possible substrings of the given string and follow the steps below for each substring to solve the problem:
- Traverse the string and for each character, check if the ASCII value of the next character is smaller than the ASCII value of the current character or not.
- If at any point, the ASCII value of the next character is greater than the ASCII value of the current character, traverse from that index and for each character from now onwards, check if the ASCII value of the next character is greater than the ASCII value of the current character or not.
- If at any point, the ASCII value of the next character is smaller than the ASCII value of the current character before the end of the substring is reached, then ignore the substring.
- If the end of the substring is reached, increase count.
- After completing all the above steps for all substrings, print the final value of count.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int CountsubString( 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++;
}
if (k > j) {
c++;
f = 2;
}
while (temp < str[k] && k <= j
&& f != 2) {
temp = str[k];
k++;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
return c;
}
int main()
{
char str[] = "bade" ;
cout << CountsubString(str, strlen (str));
return 0;
}
|
Java
class GFG{
public static int CountsubString( 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++;
}
if (k > j)
{
c++;
f = 2 ;
}
while (k <= j && temp < str[k] &&
f != 2 )
{
temp = str[k];
k++;
}
if (k > j && f != 2 )
{
c++;
f = 0 ;
}
}
}
return c;
}
public static void main(String[] args)
{
char str[] = { 'b' , 'a' , 'd' , 'e' };
System.out.println(CountsubString(
str, str.length));
}
}
|
Python3
def CountsubString(strr, n):
c = 0
for len in range (n + 1 ):
for i in range (n - len ):
j = i + len - 1
temp = strr[i]
f = 0
if (j = = i):
c + = 1
continue
k = i + 1
while (k < = j and temp > strr[k]):
temp = strr[k]
k + = 1
if (k > j):
c + = 1
f = 2
while (k < = j and f ! = 2 and
temp < strr[k]):
temp = strr[k]
k + = 1
if (k > j and f ! = 2 ):
c + = 1
f = 0
return c
if __name__ = = '__main__' :
strr = "bade"
print (CountsubString(strr, len (strr)))
|
C#
using System;
class GFG{
public static int CountsubString( 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++;
}
if (k > j)
{
c++;
f = '2' ;
}
while (k <= j && temp < str[k] &&
f != '2' )
{
temp = str[k];
k++;
}
if (k > j && f != 2)
{
c++;
f = '0' ;
}
}
}
return c;
}
public static void Main(String[] args)
{
char []str = { 'b' , 'a' , 'd' , 'e' };
Console.WriteLine(CountsubString(
str, str.Length) - 1);
}
}
|
Javascript
<script>
function CountsubString(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++;
}
if (k > j) {
c++;
f = 2;
}
while (temp < str[k] && k <= j
&& f != 2) {
temp = str[k];
k++;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
return c;
}
var str = "bade" .split( '' );
document.write( CountsubString(str, str.length));
</script>
|
Output:
10
Time Complexity: O(N^3)
Auxiliary Space: O(1)
Last Updated :
07 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...