📌  相关文章
📜  检查a的每个组是否跟着长度相同的b的组

📅  最后修改于: 2021-06-25 21:25:05             🧑  作者: Mango

给定字符串str ,任务是检查每组连续的a后面是否跟着相同长度的连续的b组。如果每个组的条件都成立,则打印1,否则打印0
例子:

方法:

  • 对于字符串的每个a ,增加计数
  • 从第一个b开始,递减每个b的计数。
  • 如果在上述循环结束时,计数!= 0,然后返回false
  • 否则,对字符串的其余部分重复前两个步骤。
  • 如果所有循环都满足条件,则返回true ,否则打印0

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to match whether there are always n consecutive b's
// followed by n consecutive a's throughout the string
int matchPattern(string s)
{
    int count = 0;
    int n = s.length();
 
    // Traverse through the string
    int i = 0;
    while (i < n) {
 
        // Count a's in current segment
        while (i < n && s[i] == 'a') {
            count++;
            i++;
        }
 
        // Count b's in current segment
        while (i < n && s[i] == 'b') {
            count--;
            i++;
        }
 
        // If both counts are not same.
        if (count != 0)
            return false;
    }
 
    return true;
}
 
// Driver code
int main()
{
    string s = "bb";
    if (matchPattern(s) == true)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation of the above approach
 
public class GFG{
 
// Function to match whether there are always n consecutive b's
// followed by n consecutive a's throughout the string
static boolean matchPattern(String s)
{
    int count = 0;
    int n = s.length();
 
    // Traverse through the string
    int i = 0;
    while (i < n) {
 
        // Count a's in current segment
        while (i < n && s.charAt(i) == 'a') {
            count++;
            i++;
        }
 
        // Count b's in current segment
        while (i < n && s.charAt(i) == 'b') {
            count--;
            i++;
        }
 
        // If both counts are not same.
        if (count != 0)
            return false;
    }
 
    return true;
}
 
// Driver code
public static void main(String []args)
{
    String s = "bb";
    if (matchPattern(s) == true)
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// This code is contributed by Ryuga
}


Python3
# Python 3 implementation of the approach
 
# Function to match whether there are
# always n consecutive b's followed by
# n consecutive a's throughout the string
def matchPattern(s):
 
    count = 0;
    n = len(s);
 
    # Traverse through the string
    i = 0;
    while (i < n) :
 
        # Count a's in current segment
        while (i < n and s[i] == 'a'):
 
            count += 1 ;
            i =+ 1;
 
        # Count b's in current segment
        while (i < n and s[i] == 'b'):
            count -= 1 ;
            i += 1;
     
        # If both counts are not same.
        if (count != 0):
            return False;
 
    return True;
 
# Driver code
s = "bb";
if (matchPattern(s) == True):
    print("Yes");
else:
    print("No");
 
# This code is contributed
# by Akanksha Rai


C#
// C# implementation of the above approach
  
using System;
public class GFG{
  
// Function to match whether there are always n consecutive b's
// followed by n consecutive a's throughout the string
static bool matchPattern(string s)
{
    int count = 0;
    int n = s.Length;
  
    // Traverse through the string
    int i = 0;
    while (i < n) {
  
        // Count a's in current segment
        while (i < n && s[i] == 'a') {
            count++;
            i++;
        }
  
        // Count b's in current segment
        while (i < n && s[i] == 'b') {
            count--;
            i++;
        }
  
        // If both counts are not same.
        if (count != 0)
            return false;
    }
  
    return true;
}
  
// Driver code
public static void Main()
{
    string s = "bb";
    if (matchPattern(s) == true)
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
}


PHP


Javascript


输出:
No
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”