fork download
  1. var input = 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth';
  2.  
  3. function start(text) {
  4. var palindromes = [], letter, i, answer;
  5.  
  6. var add = function(palindrome) {
  7. palindromes.push(palindrome);
  8. };
  9.  
  10. // Loop through each letter of the alphabet
  11. for(i = 97; i < 123; i += 1) {
  12. letter = String.fromCharCode(i);
  13. getSequences(text, getPositions(text, letter)).forEach(add);
  14. }
  15.  
  16. // Sort in descending order from length and pick the first one
  17. answer = palindromes.sort(function(a, b) {
  18. return (a.length > b.length ? -1 : 1);
  19. })[0];
  20.  
  21. console.log('The longest palindrome is: ' + answer);
  22. }
  23.  
  24. // Gets positions of specific character
  25. function getPositions(text, character) {
  26. var positions = [];
  27.  
  28. text.split('').forEach(function(c, pos) {
  29. if(c === character) {
  30. positions.push(pos);
  31. }
  32. });
  33.  
  34. return positions;
  35. }
  36.  
  37. // Gets strings from each different position pair for each letter
  38. function getSequences(text, positions) {
  39. var i, j, sequences = [], current;
  40.  
  41. for(i = 0; i < positions.length; i += 1) {
  42. for(j = 0; j < positions.length; j += 1) {
  43. current = text.substring(positions[i], positions[j] + 1);
  44. if(current.split('').reverse().join('') === current && current.length > 1) {
  45. sequences.push(current);
  46. }
  47. }
  48. }
  49.  
  50. return sequences;
  51. }
  52.  
  53. start(input);
Time limit exceeded #stdin #stdout 15s 72576KB
stdin
Standard input is empty
stdout
Standard output is empty