Check if a given string is a Reverse Bitonic String or not

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: 
 

  1. 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.
  2. If at any point, the ASCII value of the next character is greater than the ASCII value of the current character, break the loop.
  3. 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.
  4. 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.
  5. If the entire string is successfully traversed, print “YES”.

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

NO

 

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : grand_master, 29AjayKumar