We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Not optimized version// Time Complexity: O(n)// Space Complexity: O(n) due to creating a substringprivatestaticbooleanisPalindrome(Stringinput){intinputLength=input.length();StringsecondHalfOfInput=input.substring(inputLength/2);intsecondHalfOfInputLength=secondHalfOfInput.length();for(inti=0;i<inputLength/2;i++){if(input.charAt(i)!=secondHalfOfInput.charAt(secondHalfOfInputLength-(i+1))){returnfalse;}}returntrue;}// optimized version// Time Complexity: O(n)// Space Complexity: O(1) No substring privatestaticbooleanisPalindromeOptimised(Stringinput){intleft=0;intright=input.length()-1;while(left<right){if(input.charAt(left)!=input.charAt(right)){returnfalse;}left++;right--;}returntrue;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java String Reverse
You are viewing a single comment's thread. Return to all comments →
Java 8 Based