Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Pattern Searching
  • Tutorial on Pattern Searching
  • Naive Pattern Searching
  • Rabin Karp
  • KMP Algorithm
  • Z Algorithm
  • Trie for Pattern Seaching
  • Manacher Algorithm
  • Suffix Tree
  • Ukkonen's Suffix Tree Construction
  • Boyer Moore
  • Aho-Corasick Algorithm
  • Wildcard Pattern Matching
Open In App
Next Article:
Check if left and right shift of any string results into given string
Next article icon

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

Last Updated : 26 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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




// 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>
 
 
Output: 
NO

 

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



Next Article
Check if left and right shift of any string results into given string

G

grand_master
Improve
Article Tags :
  • Competitive Programming
  • DSA
  • Pattern Searching
  • Strings
  • bitonic
  • Reverse
Practice Tags :
  • Pattern Searching
  • Reverse
  • Strings

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
  • Check if a Matrix is Reverse Bitonic or Not
    Given a matrix m[][], the task is to check if the given matrix is Reverse Bitonic or not. If the given matrix is Reverse Bitonic, then print Yes. Otherwise, print No. If all the rows and the columns of the given matrix have elements in one of the following orders: Strictly increasingStrictly decreas
    9 min read
  • Check if left and right shift of any string results into given string
    Given a non-empty string S consisting of only lowercase English letters. The task is to find if there exists any string which has left shift and right shift both equal to string S. If there exists any string then print Yes, otherwise print No. Examples: Input: S = "abcd" Output: No Explanation: Ther
    7 min read
  • Check if a given string is sum-string
    Given a string of digits, determine whether it is a ‘sum-string’. A string S is called a sum-string if the rightmost substring can be written as the sum of two substrings before it and the same is recursively true for substrings before it. Examples: “12243660” is a sum string. Explanation : 24 + 36
    12 min read
  • Check if binary representations of 0 to N are present as substrings in given binary string
    Give binary string str and an integer N, the task is to check if the substrings of the string contain all binary representations of non-negative integers less than or equal to the given integer N. Examples: Input: str = “0110", N = 3 Output: True Explanation: Since substrings “0", “1", “10", and “11
    9 min read
  • Check if a given string is a rotation of a palindrome
    Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p
    15+ min read
  • Check if a given string is Even-Odd Palindrome or not
    Given a string str, the task is to check if the given string is Even-Odd Palindrome or not. An Even-Odd Palindrome string is defined to be a string whose characters at even indices form a Palindrome while the characters at odd indices also form a Palindrome separately. Examples: Input: str="abzzab"
    7 min read
  • Program to check if an array is bitonic or not
    Given an array of N elements. The task is to check if the array is bitonic or not.An array is said to be bitonic if the elements in the array are first strictly increasing and then strictly decreasing. Examples: Input: arr[] = {-3, 9, 11, 20, 17, 5, 1}Output: YES Input: arr[] = {5, 6, 7, 8, 9, 10, 1
    6 min read
  • Check if decimal representation of Binary String is divisible by 9 or not
    Given a binary string S of length N, the task is to check if the decimal representation of the binary string is divisible by 9 or not. Examples: Input: S = 1010001Output:YesExplanation: The decimal representation of the binary string S is 81, which is divisible by 9. Therefore, the required output i
    12 min read
  • Check if the given string is linear or not
    Given string str, the task is to check whether the given string is linear or not. If it is linear then print "Yes" else print "No".   Let the string be "abcdefghij". It can be broken as: "a" "bc" "def" "ghij" if the character a, b, c, and are equal then the given string is linear otherwise not. Ther
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences