- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If every group of a is followed by a group of b of same length in Python
Suppose we have a lowercase string s with only two characters a and b. We have to check whether every group of consecutive a's is followed by group of consecutive b's of equal length.
So, if the input is like s = "abaaabbbaabbaabbab", then the output will be True, as all groups are (ab), (aaabbb), (aabb), (aabb), (ab).
To solve this, we will follow these steps −
- a_count := 0, string_len := size of s
- i := 0
- while i < string_len, do
- while i < string_len and s[i] is 'a', do
- a_count := a_count + 1
- i := i + 1
- while i < string_len and s[i] is 'b', do
- a_count := a_count - 1
- i := i + 1
- if a_count is not 0, then
- return False
- while i < string_len and s[i] is 'a', do
- return True
Example
Let us see the following implementation to get better understanding −
def solve(s): a_count = 0 string_len = len(s) i = 0 while i < string_len: while i < string_len and s[i] == 'a': a_count += 1 i += 1 while i < string_len and s[i] == 'b': a_count -= 1 i += 1 if a_count != 0: return False return True s = "abaaabbbaabbaabbab" print(solve(s))
Input
"abaaabbbaabbaabbab"
Output
True
- Related Articles
- How to determine if a value appears in a GROUP BY group in MySQL?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- (a) Match the formulae in group A with appropriate names from group B :Group A: CH3COOH, CH3CHO, CH3OH Group B: Ethanol, Methanol, Ethanal, Ethanoic acid (b) Draw the structure of butanoic acid. (c) What is the IUPAC name of acetic acid?
- Python Group elements at same indices in a multi-list
- Python Program to Group Strings by K length Using Suffix
- An element X forms an oxide X2O3. In which group of Mendeleev's periodic table is this element placed?(a) group II (b) group III (c) group V (d) group VIII
- Get average of every group of n elements in an array JavaScript
- Getting a list of values by using MongoDB $group?
- On the basis of the electronic configuration of 95X, the group number and period of the element ‘X’ is :(a) Group 15 period 2 (b) Group 13 period 2(c) Group 9 period 5 (d) Group 13 period 5
- Check if it is possible to reach a number by making jumps of two given length in Python
- (a) What is meant by (i) a group, and (ii) a period, in a periodic table?(b) How many periods and groups are there in the long form of periodic table? (c) Give two examples each of (i) group 1 elements (ii) group 17 elements (iii) group 18 elements.
- Python - Compute last of group values in a Pandas DataFrame
- Python - Compute first of group values in a Pandas DataFrame
- MySQL “order by” inside of “group by”? Is it possible?
- Python Group by matching second tuple value in list of tuples

Advertisements