Programming & Design
Question:
How will you find if a string is a palindrome or not using one stack and one queue?
Preethikrish
2007-09-22 09:53:41 UTC
How will you find if a string is a palindrome or not using one stack and one queue?
Three answers:
BigRez
2007-09-22 11:39:13 UTC
The above is nice, but doesn't account for two letters in the middle that may be equal. For example,
abcdeedcba
would not be identified. The code needs to change to detect that if the length is even, the middle two letters must be the same. Add a condition as
i < len(s)-i prior to checking the string for inequality.
Michael G
2007-09-22 17:43:20 UTC
The best I could think of using VB6
Private Function isPalindrome(s As String) As Boolean
Dim i As Integer, bFail As Boolean
For i = 1 To Len(s) / 2
If Mid$(s, i, 1) <> Mid$(s, Len(s) - i + 1, 1) Then
bFail = True
Exit For
End If
Next i
isPalindrome = (Not bFail)
End Function
nachu
2007-09-22 17:19:09 UTC
have the string in both stack and queue
1.queue is first in first out
2.stack is last in first out
this is the logic that helps in to checl for palindrome
now the charcter taken from stack and queue must be equal until you reach teh end of both stack and queue.
EG:
stack[top]==queue[front]
do it until stack[top]==null and queue[front]==queue[rear]
got it??
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...