Thursday, July 4, 2024

Bitonic string

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: 
 

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. If the end of the array is reached successfully, print “YES”.

Below is the implementation of the above approach:
 

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the given
// string is bitonic
int checkBitonic(string s)
{
    int i, j;
 
    // Check for increasing 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 decreasing 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;
 
    // Return true if bitonic
    return 1;
}
 
// Driver Code
int main()
{
    // Given string
    string s = "abcdfgcba";
 
    // Function Call
    (checkBitonic(s) == 1)
        ? cout << "YES"
        : cout << "NO";
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if the given
// String is bitonic
static int checkBitonic(char []s)
{
    int i, j;
 
    // Check for increasing 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 decreasing 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;
 
    // Return true if bitonic
    return 1;
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given String
    String s = "abcdfgcba";
 
    // Function Call
    System.out.print((checkBitonic(
        s.toCharArray()) == 1) ? "YES" : "NO");
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# Python3 program for the above approach
 
# Function to check if the given
# string is bitonic
def checkBitonic(s):
     
    i = 0
    j = 0
 
    # Check for increasing sequence
    for i in range(1, 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 True;
     
    # Check for decreasing 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) - 1):
        return False;
 
    # Return true if bitonic
    return True;
 
# Driver code
 
# Given string
s = "abcdfgcba"
 
# Function Call
if(checkBitonic(s)):
    print("YES")
else:
    print("NO")
     
# This code is contributed by grand_master


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the given
// String is bitonic
static int checkBitonic(char []s)
{
    int i, j;
 
    // Check for increasing 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 decreasing 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;
 
    // Return true if bitonic
    return 1;
}
 
// Driver Code
public static void Main(String[] args)
{
 
    // Given String
    String s = "abcdfgcba";
 
    // Function call
    Console.Write((checkBitonic(
        s.ToCharArray()) == 1) ? "YES" : "NO");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to check if the given
// string is bitonic
function checkBitonic(s)
{
    var i, j;
 
    // Check for increasing 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 decreasing 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;
 
    // Return true if bitonic
    return 1;
}
 
// Driver Code
 
// Given string
var s = "abcdfgcba";
 
// Function Call
(checkBitonic(s) == 1)
    ? document.write( "YES")
    : document.write( "NO");
 
// This code is contributed by itsok.
</script>


Output: 

YES

 

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 neveropen!

Last Updated :
20 May, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments