Question

Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...

Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any.
(b) What is the big-O complexity of your method in terms of the list size n.

Homework Answers

Answer #1

Java Code:

import java.util.*;

class Main {

public static boolean isPalindrome(String input){

Stack st=new Stack();

for(int i=0;i<input.length();i++) {

st.push(input.charAt(i));

}

String rev="";

while (!st.isEmpty()) {

rev=rev+st.pop();

}

if(input.equals(rev))

return true;

else

return false;

}

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

String input=new String();

System.out.print("Enter a string: ");

input=sc.nextLine();

if(isPalindrome(input))

System.out.println("String is a palindrome");

else

System.out.println("String is not a palindrome");

}

}

b)The time complexity of the function is O(n) where n is the input string size, because there are two loops in function for loop runs n times and while loop also run n times, so total time is 2n so time commplexity = O(2n) =O(n).

if you like the answer please provide a thumbs up.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a method in class SLL<T> called public static SLL<T> reverse() that produces a new linked...
Write a method in class SLL<T> called public static SLL<T> reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. (b) What is the big-O complexity of your method in terms of the list size n.
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(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 queue,...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. What is the big-O complexity of your method in terms of the list size n Supplementary Exercise for Programming (Coding) [Singly Linked Lists] Download and unpack (unzip) the file SinglyLinkedList.rar. Compile and execute the class...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT