【问题标题】:Interleave two strings of different length交错两个不同长度的字符串
【发布时间】:2020-07-07 12:04:18
【问题描述】:

我有两个字符串 a 和 b,b 的长度是 a 长度的一半,我尝试创建一个循环以将 b 的字符每 2 个 a 字符放入我试过这个

let a = "pekaoo";
let b = "eb!";
let stop = a.length;
for (let i = 1; i <= stop; i += 2) {
    a = a.substring(0,i) + b[i/2] + a.substring(i);
}

console.log(a);

应该显示'peekaboo!但不工作

【问题讨论】:

  • a.stop 没有任何意义。

标签: javascript string loops


【解决方案1】:

您可以用单个字母迭代字符串,并使用两倍的因子对较长的字符串进行切片。

然后将所有部分添加到一个新字符串中。

let a = "pekaoo",
    b = "eb!",
    result = '';

for (let i = 0; i < b.length; i++) {
    result += a.slice(i * 2, (i + 1) * 2) + b[i];
}

console.log(result);

【讨论】:

  • 一如既往的简洁解决方案!
【解决方案2】:

使用replace()

let a = "pekao";
let b = "eb!";

let arr = [...b]
let res = a.replace(/.{1,2}/g, match => match + arr.shift())

console.log(res);

【讨论】:

    【解决方案3】:

    对于您非常具体的要求,我能够编写以下似乎可行的代码。也应该使用更大的字符串(然后您必须进行一些小的更改)。

    let a = "pekaoo";
    let b = "eb!";
    var outcome = "";
    
    for(let i = 0; i < a.length/2; i++) {
        outcome += a.substring((i*2),((i+1)*2)) + b.substring(i, i+1);
    }
    
    console.log(outcome)

    【讨论】:

      【解决方案4】:

      let a = "pekaoo";
      let b = "eb!";
      
      let result = "";
      
      for (let i = 0; i < a.length; i++) {
          result += a[i];
      
          if ((i - 1) % 2 == 0){
          result += b[(i - 1) / 2]
          }
          
      }
      
      console.log(result);

      【讨论】:

        【解决方案5】:

        并使用substr 代替substring

        const a = "pekaoo",
              b = "eb!";
        let res = "";
        
        for (let i = 0; i <= a.length; i += 2) {
            res += a.substr(i, 2) + b.substr(i/2, 1);
        }
        
        console.log(res);

        【讨论】:

          【解决方案6】:

          这可能看起来很多,但它是一种更算法化的解决方案。

          1. 首先您需要确定最长的单词,因为这将驱动您的 reducer 数组。
          2. 现在,当您减少每个字符位置时,您将减少每个字符串:
            • 如果当前字符串长度等于最长字符串,则只需返回全局索引处的字符。
            • 否则,如果全局索引的模与当前字符串的比例为1,则返回当前字符串比例的字符偏移量。

          const longestArr = arr => arr.reduce((a, b) => a.length > b.length ? a : b);
          const interleave = (...strArr) => {
            let longest = longestArr(strArr),
                ratios = strArr.map(s => Math.floor(longest.length / s.length));
            return longest.split('').reduce((r, _, globalIndex) => {
              return strArr.reduce((r1, s, localIndex) => {
                let ratio = ratios[localIndex];
                if (ratio === 1) {
                  return r1 + s.charAt(globalIndex); // Default case
                } else {
                  if (globalIndex % ratio === 1) {
                    return r1 + s.charAt(Math.floor(globalIndex / ratio));
                  } else {
                    return r1;
                  }
                }
              }, r);
            }, '');
          }
          
          console.log(interleave('pekabo!', 'eo'));
          console.log(interleave('pekaoo', 'eb!')); // Use-case
          console.log(interleave('pko', 'eao', 'eb!'));
          console.log(interleave('pa', 'eb', 'eo', 'ko', '!'));
          .as-console-wrapper { top: 0; max-height: 100% !important; }

          【讨论】:

            猜你喜欢
            • 2019-02-15
            • 2022-11-16
            • 2016-01-17
            • 1970-01-01
            • 1970-01-01
            • 2012-12-23
            • 1970-01-01
            • 2011-10-11
            • 2018-03-18
            相关资源
            最近更新 更多