Thus, your method should return the first substring with the greatest length. For every visited node, pop a node from the stack and compare data of popped node with the currently visited node. Traverse the given list from head to tail and push every visited node to stack. Length greater than three, but there are two palindromic substrings with length three, namely, "aca" and "ada". Follow the steps below to solve the problem: A simple solution is to use a stack of list nodes. Not guaranteed to be unique for example, in the string "abracadabra", there is no palindromic substring with About Products For Teams Stack Overflow Public questions & answers Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers. Write an Java method longestPalindrome that given a string s, it returns the longest palindromic substring.įor example, the longest palindromic substring of "bananas" is "anana". I am using recursion to find out whether a string is a palindrome, while ignoring spaces and non letter characters. Specifically, most of the inefficiency arises from the single line: char chars string.replaceAll (' ', '').toLowerCase ().toCharArray () The replaceAll () method creates another copy of the string. "The longest palindromic substring problem is the problem of finding a maximum-length contiguous substring of a 5 Answers Sorted by: 9 The implementation given by janos is a good one, but it is possible to improve it further. Exception in thread 'main' : Uncompilable source code - missing return statement at (Palindrome.java:31) at (Palindrome.java:27) Java Result: 1 So, basically it says Im missing a return statement I do not see how that is possible. InputStreamReader input = new InputStreamReader (System.in) īufferedReader reader = new BufferedReader (input) įor (int i = userinput.I've been solving some questions to prepare for my midterm and I can't get my hands on what is causing the error in this one.The problem states that How can I optimize this solution? import java.io.InputStreamReader int i 0 int j myString.length () - 1 boolean isPalindrome. Push all the elements till mid into the stack i.e. Examples: Input: str geeksforgeeks Output: No Input: str madam Output: Yes Approach: Find the length of the string say len. Are you assigning a value to a function (pop ()) You have to build reverse from popping from the stack. For example, the longest palindromic substring of 'bananas' is 'anana'. PDrome.pop ()reverse.charAt (i) is wrong. First, we turn the string to an array of. Practice Given string str, the task is to find whether the given string is a palindrome or not using a stack. Write an Java method longestPalindrome that given a string s, it returns the longest palindromic substring. If it is, then string is a palindrome, otherwise its not. The example uses the Java Stack container to build a reversed string. After the loop ends, check if the value of the boolean variable is still true. A string is called a palindrome string if the reverse of that string is the same as the original string. I was wondering if you can give me advice on this solution to assess if an user input string is a palindrome or not. Inside the loop, in any iteration, if the if condition evaluates to true, set that boolean variables value to false and break out of the loop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |