Computer Science, asked by mdlitonhossain2000, 4 months ago

write a program to recognize strings under 'ab' '(ab)* '.take input multi time.
do it c/c++/java.

//sample inpute
ab
ababababababab
e(empty)
abaaab
bbaa
//sample output
recognize by 'ab',(ab)*
recognize by '(ab)*'
recognize by '(ab)*'
recognize by '(ab)*'
not recognized
not recognized
help me hurry

Answers

Answered by TaheniyatAnjum
1

Explanation:

Count of operations to make a binary string”ab” free

Given a string containing characters ‘a’ and ‘b’ only. Convert the given string into a string in which there is no ‘ab’ substring. To make string ‘ab’ free we can perform an operation in which we select a ‘ab’ substring and replace it by ‘bba’.

Find the total number of operations required to convert the given string.

Examples:

Input : s = 'abbaa'

Output : 2

Explanation:

Here, ['ab'baa] is replaced s = [bbabaa]

[bb'ab'aa] is replaced s = [bbbbaaa]

which is ab free. Hence, 2 operations required.

Input : s = 'aab'

Output : 3

Explanation:

Here, [a'ab'] is replaced s = [abba]

['ab'ba] is replaced s = [bbaba]

[bb'ab'a] is replaced s = [bbbbaa]

which is ab free. Hence, 3 operations required

Recommended : Please try your approach on { IDE } first, before moving on to the solution.

Approach:

The final state will be some character ‘a’ after ‘b’: “bbb…baaa…a”

It’s obvious to prove all ‘b’s are distinctive to each other(i.e. Each ‘b’ in the initial state, will add some number of ‘b’s to the final state disjoint from other ‘b’s). For a character ‘b’ from the initial state it will double after seeing a character ‘a’. For each i-th character ‘b’, consider ti the number of a before it. So the final number of ‘b’s can be defined as sumation of 2^ti.

Similar questions