NPDA for the language L ={wЄ{a,b}* | w contains equal no. of a's and b's}
Last Updated :
11 Jul, 2025
A solid understanding of pushdown automata fundamentals is essential for this question.
Problem
Design a non deterministic PDA for accepting the language L ={wЄ{a,b}* | w contains equal no. of a's and b's}, i.e.,
L = {ab, aabb, abba, aababb, bbabaa, baaababb, .......}
The number of a's and b's are same in all the strings.
Explanation
Here, we need not to maintain any order of a's and b's. Thus our state diagram will contain only an initial state and a final state. The count of a's and b's is maintained by stack. We will take 3 stack alphabets:
𝚪 = {a, b, z}
Where,
𝚪 = set of all the stack alphabet and z = stack start symbol
Approach Used in the Construction of PDA
If 'a' comes first then push it in stack and if again 'a' comes then also push it. Similarly, if 'b' comes first ('a' did not comes yet) then push it into the stack and if again 'b' comes then also push it. Now, if 'a' is present in the top of the stack and 'b' comes then pop the 'a' from the stack. And if 'b' present in the top of the stack and 'a' comes then pop the 'b' from the stack. So, at the end if the stack becomes empty then we can say that the string is accepted by the PDA.
Stack Transition Functions
\delta(q0, a, z) \vdash (q0, az)
\delta(q0, a, a) \vdash (q0, aa)
\delta(q0, b, z) \vdash (q0, bz)
\delta(q0, b, b) \vdash (q0, bb)
\delta(q0, a, b) \vdash (q0, \epsilon)
\delta(q0, b, a) \vdash (q0, \epsilon)
\delta(q0, \epsilon, z) \vdash (qf, z)
Where, q0 = Initial state , qf = Final state
ϵ = indicates pop operation

So, this is our required non deterministic PDA for accepting the strings which contain equal no. of a's and b's.
Example:
We will take one input string: “aabbba” PDA accept or not?.
Solution:
1.Scan string from left to right.
2.on input 'a' and STACK alphabet Z, push the ‘a’s into STACK as : (a,Z/aZ) and state will be q0.
3.second input 'a' and STACK alphabet 'a', push the ‘a’s into STACK as : (a,a/aa) and state will be q0.
4.Third input 'b' and STACK alphabet 'a', pop from STACK as : (b,a/∈) and state will be q0.
5.on input 'b' and STACK alphabet 'a', pop from STACK as : (b,a/∈) and state will be q0.
6.on input 'b'and STACK alphabet Z, push the ‘b’s into STACK as : (b,Z/bZ) and state will be q0.
7.on input 'a' and STACK alphabet 'b', pop from STACK as : (a,b/∈) and state will be q0.
8.on input ∈ and STACK alphabet Z, go to final state(qf) as : (∈, Z/Z).
So, at the end the stack becomes empty then we can say that the string is accepted by the PDA.\
Important Links
- Pushdown automata
- Pushdown automata acceptance by final state
Explore
Automata _ Introduction
Regular Expression and Finite Automata
CFG
PDA (Pushdown Automata)
Introduction of Pushdown Automata
5 min read
Pushdown Automata Acceptance by Final State
4 min read
Construct Pushdown Automata for given languages
4 min read
Construct Pushdown Automata for all length palindrome
6 min read
Detailed Study of PushDown Automata
3 min read
NPDA for accepting the language L = {anbm cn | m,n>=1}
2 min read
NPDA for accepting the language L = {an bn cm | m,n>=1}
2 min read
NPDA for accepting the language L = {anbn | n>=1}
2 min read
NPDA for accepting the language L = {amb2m| m>=1}
2 min read
NPDA for accepting the language L = {am bn cp dq | m+n=p+q ; m,n,p,q>=1}
2 min read
Construct Pushdown automata for L = {0n1m2m3n | m,n ⥠0}
3 min read
Construct Pushdown automata for L = {0n1m2n+m | m, n ⥠0}
2 min read
NPDA for accepting the language L = {ambncm+n | m,n ⥠1}
2 min read
NPDA for accepting the language L = {amb(m+n)cn| m,n ⥠1}
3 min read
NPDA for accepting the language L = {a2mb3m|m>=1}
2 min read
NPDA for accepting the language L = {amb2m+1 | m ⥠1}
2 min read
NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}
3 min read
Construct Pushdown automata for L = {a2mc4ndnbm | m,n ⥠0}
3 min read
NPDA for L = {0i1j2k | i==j or j==k ; i , j , k >= 1}
2 min read
NPDA for accepting the language L = {anb2n| n>=1} U {anbn| n>=1}
2 min read
NPDA for the language L ={wÐ{a,b}* | w contains equal no. of a's and b's}
3 min read
Turing Machine
Decidability
TOC Interview preparation
TOC Quiz and PYQ's in TOC