Algorithm to reverse a string and check if palindrome. This would replace this if-else block with a simple line: (n. The string that reads the same backward as well as forward is called as palindrome string. However, having said that this is the most optimised solution for this part of your code, you can make the code slightly shorted and less bulky without a big if-else.Īnd to do that, the best solution IMO would be with a ternary expression. If you think about this, there isn't really any optimisation that you can do as this condition will always have to be evaluated and always have to carry out the necessary task ( printing). Learn to check if a given string is palindrome string with simple java programs using stack, queue or simple loops. The reason is that the if-statement: if (n.equals(s2))Īt the processor level would simply evaluate the statement: n.equals(s2) and then switch to the first println if true else go to the second println. This can't really be achieved any more efficiently (as in using a method or function). StringBuffer str2=new StringBuffer(str.reverse()) java - Longest palindrome in a string - Code Review Stack Exchange Longest palindrome in a string Asked 9 years, 3 months ago Modified 8 years, 5 months ago Viewed 25k times 17 Please be brutal, and judge my code as if it was written at a top 3 tech company, straight out of college. I've created this using if statements, but would like too know if it is possible too create using only methods and loops, and if so how? I have looked for duplicates, and there are similar posts that achieve what I have below, but everything I find uses if else statementsĪny help appreciated as always thanks. It simply gives a boolean true/false on if a string backwards is still spelt the same way. Ironically the teacher solution uses Character. I also tried tmp.equals(c), but it returned the same results.I'm trying to create a programme like this previous one I've made This is the programming assignment from the Java Software Solutions (PP3.11) that I assign my students. I assume it just checks if it contains the same elements, but I'm not sure. So, it seems to be properly reversing the List, but when it compares them, it assumes they are equal? I believe there is an issue with the tmp = c and how it checks whether they are equal. 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. Check palindrome using LinkedList as queue and stack. So, first, here is my isPalindrome method: public static boolean isPalindrome(Collection c) array: I have made programs that check for palindromes before, but none that check an array or list. I saw similar questions online, but not many that help me with this. In simplest words, a string is palindrome if it is equal to it’s reverse string. I am trying to reverse a LinkedList, and check if it is a palindrome (the same backwards and forwards). Learn to check if a given string is palindrome string with simple java programs using stack, queue or simple loops.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |