【问题标题】:Create String Combinations That Include k Amount of Consecutive Strings From The Array从数组中创建包含 k 个连续字符串的字符串组合
【发布时间】:2019-08-18 16:44:44
【问题描述】:

如果我们有以下数组:

["it", "wkppv", "ixoyx", "3452", "zzzzzzzzzzzz"]

如果我们尝试获取所有可能的 k 字符串组合(在本例中为 3),而不进行随机化,我们应该得到以下字符串:

'itwkppvixoyx'
'wkppvixoyx3452
'ixoyx3452zzzzzzzzzzzz'

因为当然,

"it" + "wkppv" + "ixoyx" = "itwkppvixoyx",

"wkppv" + "ixoyx" + "3452" = "wkppvixoyx3452",

"ixoyx" + "3452" + "zzzzzzzzzzzz" = "ixoyx3452zzzzzzzzzzzz"

但我还没有找到创建这些字符串的方法。

我了解如何根据k创建包含first字符串和last字符串的字符串,例如:

function createStrings (array, k) {
    let strings = [];
    for (let i = 0; i < array.length - (k - 1); i++) {
        strings.push(`${array[i]}${(array[i + (k-1)])}`);
    }    
    return strings;
}

console.log(createStrings(["it", "wkppv", "ixoyx", "3452", "zzzzzzzzzzzz"], 3));

当然,这些都缺少“中间”元素。

如果 k 是 2 或 4 怎么办?我需要找到一种方法来创建字符串组合,其中包含 k 个数组中的 连续 个字符串。关于如何做到这一点的任何想法?

【问题讨论】:

  • 可以使用滑动窗口之类的东西。

标签: javascript arrays string


【解决方案1】:

这是滑动窗口的问题,窗口大小为k

解决方案是创建一个初始窗口,然后将其滑向末尾。

我记录在案的解决方案在这里

function createCombinations(stringArray, k) {
    // Define an empty array to store combinations
    let result = [];

    // Make a combination for starting window
    let initialWindow = '';

    for (let i = 0; i < k; i++) {
        // '+' operator, by default string concatenation
        initialWindow += stringArray[i];
    }

    // Add it to Result array
    result.push(initialWindow);

    // Iterate over remaining stringArray element
    for (let i = k; i < stringArray.length; i++) {

        // For debugging
        // console.log('Initial Combination->', initialWindow, initialWindow.length);
        // console.log('Remove first entry->', stringArray[i-k], stringArray[i-k].length);
        // console.log('After remove->', initialWindow.slice(stringArray[i-k].length));

        // Remove k previous element and add next element
        initialWindow = initialWindow.slice(stringArray[i-k].length) + stringArray[i];

        // console.log('After add next->', initialWindow);

        result.push(initialWindow);
    }

    return result;
}

/* Tests */

console.log(createCombinations(['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'], 3));
console.log(createCombinations(['This', 'is', 'Some', 'Random', 'String'], 4));
console.log(createCombinations(['What', 'Am', 'I', 'Doing', 'Here', 'Some', 'Other'], 2));
console.log(createCombinations(['1', '2', '3', '4', '5', '6', '7'], 3));

注意:如果你传递没有' ' 的数字,它将评估表达式而不是字符串连接。

【讨论】:

    【解决方案2】:

    arrow functionsmap 的较短替代方案:

    const createStrings = (a, k) => a.slice(k - 1).map((v, i) => a.slice(i, i + k).join(''))
    
    console.log( createStrings([1, 2, 3, 4, 5], 4) )
    console.log( createStrings(["it", "wkppv", "ixoyx", "3452", "zzzzzzzzzzzz"], 3) )

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多