【问题标题】:How to create a recursive function that checks every substring?如何创建一个检查每个子字符串的递归函数?
【发布时间】:2020-11-10 02:24:27
【问题描述】:

如何使用递归函数找到任何给定字符串中的所有子字符串?我知道如何使用 2 个 for 循环来做到这一点,但我不知道如何使用递归来做到这一点。需要检查每个子字符串是否为回文。这是我的非迭代解决方案。

console.log(palindromeIterative("madam"));

function palindromeIterative(word) {
  let noOfP = 0;

  for (let i = 0; i < word.length; i++) {
    for (let j = i + 1; j <= word.length; j++) {
      noOfP = palindromeIterativeHelper(word.substring(i, j), noOfP);
    }
  }

  return noOfP;
}

function palindromeIterativeHelper(word, noOfP) {
  if (word === word.split("").reverse().join("") && word.length > 1) {
    console.log(word);
    noOfP++;
  }

  return noOfP;
}

【问题讨论】:

  • 您的递归函数应该检查给定的字符串以查看它是否是回文,然后遍历每个短一个字符的子字符串并在该子字符串上调用自身。例如,如果您的字符串是“ABC”(3 个字符长),该函数将检查“ABC”的回文性,然后调用自身两次:首先传递“AB”,然后传递“BC”(即所有2 个字符长的子字符串)。当要检查的字符串只有一个字符时,函数不再递归。
  • 我昨天回答了一个类似的问题。也许它有帮助? stackoverflow.com/a/64760183/1244884
  • 这是我的确切任务,哈哈

标签: javascript recursion palindrome


【解决方案1】:

这里是递归版本。这可能不是最佳优化的解决方案,但可以解决上述问题。

let str = "forgeeksskeegfor"
let noOfP = 0;
let getSubstrings = (str) => {
  let countPalindrome = (str) => {
    if (str.length < 2)
      return
    else {
      if (str == str.split("").reverse().join("")) {
        console.log(str);
        noOfP++;
      }
      countPalindrome(str.substring(0, str.length - 1))
    }
  }
  countPalindrome(str)
  if (str.length < 2) {
    return noOfP;
  }
  return getSubstrings(str.substring(1))
}
console.log(`Total Palindromes : ${getSubstrings(str)}`)

【讨论】:

    【解决方案2】:

    我会这样做:

    const sublists = (list, rightmost = true) => {
        if (list.length === 0) return [];
        const result = [list, ...sublists(list.slice(0, -1), false)];
        if (rightmost) result.push(...sublists(list.slice(1), true));
        return result;
    };
    
    const isPalindrome = string => string.length > 1 &&
        string.split("").reverse().join("") === string;
    
    const palindromes = string => sublists(string).filter(isPalindrome);
    
    console.log(palindromes("malayalam"));

    这是它的工作原理。我们执行深度优先搜索。对于每个字符串,我们递归地删除最后一个字母。对于每一层最右边的字符串,我们还递归地删除第一个字母。在我们生成所有子串的列表后,我们过滤那些是回文的子串。

    malayalam
    |        \
    malayala  alayalam
    |         |       \
    malayal   alayala  layalam
    |         |        |      \
    malaya    alayal   layala  ayalam
    |         |        |       |     \
    malay     alaya    layal   ayala  yalam
    |         |        |       |      |    \
    mala      alay     laya    ayal   yala  alam
    |         |        |       |      |     |   \
    mal       ala      lay     aya    yal   ala  lam
    |         |        |       |      |     |    |  \
    ma        al       la      ay     ya    al   la  am
    |         |        |       |      |     |    |   | \
    m         a        l       a      y     a    l   a  m

    上图是字符串malayalamsublists函数递归调用栈的可视化。回文以粗体突出显示。

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2018-08-31
      • 2016-01-17
      • 2013-05-12
      相关资源
      最近更新 更多