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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if the given // string is reverse bitonic int checkReverseBitonic(string s) { int i, j; // Check for decreasing sequence for (i = 1; i < s.size(); i++) { if (s[i] < s[i - 1]) continue ; if (s[i] >= s[i - 1]) break ; } // If end of string has // been reached if (i == s.size() - 1) return 1; // Check for increasing sequence 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 the end of string // hasn't been reached if (i != s.size()) return 0; // If the string is // reverse bitonic return 1; } // Driver Code int main() { string s = "abcdwef" ; (checkReverseBitonic(s) == 1) ? cout << "YES" : cout << "NO" ; return 0; } |
Java
// Java program to implement // the above approach class GFG{ // Function to check if the given // string is reverse bitonic static int checkReverseBitonic(String s) { int i, j; // Check for decreasing sequence 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 end of string has // been reached if (i == s.length() - 1 ) return 1 ; // Check for increasing sequence 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 the end of string // hasn't been reached if (i != s.length()) return 0 ; // If the string is // reverse bitonic return 1 ; } // Driver Code public static void main(String []args) { String s = "abcdwef" ; if (checkReverseBitonic(s) == 1 ) System.out.println( "YES" ); else System.out.println( "NO" ); } } // This code is contributed by grand_master |
Python3
# Python3 program to implement # the above approach # Function to check if the given # string is reverse bitonic def checkReverseBitonic(s): i = 0 j = 0 # Check for decreasing sequence for i in range ( len (s)): if (s[i] < s[i - 1 ]) : continue ; if (s[i] > = s[i - 1 ]) : break ; # If end of string has been reached if (i = = len (s) - 1 ) : return 1 ; # Check for increasing sequence 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 the end of string hasn't # been reached if (i ! = len (s)) : return 0 ; # If reverse bitonic return 1 ; # Given string s = "abcdwef" # Function Call if (checkReverseBitonic(s) = = 1 ) : print ( "YES" ) else : print ( "NO" ) |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to check if the given // string is reverse bitonic static int checkReverseBitonic(String s) { int i, j; // Check for decreasing sequence for (i = 1; i < s.Length; i++) { if (s[i] < s[i - 1]) continue ; if (s[i] >= s[i - 1]) break ; } // If end of string has // been reached if (i == s.Length - 1) return 1; // Check for increasing sequence 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 the end of string // hasn't been reached if (i != s.Length) return 0; // If the string is // reverse bitonic return 1; } // Driver Code public static void Main(String []args) { String s = "abcdwef" ; if (checkReverseBitonic(s) == 1) Console.WriteLine( "YES" ); else Console.WriteLine( "NO" ); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program to implement // the above approach // Function to check if the given // string is reverse bitonic function checkReverseBitonic(s) { var i, j; // Check for decreasing sequence for (i = 1; i < s.length; i++) { if (s[i] < s[i - 1]) continue ; if (s[i] >= s[i - 1]) break ; } // If end of string has // been reached if (i == s.length - 1) return 1; // Check for increasing sequence 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 the end of string // hasn't been reached if (i != s.length) return 0; // If the string is // reverse bitonic return 1; } // Driver Code var s = "abcdwef" ; (checkReverseBitonic(s) == 1) ? document.write( "YES" ) : document.write( "NO" ); // This code is contributed by rutvik_56 </script> |
NO
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!