functionlengthOfLongestSubstring(str) { if (str.length == 1) { return str; } let chache = ''; let maxstr = ""; let startindex = 0; let endindex = 1; let lt=0; let rt=0; while (startindex != str.length - 1) { if (endindex == str.length) { chache = ""; startindex++; endindex = startindex + 1; } let cur = str[endindex]; if (str[startindex] == cur && startindex != endindex) { let sub=endindex - startindex; if (sub > 2) { lt=startindex; rt=endindex; let judge=true; while(lt!=rt&&rt>lt){ let ltval=str[lt]; let rtval=str[rt]; if(ltval!=rtval){ judge=false; break; } lt++; rt--; }
if (judge==true) { chache=str.substring(startindex,endindex+1); if (chache.length > maxstr.length) { maxstr = chache; } }
var longestPalindrome = function (s) { var longest = '', c; for (c = 0; c < s.length; c++) { if ((s.length - c) * 2 <= longest.length) break; // exit early if remaining can't surpass largest found