Question

In: Computer Science

An English word is called palindrome if its meaning may be interpreted the same way in...

An English word is called palindrome if its meaning may be interpreted the same way in either forward or reverse direction. Some examples of common palindromic words: civic, radar, level, rotor, noon, etc.

Design an algorithm that takes a word as input and decides if the input word is palindrome. In your algorithm, a stack should be used. Describe the pseudo-code of your algorithm. What is the time complexity of your algorithm in big-O?

PLEAS USE PSEUDO-CODE.

Solutions

Expert Solution

pseudocode -

1. read the word
2. len=length(word)
3. declare stack S
4. for i=0 to len
   S.push(word[i])
5. end loop with variable i
6. for i=0 to len
   ch=s.top()

s.pop()
   if ch equals word[i]
       continue
   else
       print("Not palindrome")
       exit()
7. end loop with variable i
8. print("Palindrome")

Time complexity -

We are traversing the string once calculating its length, then pushing elements to the stack and once popping out the elements from the stack.

And we are also traversing stack once.

So time complexity = (O(n+n+n+n))= O(4*n)= O(n), where n is the length of the string

And we are using stack too, so space complexity = O(n)

c++ code -

#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
   string word;
   cout<<"Enter word"<<endl;
   cin>>word;
   int l=word.size();
   stack<char> s;
   for (int i = 0; i < l; ++i)
   {
       s.push(word[i]);
   }
   for (int i = 0; i < l; ++i)
   {
       char ch=s.top();
       s.pop();
       if (ch==word[i])
       {
           continue;
       }
       else
       {
           cout<<"Not palindrome"<<endl;
           exit(0);
       }
   }
   cout<<"palindrome"<<endl;
   return 0;
}
output-

Enter word
civik
Not palindrome

output -

Enter word
civic
palindrome


Related Solutions

A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
A palindrome is a word or a phrase that is the same when read both forward...
A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. Ex: If the input is: bob the output is: bob is a palindrome Ex: If the input is: bobby the output is: bobby is not a palindrome Hint: Start by just handling...
1. A is called a palindrome if it reads the same from left and right. For...
1. A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A is called a palindrome if it reads the same from left and right. For instance,...
A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A palindrome is a word or a phrase that is the same when read both forward and backward.
6.7 LAB: PalindromeA palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.Ex: If the input is:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.This is my code:s =...
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
python question A word is a palindrome if it the same read forwards and backwards. We...
python question A word is a palindrome if it the same read forwards and backwards. We will call a word a fuzzy palindrome if it is the same read forwards and backwards, except for possible differences in case. For example, both 'tattarrattat' and 'TaTtArRAttat' are fuzzy palindromes. Define a function is_fuzzy_palindrome that returns True if and only if its argument is a fuzzy palindrome. This method may be useful: S.lower() -> str : Return a copy of the string S...
2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example,...
2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example, \aibohphobia" (the irrational fear of palindromes) is a word that reads the same forwards and backwards. Write a function called isPalindrome() that accepts a string as a parameter and returns True if the string is a palindrome, False otherwise. Using the function you created, write a program Python home work Python
JAVA. A palindrome is a word or a phrase that is the same when read both forward and backward.
java please. A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.Ex: If the input is:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.Hint: Start by just handling single-word...
Not all leaders lead in the same way, and even then a leader may switch leadership...
Not all leaders lead in the same way, and even then a leader may switch leadership styles based on the situation. Identify and describe three different leadership styles (this is NOT manager primary functions). In addition, for each leadership style give a specific example of a situation in which that style is appropriate, and explain why. FOR BUSINESS100
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT