Question

In: Computer Science

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.

Solutions

Expert Solution

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.


Related Solutions

Public static boolean isPalindrome(String word) * Check to see if a word is a palindrome. Should...
Public static boolean isPalindrome(String word) * Check to see if a word is a palindrome. Should be case-independent. * @param word a String without whitespace * @return true if the word is a palindrome    Public static int longestWordLength(String words) * Returns length of the longest word in the given String using recursion (no loops). * Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space, * use the following method on your String to remove...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
public static void main(String[] args) {            //Part1: using three stacks           ...
public static void main(String[] args) {            //Part1: using three stacks            iQueue<Integer> main = new ourLinkedList<Integer>();                       for (int i = 0; i<10; i++) {                int num = -15 + (int) (Math.random() * ((15 - (-15)) + 1));            main.add(num);            }                       System.out.println("main: " +main);            iQueue<Integer> Q1 = new ourLinkedList<Integer>();           ...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it returns 0 if (words == null OR words.length == 0) // returns number of times letter is the first letter of a String in words array // use equalIgnoreCase to check if letter and str.charAt(0) are equal and ignoring case }
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[]...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] strings = new String[100];        for (int i = 0; i < strings.length; i++) {            System.out.println("Enter Strings: stop ");            strings[i]=input.nextLine();            if(strings[i].equalsIgnoreCase("stop"))                break;        }               MaxLength(strings);//here is the error    }    public static int MaxLength(String[] array) {        int max = array[0].length();       ...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT