Count of bitonic substrings from the given string
Last Updated :
31 Mar, 2023
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find all the bitonic
// sub strings
void subString(char str[], int n)
{
// Pick starting point
int c = 0;
// Iterate till length of the string
for (int len = 1; len <= n; len++) {
// Pick ending point for string
for (int i = 0; i <= n - len; i++) {
// Substring from i to j
// is obtained
int j = i + len - 1;
char temp = str[i], f = 0;
// Substrings of length 1
if (j == i) {
// Increase count
c++;
continue;
}
int k = i + 1;
// For increasing sequence
while (temp < str[k] && k <= j) {
temp = str[k];
k++;
f = 2;
}
// Check for strictly increasing
if (k > j) {
// Increase count
c++;
f = 2;
}
// Check for decreasing sequence
while (temp > str[k]
&& k <= j
&& f != 2) {
k++;
f = 0;
}
if (k > j && f != 2) {
// Increase count
c++;
f = 0;
}
}
}
// Print the result
cout << c << endl;
}
// Driver Code
int main()
{
// Given string
char str[] = "bade";
// Function Call
subString(str, strlen(str));
return 0;
}
Java
// Java+ program for the above approach
import java.util.*;
class GFG{
// Function to find all the bitonic
// sub Strings
static void subString(char str[], int n)
{
// Pick starting point
int c = 0;
// Iterate till length of the String
for(int len = 1; len <= n; len++)
{
// Pick ending point for String
for(int i = 0; i <= n - len; i++)
{
// SubString from i to j
// is obtained
int j = i + len - 1;
char temp = str[i], f = 0;
// SubStrings of length 1
if (j == i)
{
// Increase count
c++;
continue;
}
int k = i + 1;
// For increasing sequence
while (k < n && temp < str[k])
{
temp = str[k];
k++;
f = 2;
}
// Check for strictly increasing
if (k > j)
{
// Increase count
c++;
f = 2;
}
// Check for decreasing sequence
while (k < n && temp > str[k] &&
f != 2)
{
k++;
f = 0;
}
if (k > j && f != 2)
{
// Increase count
c++;
f = 0;
}
}
}
// Print the result
System.out.print(c + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given String
char str[] = "bade".toCharArray();
// Function call
subString(str, str.length);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find all the bitonic
# sub strings
def subString(str, n):
# Pick starting po
c = 0;
# Iterate till length of the string
for len in range(1, n + 1):
# Pick ending point for string
for i in range(0, n - len + 1):
# Substring from i to j
# is obtained
j = i + len - 1;
temp = str[i]
f = 0;
# Substrings of length 1
if (j == i):
# Increase count
c += 1
continue;
k = i + 1;
# For increasing sequence
while (k <= j and temp < str[k]):
temp = str[k];
k += 1;
f = 2;
# Check for strictly increasing
if (k > j):
# Increase count
c += 1;
f = 2;
# Check for decreasing sequence
while (k <= j and temp > str[k] and
f != 2):
k += 1;
f = 0;
if (k > j and f != 2):
# Increase count
c += 1;
f = 0;
# Print the result
print(c)
# Driver code
# Given string
str = "bade";
# Function Call
subString(str, len(str))
# This code is contributed by grand_master
C#
// C# program for the above approach
using System;
class GFG{
// Function to find all the bitonic
// sub Strings
static void subString(char []str, int n)
{
// Pick starting point
int c = 0;
// Iterate till length of the String
for(int len = 1; len <= n; len++)
{
// Pick ending point for String
for(int i = 0; i <= n - len; i++)
{
// SubString from i to j
// is obtained
int j = i + len - 1;
char temp = str[i], f = (char)0;
// SubStrings of length 1
if (j == i)
{
// Increase count
c++;
continue;
}
int k = i + 1;
// For increasing sequence
while (k < n && temp < str[k])
{
temp = str[k];
k++;
f = (char)2;
}
// Check for strictly increasing
if (k > j)
{
// Increase count
c++;
f = (char)2;
}
// Check for decreasing sequence
while (k < n && temp > str[k] &&
f != 2)
{
k++;
f = (char)0;
}
if (k > j && f != 2)
{
// Increase count
c++;
f = (char)0;
}
}
}
// Print the result
Console.Write(c + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given String
char []str = "bade".ToCharArray();
// Function call
subString(str, str.Length);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program for the above approach
// Function to find all the bitonic
// sub strings
function subString(str, n)
{
// Pick starting point
var c = 0;
// Iterate till length of the string
for (var len = 1; len <= n; len++) {
// Pick ending point for string
for (var i = 0; i <= n - len; i++) {
// Substring from i to j
// is obtained
var j = i + len - 1;
var temp = str[i], f = 0;
// Substrings of length 1
if (j == i) {
// Increase count
c++;
continue;
}
var k = i + 1;
// For increasing sequence
while (temp < str[k] && k <= j) {
temp = str[k];
k++;
f = 2;
}
// Check for strictly increasing
if (k > j) {
// Increase count
c++;
f = 2;
}
// Check for decreasing sequence
while (temp > str[k]
&& k <= j
&& f != 2) {
k++;
f = 0;
}
if (k > j && f != 2) {
// Increase count
c++;
f = 0;
}
}
}
// Print the result
document.write( c );
}
// Driver Code
// Given string
var str = "bade".split('');
// Function Call
subString(str, str.length);
// This code is contributed by itsok.
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
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 IncreasingStrictly decreasingDecreasing and then increasin
8 min read
Count of substrings containing only the given character Given a string S and a character C, the task is to count the number of substrings of S that contains only the character C.Examples: Input: S = "0110111", C = '1' Output: 9 Explanation: The substrings containing only '1' are: "1" â 5 times "11" â 3 times "111" â 1 time Hence, the count is 9. Input: S
6 min read
Count of substrings from given Ternary strings containing characters at least once Given string str of size N consisting of only 0, 1, and 2, the task is to find the number of substrings that consists of characters 0, 1, and 2 at least once. Examples: Input: str = "0122"Output: 2Explanation:There exists 2 substrings such that the substrings has characters 0, 1, 2 at least once is
6 min read
Number of alternating substrings from a given Binary String Given a binary string of size N, the task is to count the number of alternating substrings that are present in the string S. Examples: Input: S = "0010"Output: 7Explanation: All the substring of the string S are: {"0", "00", "001", "0010", "0", "01", "010", "1", "10", "0"}Strings that are alternatin
13 min read
Count of substrings that start and end with 1 in given Binary String Given a binary string s, the task is to count all substrings that start and end with the character '1'. A valid substring must have both its first and last characters as '1', and can include one or more number of characters in between.Examples:Input: s = "00100101"Output: 3Explanation: Valid substri
7 min read
Count of substrings of a given Binary string with all characters same Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same. Examples: Input: str = â011âOutput: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there
10 min read