javascript - 为什么我的 for 循环没有递增,为什么我的拼接没有被改变?

标签 javascript arrays string hashmap

我遇到了一个看起来非常简单的问题,但是随着我编写更多代码,我的一些预期返回并不像我预期的那样。

感谢任何帮助。如果您确实提供了任何帮助,请解释您的方法以及我是如何陷入困境的。

问题:

We're given a string and need to see if it can be broken down into words from a dictionary array. For example:

const str = "applecomputer";
const dictArr = ["apple", "computer"];
stringBreakdown(str, dictArr);
// true

Assuming that there are no repeats in the dictionary array, can you write a method that will return true if the string can be broken down into words from the array, or false if not?

两个测试用例:

Expect stringBreakdown('crazyrichasians', [ 'crazy', 'rich', 'asians' ]) // to return true

Expect stringBreakdown('lockcombination', [ 'lock', 'combo' ]) // to return false

我的代码和方法:

  • 创建字符串中所有字符的 HashMap
  • 创建一个辅助函数,从数组中的每个字符串中删除字符
  • 当我删除字符串中的字符时,每次我从 HashMap 中看到该字母时,我也会减少
  • 如果我看到该字符串中的所有字母,那么我将从给定数组中删除它
  • 最后,如果给定的数组长度小于 0,则返回 true,因为我能够拼出所有单词,否则返回 false,因为数组中有更多单词
const stringBreakdown = (str, dictArr)=> {
  let hashDictionary = {};
  let shouldRemoveWord

  for(let x = 0; x <= str.length-1;x++){
    !hashDictionary[str[x]]  ? hashDictionary[str[x]] =1 : hashDictionary[str[x]]+=1
  }

for(let y = 0; y < dictArr.length;y++ ){
  shouldRemoveWord = removeLetters(hashDictionary,dictArr[y]) 
    if(shouldRemoveWord === true){
      dictArr.splice(y,1)
    }
  }
  console.log('dictArr',dictArr)
  return dictArr.length > 0 ? true : false;
}

const removeLetters = (hash,word) =>{
let modifiedWord = word.split('')

for(let k = 0; k < modifiedWord.length;k++){
    if(hash[word[k]]){
     modifiedWord.splice(k,1)
     hash[word[k]]-=1
   }
}
 return modifiedWord.join('').length < 0  ? true : false;
}

最佳答案

然后您可以遍历数组中提供的每个单词:

首先,检查所有单词组合在一起的长度是否与被测字符串的长度相匹配。如果不是返回false

其次,如果长度匹配,则检查每个单词作为一个整体是否作为子字符串包含在提供的字符串中:

function stringBreakdown(str, dictArr){
  return dictArr.join("").length === str.length
         && 
         dictArr.every(word => str.includes(word)); 
}
//tests
console.log(stringBreakdown('crazyrichasians', [ 'crazy', 'rich', 'asians' ]));
console.log(stringBreakdown('lockcombination', [ 'lock', 'combo' ]));
console.log(stringBreakdown('applecomputer', [ 'apple', 'computer']));
console.log(stringBreakdown('appelcomputer', [ 'apple', 'computer']));
console.log(stringBreakdown('appcolemputer', [ 'apple', 'computer']));
console.log(stringBreakdown('applecomputer', [ 'app', 'le', 'computer']));

你的方法是可疑的,因为当你逐个字符地浏览每个字符时,你没有看到它形成的词,即在你的情况下,如果 applecomputer 是字符串并且数组有 ['appel', 'comterpu'] 在这种情况下,您的算法将返回 true

这是因为您正在从输入字符串 str 制作一个字符映射,然后遍历每个单词的字符并减少它在字符映射中的出现次数,因此组合无关紧要。

const stringBreakdown = (str, dictArr)=> {
  let hashDictionary = {};
  let shouldRemoveWord

  for(let x = 0; x <= str.length-1;x++){
    !hashDictionary[str[x]]  ? hashDictionary[str[x]] =1 : hashDictionary[str[x]]+=1
  }

for(let y = 0; y < dictArr.length;y++ ){
  shouldRemoveWord = removeLetters(hashDictionary,dictArr[y]) 
    if(shouldRemoveWord === true){
      dictArr.splice(y,1)
    }
  }
  return dictArr.length > 0 ? true : false;
}

const removeLetters = (hash,word) =>{
let modifiedWord = word.split('')

for(let k = 0; k < modifiedWord.length;k++){
    if(hash[word[k]]){
     modifiedWord.splice(k,1)
     hash[word[k]]-=1
   }
}
 return modifiedWord.join('').length < 0  ? true : false;
}
//doesn't work outputs true
console.log(stringBreakdown('applecomputer', ['appel', 'computer']));

关于javascript - 为什么我的 for 循环没有递增,为什么我的拼接没有被改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57894468/

相关文章:

c++ - 错误 : Initializing Argument 1 of

arrays - Swift 字符串到单个字符串数组

javascript - WebGL - 从渲染缓冲区读取像素数据

javascript - 如何通过 IoT 提高 AWS Lambda 和 Web 应用程序之间 MQTT 消息的可靠性

ruby - 在 Ruby 中对哈希数组进行排序(按存在可选键+值排序)

c - 将输入文本保存到 main 中的结构

ios - UITextField .text 属性总是返回一个空字符串

c++ - 字符串 find_first 不更新字符串

javascript - 达到限制后禁用复选框

javascript - Chrome 扩展上的 browserAction 用于在网页上嵌入内容(如 Rapportive)