Given a string str, the task is to check if that string is a Reverse Bitonic string or not. If the string str is reverse Bitonic string, then print “YES”. Otherwise, print “NO”.
A Reverse Bitonic String is a string in which the characters are arranged in decreasing order followed by increasing order of their ASCII values.
Examples:
Input: str = “zyxbcd”
Output: YES
Explanation:
In the above string, the ASCII values first decreases {z, y, x} and then increases {b, c, d}.
Input: str = “abcdwef”
Output: NO
Approach:
To solve the problem, 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 decreasing followed by strictly increasing.
Follow these steps below 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, break the loop.
- Now traverse from that index and for each character, 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 array is reached, then print “NO” and break the loop.
- If the entire string is successfully traversed, print “YES”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int checkReverseBitonic(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 = "abcdwef" ;
(checkReverseBitonic(s) == 1)
? cout << "YES"
: cout << "NO" ;
return 0;
}
|
Java
class GFG{
static int checkReverseBitonic(String s)
{
int i, j;
for (i = 1 ; i < s.length(); i++)
{
if (s.charAt(i) < s.charAt(i - 1 ))
continue ;
if (s.charAt(i) >= s.charAt(i - 1 ))
break ;
}
if (i == s.length() - 1 )
return 1 ;
for (j = i + 1 ; j < s.length(); j++)
{
if (s.charAt(j) > s.charAt(j - 1 ))
continue ;
if (s.charAt(j) <= s.charAt(j - 1 ))
break ;
}
i = j;
if (i != s.length())
return 0 ;
return 1 ;
}
public static void main(String []args)
{
String s = "abcdwef" ;
if (checkReverseBitonic(s) == 1 )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def checkReverseBitonic(s):
i = 0
j = 0
for i in range ( len (s)):
if (s[i] < s[i - 1 ]) :
continue ;
if (s[i] > = s[i - 1 ]) :
break ;
if (i = = len (s) - 1 ) :
return 1 ;
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)) :
return 0 ;
return 1 ;
s = "abcdwef"
if (checkReverseBitonic(s) = = 1 ) :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static int checkReverseBitonic(String 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 = "abcdwef" ;
if (checkReverseBitonic(s) == 1)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
function checkReverseBitonic(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 = "abcdwef" ;
(checkReverseBitonic(s) == 1) ?
document.write( "YES" ) :
document.write( "NO" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Mar, 2021
Like Article
Save Article