JavaScript过程,用于查找一个字符串中但不存在于另一个字符串中的字符

时间:2014-03-25 20:44:08

标签: javascript algorithm

我一直试图写一个而且它变得凌乱!

假设我有两个字符串textStarttextTarget,并且我想跟踪我需要添加和删除textStart的字符,以便产品textTarget

例如,如果textStart = "dude"textTarget = "deck",则需要添加的字符为'c'和{{1并且需要减去的字符将是'k''u'之一。

我认为我首先需要创建代表'd'textStart中每个角色数量的地图。

所以我写了这个:

textTarget

哪会给我

var startChars = {}; for (var k = 0, n = textStart.length; k < n; ++k) { if (textStart[k] in startChars) ++startChars[textStart[k]]; else startChars[textStart[k]] = 1; } var targetChars = {}; for (var k = 0, n = textTarget.length; k < n; ++k) { if (textTarget[k] in startChars) ++targetChars[textTarget[k]]; else map1[targetChars[k]] = 1; } = startChars['d']2 = startChars['u']1 = startChars['e']

1 = targetChars['d']1 = targetChars['e']1 = targetChars['c']1 = targetChars['k']

然后我可以制作创建地图1needAdded来查看上述两张地图的不同之处:

needRemoved

我不确定如何按预期填写这些地图,因为我不知道如何使用JavaScript迭代地图的键。我不知何故最终需要

var needAdded = {}; var needRemoved = {}; = needAdded['c']1 = needAdded['k']1 = needRemoved['u']1 = needRemoved['d']

你们进来帮助我的地方。

我希望我已经很好地描述了我想要做的事以及到目前为止我是如何尝试做的。我的编程直觉告诉我,我编写了太多代码行,我需要咨询StackOverflow以寻求帮助。没有JQuery或Regex的任何方式优雅地做到这一点?我知道有人会进入这个主题并编写一个1行的Regex解决方案或类似的东西。

3 个答案:

答案 0 :(得分:0)

var s = 'dude',
t = 'deck',
finalOutput = '';
for (var i = 0; i < s.length; i++){
    if ( typeof t[i] != 'undefined' ){
        if ( s[i] != t[i] ){
            console.log(s[i] + ' changed to ' + t[i]);
            s[i] = t[i];
            finalOutput += t[i];

        } else{
            finalOutput += s[i];
        }
    }
}
console.log('FINAL: ' + finalOutput);

答案 1 :(得分:0)

这里是一个jsfiddle我只花了太多时间......希望它有意义:))

var textStart = 'dude';
var textTarget = 'deck';

var startChars = {};
for (var k = 0, n = textStart.length; k < n; ++k)
{
    if (textStart[k] in startChars)
        ++startChars[textStart[k]];
    else
        startChars[textStart[k]] = 1;
}

var targetChars = {};
for (var k = 0, n = textTarget.length; k < n; ++k)
{
    if (textTarget[k] in targetChars)
        ++targetChars[textTarget[k]];
    else
        targetChars[textTarget[k]] = 1;
}

console.log('start: ' + JSON.stringify(startChars));
console.log('target: ' + JSON.stringify(targetChars));

var needAdded = {};
var needRemoved = {};

for (var c in startChars) {
    // If target does not contain letter, remove all, otherwise remove excess
    if (targetChars[c] > 0) {
        if (startChars[c] > targetChars[c])
            needRemoved[c] = startChars[c] - targetChars[c];
        else if (startChars[c] < targetChars[c])
            needAdded[c] = targetChars[c] - startChars[c];
    } else {
        needRemoved[c] = startChars[c];
    }
}

for (var c in targetChars) {
    // If start does not contain letter, add all, otherwise add excess
    if (startChars[c] > 0) {
        if (startChars[c] > targetChars[c])
            needRemoved[c] = startChars[c] - targetChars[c];
        else if (startChars[c] < targetChars[c])
            needAdded[c] = targetChars[c] - startChars[c];
    } else {
        needAdded[c] = targetChars[c];
    }
}

console.log('needAdded: ' + JSON.stringify(needAdded));
console.log('needRemoved: ' + JSON.stringify(needRemoved));

输出如下:

start: {"d":2,"u":1,"e":1}
target: {"d":1,"e":1,"c":1,"k":1}
needAdded: {"c":1,"k":1}
needRemoved: {"d":1,"u":1} 

答案 2 :(得分:0)

好的,也有太多时间:

var textStart = "dude";
var textTarget = "duck";

var map = {};
MapCharacters(textStart, map, 1);
MapCharacters(textTarget, map, -1);
console.log(map);

var toDelete = [];
var toAdd = [];

for (var prop in map) {
    if (map.hasOwnProperty(prop)) {
        while (map[prop] > 0) {
            toDelete.push(prop);
            map[prop]--;
        }
        while (map[prop] < 0) {
            toAdd.push(prop);
            map[prop]++;
        }

    }
}

console.log(toDelete);
console.log(toAdd);

function MapCharacters(string, map, add) {
    for (var k = 0, n = string.length; k < n; ++k) {
        if (string[k] in map) {
            map[string[k]] += add;
        } else {
            map[string[k]] = add;
        }
    }
}

http://jsfiddle.net/nSV2J/1/

它可能会更有效率地完成,但正如我所说 - 太多时间!

我意识到这样做的最好方法不是制作两张地图,而只制作一张。在第一种情况下,您增加每个字母的计数,在第二种情况下,您减少它。现在很容易找到哪些需要删除(最终结果> 0)以及需要添加哪些(最终结果为&lt; 0)