【问题标题】:Find how much of a string is needed before it becomes unique among a set of strings找出在一组字符串中变得唯一之前需要多少字符串
【发布时间】:2021-06-13 20:53:11
【问题描述】:

这很难解释,但我会尽力...

我有一个字符串数组。让我们在这里举个例子:

var myArray = [
    "hello there my friend robert"
    "hello there friend kimberly"
    "hello friend claire"
    "hi there friend chris"
]

我正在尝试编写一个函数,该函数将确定数组中每个字符串在变得唯一之前需要多少个单词。例如,使用上述数组作为输入,函数应返回以下内容:

[
    "hello there my",
    "hello there friend",
    "hello friend",
    "hi"
]

返回数组中的字符串表示在字符串变得唯一之前每个字符串所需的单词。

例如,该函数为“你好,我的朋友罗伯特”返回“你好,我的”。这是因为“你好”本身是模棱两可的。它可以指“你好,我的朋友罗伯特”或“你好,朋友金伯利”。但是,“hello there my”只能指“hello there my friend robert”。

我完全不知道如何做到这一点。还有其他人知道解决方案吗?

(为简单起见,假设输入字符串没有标点符号并且完全小写。)

【问题讨论】:

  • 应该是'hi',因为第一个字和其他字不一样
  • @YasharthDubey 是正确的。我试图找出从字符串开头开始需要多少个单词。由于 "chris" 位于字符串的末尾,因此不会使用它。
  • 你有没有试过我的解决方案兄弟

标签: javascript arrays string


【解决方案1】:

我想我对此有一些想法。你可以做的就是像这样存储列表。

{
   'first' : ['hello', 'hello', 'hello', 'hi'],
   'second' : ['there','friend','there'],
   'third' : ['my', 'friend', 'claire'],
   'fourth' : ['robert', 'kimberly', 'chris']
}

现在只需检查这张地图上第一个的计数是否超过一个,那么你必须继续前进,如果计数是一个,你必须停在那里。

【讨论】:

  • 这似乎是一个聪明的方法!我只需要先将数组转换为这种格式。感谢您的帮助!
【解决方案2】:
var myArray = [
    "hello there my friend robert",
    "bb gnhgn",
    "hello there friend kimberly",
    "hello friend claire",
    "hi there friend chris"
];
function prepareArray(arr){
    let res = [];
  
    arr.forEach((element) => {
        console.log(element.split(" "));
        res.push(element.split(" "));
    });
   
    return res;
}
function calcWordsWeights(splitedArr){
   let res = [];
   let searchMask = new Array(splitedArr.length).fill(1);
  
   for(let i=0;i<getMaxLength(splitedArr);i++){
        splitedArr.forEach((elem,index)=>{
           
            if(searchMask[index]==true){
            if(res.length <= i){
                res[i]=[];
               
            }
            let isAdded=false;
            if(res[i].length<=0){
                res[i].push({word:elem[i],weight:0});
               
            }else{
                res[i].forEach(e=>{
                    if(e.word == elem[i]){
                        e.weight=e.weight+1;  
                        isAdded=true;  
                    }
                    
                });
                if(!isAdded){
                    res[i].push({word:elem[i],weight:0});
                }
            }
        
           
        }
        });
        if(res[i]==undefined){
            break;
        }else{
            res[i].forEach(el=>{
                if(el.weight==0){
                    for(let index in splitedArr){
                        if(splitedArr[index][i]==el.word){
                            searchMask[index]=0;
                        }
                    }
                }
            });
        }
     
     
        
   }
   console.log(res);
   return res;
}
function regenerateUniqueSentences(resArr){
    console.log(resArr);
    let numOfUniqueSentences=0; 
     resArr[0].forEach(el=>{
        numOfUniqueSentences += el.weight+1;
    });

    const finalRes =[];
   
  
    resArr.forEach((el,index)=>{
        let counter =0;
        
        el.forEach(e=>{
            for(let x=0;x<= e.weight;x++){
                if(index==0){
                    finalRes.push({txt:e.word,isDone:(e.weight==0?true:false)});
                }else{
                    if(!finalRes[counter].isDone){
                        finalRes[counter].txt += " "+ e.word;
                    }
                } 
                counter++;  
            }
        });
    });
   return finalRes;
}
function checkForExistingWord(resArr,word){
    let isExist =false;
    resArr.forEach(elem=>{
        if(elem.word == undefined  ){
            return false;
        }else if(elem.word == word){
            return true;
        }
    })
}
function getMaxLength(arr){
    let max=0;
    arr.forEach((element)=>{
        if(max < element.length)
            max= element.length;
    });
    return max
}
function stopSearchInArray(arr,word,weight){

}
(function(){
    const splitedArr = prepareArray(myArray);
    const weigthedArr =calcWordsWeights(splitedArr);
    console.log(regenerateUniqueSentences(weigthedArr));
})();

【讨论】:

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