📌  相关文章
📜  检查给定的字符串是否是反向双音字符串

📅  最后修改于: 2021-06-26 02:03:06             🧑  作者: Mango

给定字符串str ,任务是检查该字符串是否为反向双音字符串。如果字符串str是反向Bitonic字符串,则打印“ YES” 。否则,打印“否”

例子:

方法:
为了解决这个问题,遍历字符串并检查字符串的字符的ASCII值以下任何模式:

  • 严格增加。
  • 严格减少。
  • 严格减少,然后严格增加。

请按照以下步骤解决问题:

  1. 遍历字符串,对于每个字符,检查下一个字符的ASCII值是否小于当前字符的ASCII值。
  2. 如果在任何时候下一个字符的ASCII值大于当前字符的ASCII值,请中断循环。
  3. 现在从该索引开始遍历,对于每个字符,检查下一个字符的ASCII值是否大于当前字符的ASCII值。
  4. 如果在到达数组末尾之前的任何时候,下一个字符的ASCII值小于当前字符的ASCII值,则打印“ NO”并中断循环。
  5. 如果成功遍历了整个字符串,请打印“ YES”

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
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


输出:
NO

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。