Question

In: Computer Science

Question 3. A string p = p1…pn is a palindrome if it spells the same string...

Question 3. A string p = p1pn is a palindrome if it spells the same string when read backward, that is pi = pn+1 –i for 1≤ in. Design an efficient algorithm for finding all palindromes (of all lengths) in a text. (The pseudo code the algorithm should be provided) (40 points)

Solutions

Expert Solution

Algorithm:

Step 1: Input S (string)

Step 2:  Length = 0 , Flag =0

Step 3: While (S[Length] != NULL)

Length++

Step 4:  I = 0 , J = Length-1

Step 5: While ( I < (Length/2)+1 )

                        If ( S[I] == S[J] )

                                 Flag=0

                        else

                                  Flag=1

                        I++ , J–

Step 6:      If ( Flag == 0 )

                               Print string Is a Palindrome

                     else

                                Print string Is Not a Palindrome

Step 7: End

Pseudo code:

  • Input a String
  • Initialize Length to zero , Flag to zero
  • While String[Length] is not equal to NULL
  •    Increment Length
  • Initialize I to zero , J to Length-1
  • While I is less than (Length/2)+1
  •               If String[I] equal to String[J]
  •                           Flag=0
  •                 else
  •                            Flag=1
  •                 Increment I , Decrement J
  • If Flag equal to zero
  •         Print string Is a Palindrome
  • else
  •         Print string Is Not a Palindrome
  • Stop

Related Solutions

PLEASE DO IN JAVA 4.15 Palindrome A palindrome is a string that is the same spelled...
PLEASE DO IN JAVA 4.15 Palindrome A palindrome is a string that is the same spelled forward as it is spelled backward. So, "abcba" is a palindrome, as are "eye" and "madam". This program takes as input from the user, a string and outputs whether the string is a palindrome. (1) Modify the given program to use a loop to output the string one character at a time. (1 pt) Example output for word = radar: Word Entered: radar r...
C++: A palindrome is a string that is the same backward as it is forward. For...
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q...
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...
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
A palindrome is a string that reads the same forward and backward, i.e., the letters are...
A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right.      Examples: radar à is a palindrome Able was I ere I saw Elba à is a palindrome good à not a palindrome Write a java program to read a line of text and tell if the line is a palindrome. Use a stack to read each non-blank character...
Let P = (p1,...,pn) be a permutation of [n]. We say a number i is a...
Let P = (p1,...,pn) be a permutation of [n]. We say a number i is a fixed point of p, if pi = i. (a) Determine the number of permutations of [6] with at most three fixed points. (b) Determine the number of 9-derangements of [9] so that each even number is in an even position. (c) Use the following relationship (not proven here, but relatively easy to see) for the Rencontre numbers: Dn =(n-1)-(Dn-1 +Dn-2) (∗) to perform an...
Foundation of Computer Science A palindrome is a string that reads the same forward and backward....
Foundation of Computer Science A palindrome is a string that reads the same forward and backward. 1. Describe an algorithm that determines whether a string of n characters is a palindrome. 2. Write its corresponding program using your favorite programming language.
A palindrome is a string that reads the same forward and backward, for example, radar, toot,...
A palindrome is a string that reads the same forward and backward, for example, radar, toot, and madam. Your task is to construct a python algorithm to receive as input a string of characters and check whether it is a palindrome using a stack and a queue. Your ADTs contains the following methods: Queue Queue() – constructor enqueue(e) – enqueue an element dequeue(e) – dequeue an element len() – returns the number of elements in the queue Stack Stack() -...
4.A palindrome is a nonempty string over some alphabet that reads the same forward and backward....
4.A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic, racecar, abba,… Give a DP algorithm to find the longest palindrome that is a subsequence of a given input string. What is the running time of your algorithm? (40 points). •Examples: •Given the input “character”, your algorithm should return “carac”. •Given the input “TTATGTGAT”, your algorithm should return “TATGTAT” Python Code only
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT