"Coding is just like talking to a unknown girl. Once you talked to her with guts, the next time you'll never afraid",Really? Yes , try it Now !!!
Everything here is for education and learning purpose only , individual holds rights on outer posts/links etc.
Team Cracking The Code
For such problems we can use a DFA -Make new states of 'X' and 'Y' till the occurrence of 'Z' and store the same into a stack - After than pop an element from the stack and compare it with the next element - If at the end stack is empty, string is of format pZq, in case of mismatch it isn't
3 comments :
I think in your question:
"like if p=XXYX then z will be XYXX
for ex XXYXXXYXX is valid"
'z' should be replaced by 'q' and the Input should be:
XXYXZXYXX
Correct me, if I am wrong
For such problems we can use a DFA
-Make new states of 'X' and 'Y' till the occurrence of 'Z' and store the same into a stack
- After than pop an element from the stack and compare it with the next element
- If at the end stack is empty, string is of format pZq, in case of mismatch it isn't
int indexOfZ = str.indexOf("Z");
if(indexOfZ == -1)
return false
String revP = str.subString(0, indexOfZ).reverse();
String q = str.subString(indexOfZ+1)
if(revP.equals(q))
{
return true;
}
else
return false;
Post a Comment