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

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...
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...
The word "euthanasia" draws its roots from Greek meaning "good death." As it is used in...
The word "euthanasia" draws its roots from Greek meaning "good death." As it is used in this discussion, it means "the act of ending the life of a person suffering from either a terminal illness, or an incurable disease." The American Medical Association (AMA) is against physicians assisting in euthanasia. Only a small number of states allow for euthanasia. Euthanasia advocates stress that it should be allowed as an extension of a person's autonomy. Those who are against euthanasia often...
When a variable is declared, it may also be initialized (given its initial – meaning first...
When a variable is declared, it may also be initialized (given its initial – meaning first – value) using the assignment operator (an equals sign). What is the initial value of cupSize?
For many years, Thomson Company manufactured a single product called LEC 40. Then three years ago, the company automated a portion of its plant and at the same time introduced a second product called LEC 90 that has become increasingly popular.
For many years, Thomson Company manufactured a single product called LEC 40. Then three years ago, the company automated a portion of its plant and at the same time introduced a second product called LEC 90 that has become increasingly popular. The LEC 90 is a more complex product, requiring 0.60 hours of direct labor time per unit to manufacture and extensive machining in the automated portion of the plant. The LEC 40 requires only 0.20 hours of direct labor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT