Bitonic string
Last Updated :
20 May, 2021
Given a string str, the task is to check if that string is a Bitonic String or not. If string str is Bitonic String then print “YES” else print “NO”.
A Bitonic String is a string in which the characters are arranged in increasing order followed by decreasing order of their ASCII values.
Examples:
Input: str = “abcdfgcba”
Output: YES
Explanation:
In the above string, the ASCII values first increases {a, b, c, d, f, g} and then decreases {g, c, b, a}.
Input: str = “abcdwef”
Output: NO
Approach:
To solve the problem, we simply need to traverse the string and check if the ASCII values of the characters of the string follow any of the following patterns:
- Strictly increasing.
- Strictly decreasing.
- Strictly increasing followed by strictly decreasing.
Follow these steps below to solve the problem:
- Start traversing the string and keep checking 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 not greater than the ASCII value of the current character, break the loop.
- Again start traversing from the above break index and 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 not smaller than the ASCII value of the current character before the end of the array is reached, then print “NO” and break the loop.
- If the end of the array is reached successfully, print “YES”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int checkBitonic(string s)
{
int i, j;
for (i = 1; i < s.size(); i++) {
if (s[i] > s[i - 1])
continue ;
if (s[i] <= s[i - 1])
break ;
}
if (i == s.size() - 1)
return 1;
for (j = i + 1; j < s.size();
j++) {
if (s[j] < s[j - 1])
continue ;
if (s[j] >= s[j - 1])
break ;
}
i = j;
if (i != s.size())
return 0;
return 1;
}
int main()
{
string s = "abcdfgcba" ;
(checkBitonic(s) == 1)
? cout << "YES"
: cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int checkBitonic( char []s)
{
int i, j;
for (i = 1 ; i < s.length; i++)
{
if (s[i] > s[i - 1 ])
continue ;
if (s[i] <= s[i - 1 ])
break ;
}
if (i == s.length - 1 )
return 1 ;
for (j = i + 1 ; j < s.length; j++)
{
if (s[j] < s[j - 1 ])
continue ;
if (s[j] >= s[j - 1 ])
break ;
}
i = j;
if (i != s.length)
return 0 ;
return 1 ;
}
public static void main(String[] args)
{
String s = "abcdfgcba" ;
System.out.print((checkBitonic(
s.toCharArray()) == 1 ) ? "YES" : "NO" );
}
}
|
Python3
def checkBitonic(s):
i = 0
j = 0
for i in range ( 1 , len (s)):
if (s[i] > s[i - 1 ]):
continue ;
if (s[i] < = s[i - 1 ]):
break ;
if (i = = ( len (s) - 1 )):
return True ;
for j in range (i + 1 , len (s)):
if (s[j] < s[j - 1 ]):
continue ;
if (s[j] > = s[j - 1 ]):
break ;
i = j;
if (i ! = len (s) - 1 ):
return False ;
return True ;
s = "abcdfgcba"
if (checkBitonic(s)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static int checkBitonic( char []s)
{
int i, j;
for (i = 1; i < s.Length; i++)
{
if (s[i] > s[i - 1])
continue ;
if (s[i] <= s[i - 1])
break ;
}
if (i == s.Length - 1)
return 1;
for (j = i + 1; j < s.Length; j++)
{
if (s[j] < s[j - 1])
continue ;
if (s[j] >= s[j - 1])
break ;
}
i = j;
if (i != s.Length)
return 0;
return 1;
}
public static void Main(String[] args)
{
String s = "abcdfgcba" ;
Console.Write((checkBitonic(
s.ToCharArray()) == 1) ? "YES" : "NO" );
}
}
|
Javascript
<script>
function checkBitonic(s)
{
var i, j;
for (i = 1; i < s.length; i++) {
if (s[i] > s[i - 1])
continue ;
if (s[i] <= s[i - 1])
break ;
}
if (i == s.length - 1)
return 1;
for (j = i + 1; j < s.length;
j++) {
if (s[j] < s[j - 1])
continue ;
if (s[j] >= s[j - 1])
break ;
}
i = j;
if (i != s.length)
return 0;
return 1;
}
var s = "abcdfgcba" ;
(checkBitonic(s) == 1)
? document.write( "YES" )
: document.write( "NO" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...