从字符串获取回文长度

时间:2019-01-29 19:26:24

标签: javascript jquery

我有一个字符串“ testaabbaccc”,该字符串包含回文字母“ abba”,长度为4,但是如何用JavaScript代码识别呢?

 var string ="testaabbaccc"

需要输出,因为阿巴是回文,长度是4

1 个答案:

答案 0 :(得分:2)

您可以使用此article并根据需要对其进行修改。

工作演示

Select a.*, b.schedule
from Tablea a 
    left join (select * from Tableb where date > NOW() and active = 1) b 
        on a.subid = b.subid
WHERE 
function isPalindrome(s) {
  var rev = s.split("").reverse().join("");
  return s == rev;
}
function longestPalind(s) {
  var maxp_length = 0,
    maxp = '';
  for (var i = 0; i < s.length; i++) {
    var subs = s.substr(i, s.length);
    for (var j = subs.length; j >= 0; j--) {
      var sub_subs = subs.substr(0, j);
      if (sub_subs.length <= 1)
        continue;
      if (isPalindrome(sub_subs)) {
        if (sub_subs.length > maxp_length) {
          maxp_length = sub_subs.length;
          maxp = sub_subs;
        }
      }
    }
  }
  return maxp;
}

console.log(longestPalind("testaabbaccc"));
console.log(longestPalind("testaabbaccc").length);