如何解决这个js嵌套数组问题:给定一个单词和一个字符串骨架数组,返回一个可能的骨架单词匹配数组

发布于 2025-01-13 13:48:46 字数 1101 浏览 1 评论 0原文

在这工作了很多小时之后,我不知道如何解决以下问题。请提供解决方案并提供解释或建议以改进我的解决方案。

/* instructions
Given a word and an array of string skeletons, return an array of all of the skeletons that could be turned into the word by replacing the '-' with letters. If there are no possible matches return an empty string example: given the word 'hello' 'he---' would 
be a match, 'h--l' or 'g-llo' would not be a match */

// example test case:
let word = 'hello';
let skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'h-l--'];

function findSkels(word, skeleton){
let goodSkels = [];
skeleton = skeletons.filter(w => w.length === word.length);
console.log(skeletons)
  for(let sw = 0; sw < skeletons.length; sw++){
    let possibleMatch = true;
    for(let letter = 0; letter < word.length; letter++){
      if(word[letter] !== skeletons[sw][letter] || skeletons[sw][letter] == '-'){
        possibleMatch = false
      }
    }
    if(possibleMatch){
      goodSkels.push(skeletons[sw])
    }
  } 
    return goodSkels;
}

after working at this for many hours, I am lost at how to solve the following problem. Please offer solutions with explanations or suggestions to improve my solution.

/* instructions
Given a word and an array of string skeletons, return an array of all of the skeletons that could be turned into the word by replacing the '-' with letters. If there are no possible matches return an empty string example: given the word 'hello' 'he---' would 
be a match, 'h--l' or 'g-llo' would not be a match */

// example test case:
let word = 'hello';
let skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'h-l--'];

function findSkels(word, skeleton){
let goodSkels = [];
skeleton = skeletons.filter(w => w.length === word.length);
console.log(skeletons)
  for(let sw = 0; sw < skeletons.length; sw++){
    let possibleMatch = true;
    for(let letter = 0; letter < word.length; letter++){
      if(word[letter] !== skeletons[sw][letter] || skeletons[sw][letter] == '-'){
        possibleMatch = false
      }
    }
    if(possibleMatch){
      goodSkels.push(skeletons[sw])
    }
  } 
    return goodSkels;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2025-01-20 13:48:46

您很接近,但

if (word[letter] !== skeletons[sw][letter] || skeletons[sw][letter] == '-') {
  possibleMatch = false
}

如果有任何字母不匹配该字母是-,则会取消骨架资格。因此,这将取消任何不完全匹配的单词的资格。您需要使用 && - 仅当字母不匹配字母不是 - 时才取消资格。

/* instructions
Given a word and an array of string skeletons, return an array of all of the skeletons that could be turned into the word by replacing the '-' with letters. If there are no possible matches return an empty string example: given the word 'hello' 'he---' would 
be a match, 'h--l' or 'g-llo' would not be a match */

// example test case:
let word = 'hello';
let skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'h-l--'];

function findSkels(word, skeleton) {
  let goodSkels = [];
  skeleton = skeletons.filter(w => w.length === word.length);
  for (let sw = 0; sw < skeletons.length; sw++) {
    let possibleMatch = true;
    for (let letter = 0; letter < word.length; letter++) {
      if (word[letter] !== skeletons[sw][letter] && skeletons[sw][letter] !== '-') {
        possibleMatch = false
      }
    }
    if (possibleMatch) {
      goodSkels.push(skeletons[sw])
    }
  }
  return goodSkels;
}
console.log(findSkels(word, skeletons));

或者,重构为看起来更好:

const word = 'hello';
const skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'h-l--'];

const findSkels = (word, skeletons) => skeletons
  .filter(skel => (
    skel.length === word.length &&
    [...skel].every((char, i) => char === '-' || char === word[i])
  ));
console.log(findSkels(word, skeletons));

数组方法通常是一种比命令式基于索引的 for 循环更简洁的好方法。如果您实际上并不关心索引,只关心正在迭代的值,那么您通常可以完全放弃 for (let i = 构造,并使用数组方法或 for..of.

You're close, but

if (word[letter] !== skeletons[sw][letter] || skeletons[sw][letter] == '-') {
  possibleMatch = false
}

will disqualify a skeleton if any letter doesn't match or the letter is a -. So this'll disqualify any words that aren't exact matches. You want && instead - disqualify only if the letter doesn't match and the letter isn't a -.

/* instructions
Given a word and an array of string skeletons, return an array of all of the skeletons that could be turned into the word by replacing the '-' with letters. If there are no possible matches return an empty string example: given the word 'hello' 'he---' would 
be a match, 'h--l' or 'g-llo' would not be a match */

// example test case:
let word = 'hello';
let skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'h-l--'];

function findSkels(word, skeleton) {
  let goodSkels = [];
  skeleton = skeletons.filter(w => w.length === word.length);
  for (let sw = 0; sw < skeletons.length; sw++) {
    let possibleMatch = true;
    for (let letter = 0; letter < word.length; letter++) {
      if (word[letter] !== skeletons[sw][letter] && skeletons[sw][letter] !== '-') {
        possibleMatch = false
      }
    }
    if (possibleMatch) {
      goodSkels.push(skeletons[sw])
    }
  }
  return goodSkels;
}
console.log(findSkels(word, skeletons));

Or, refactored to look nicer:

const word = 'hello';
const skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'h-l--'];

const findSkels = (word, skeletons) => skeletons
  .filter(skel => (
    skel.length === word.length &&
    [...skel].every((char, i) => char === '-' || char === word[i])
  ));
console.log(findSkels(word, skeletons));

Array methods are often a nice way to make code much cleaner than imperative index-based for loops. If you don't actually care about the index, only the value being iterated over, you can often ditch the for (let i = construct entirely, and either use an array method or for..of.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文