我需要找到两个字符串之间的区别。
const string1 = 'lebronjames';
const string2 = 'lebronnjames';
预期的输出是找到多余的n并将其记录到控制台。
在JavaScript中有什么方法可以做到吗?
答案 0 :(得分:1)
function getDifference(a, b)
{
var i = 0;
var j = 0;
var result = "";
while (j < b.length)
{
if (a[i] != b[j] || i == a.length)
result += b[j];
else
i++;
j++;
}
return result;
}
console.log(getDifference("lebronjames", "lebronnjames"));
答案 1 :(得分:1)
用于更复杂的差异检查的另一种方法是使用PatienceDiff算法。我将此算法移植到Javascript上...
https://github.com/jonTrent/PatienceDiff
...尽管该算法通常用于文本(例如计算机程序)的逐行比较,但仍可以用于逐字符比较。例如,要比较两个字符串,可以执行以下操作...
let a = "thelebronnjamist";
let b = "the lebron james";
let difference = patienceDiff( a.split(""), b.split("") );
...将difference.lines
设置为具有比较结果的数组...
difference.lines: Array(19)
0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
无论aIndex === -1
还是bIndex === -1
都表明两个字符串之间存在差异。具体来说...
b
的位置3中找到了字符“”。a
的位置9中找到字符“ n”。b
的位置10中找到了字符“”。a
的位置13中找到了字符“ i”。b
的位置14中找到了字符“ e”。a
的位置15中找到了字符“ t”。请注意,PatienceDiff算法对于比较两个相似的文本或字符串块很有用。它不会告诉您是否进行了基本编辑。例如,以下...
let a = "james lebron";
let b = "lebron james";
let difference = patienceDiff( a.split(""), b.split("") );
...返回difference.lines
包含...
difference.lines: Array(18)
0: {line: "j", aIndex: 0, bIndex: -1}
1: {line: "a", aIndex: 1, bIndex: -1}
2: {line: "m", aIndex: 2, bIndex: -1}
3: {line: "e", aIndex: 3, bIndex: -1}
4: {line: "s", aIndex: 4, bIndex: -1}
5: {line: " ", aIndex: 5, bIndex: -1}
6: {line: "l", aIndex: 6, bIndex: 0}
7: {line: "e", aIndex: 7, bIndex: 1}
8: {line: "b", aIndex: 8, bIndex: 2}
9: {line: "r", aIndex: 9, bIndex: 3}
10: {line: "o", aIndex: 10, bIndex: 4}
11: {line: "n", aIndex: 11, bIndex: 5}
12: {line: " ", aIndex: -1, bIndex: 6}
13: {line: "j", aIndex: -1, bIndex: 7}
14: {line: "a", aIndex: -1, bIndex: 8}
15: {line: "m", aIndex: -1, bIndex: 9}
16: {line: "e", aIndex: -1, bIndex: 10}
17: {line: "s", aIndex: -1, bIndex: 11}
请注意,PatienceDiff不会报告姓氏和名字的交换,而是提供一个结果,显示从a
中删除了哪些字符,以及向b
中添加了哪些字符以结束结果为b
。
编辑:添加了名为 patienceDiffPlus 的新算法。
仔细研究了上面提供的最后一个示例,该示例显示了PatienceDiff在识别可能移动的行方面的局限性,让我意识到,有一种优雅的方法可以使用PatienceDiff算法来确定是否有任何行确实确实在移动,不仅仅是显示删除和添加。
简而言之,我在PatienceDiff.js文件的底部添加了patienceDiffPlus
算法(到上面标识的GitHub存储库中)。 patienceDiffPlus
算法采用最初的patienceDiff
算法中删除的aLines []和添加的bLines [],然后再次通过patienceDiff
算法运行它们。即,patienceDiffPlus
正在寻找可能移动的行的最长公共子序列,因此它将其记录在原始patienceDiff
结果中。 patienceDiffPlus
算法会继续执行此操作,直到找不到更多的移动行为止。
现在,使用patienceDiffPlus
,进行以下比较...
let a = "james lebron";
let b = "lebron james";
let difference = patienceDiffPlus( a.split(""), b.split("") );
...返回difference.lines
包含...
difference.lines: Array(18)
0: {line: "j", aIndex: 0, bIndex: -1, moved: true}
1: {line: "a", aIndex: 1, bIndex: -1, moved: true}
2: {line: "m", aIndex: 2, bIndex: -1, moved: true}
3: {line: "e", aIndex: 3, bIndex: -1, moved: true}
4: {line: "s", aIndex: 4, bIndex: -1, moved: true}
5: {line: " ", aIndex: 5, bIndex: -1, moved: true}
6: {line: "l", aIndex: 6, bIndex: 0}
7: {line: "e", aIndex: 7, bIndex: 1}
8: {line: "b", aIndex: 8, bIndex: 2}
9: {line: "r", aIndex: 9, bIndex: 3}
10: {line: "o", aIndex: 10, bIndex: 4}
11: {line: "n", aIndex: 11, bIndex: 5}
12: {line: " ", aIndex: 5, bIndex: 6, moved: true}
13: {line: "j", aIndex: 0, bIndex: 7, moved: true}
14: {line: "a", aIndex: 1, bIndex: 8, moved: true}
15: {line: "m", aIndex: 2, bIndex: 9, moved: true}
16: {line: "e", aIndex: 3, bIndex: 10, moved: true}
17: {line: "s", aIndex: 4, bIndex: 11, moved: true}
请注意,添加了moved
属性,该属性标识是否可能移动了一行(或本例中的字符)。同样,patienceDiffPlus
仅匹配已删除的aLines []和添加的bLines [],因此无法保证这些行实际上已移动,但是很有可能确实已移动了这些行。
答案 2 :(得分:1)
对于那些想要返回first 两个字符串之间的差异的人可以这样调整:
const getDifference = (s, t) => {
s = [...s].sort();
t = [...t].sort();
return t.find((char, i) => char !== s[i]);
};
console.log(getDifference('lebronjames', 'lebronnjames'));
console.log(getDifference('abc', 'abcd'));
const getDifference = (s, t) => {
let sum = t.charCodeAt(t.length - 1);
for (let j = 0; j < s.length; j++) {
sum -= s.charCodeAt(j);
sum += t.charCodeAt(j);
}
return String.fromCharCode(sum);
};
console.log(getDifference('lebronjames', 'lebronnjames'));
console.log(getDifference('abc', 'abcd'));
答案 3 :(得分:1)
function findDifference(s, t) {
if(s === '') return t;
// this is useless and can be omitted.
for(let i = 0; i < t.length; i++) {
if(!s.split('').includes(t[i])) {
return t[i];
}
}
// this is useless and can be omitted.
// (if the additional letter exists)
// cache them, count values, different values of the same letter would give the answer.
const obj_S = {};
const obj_T = {};
for(let i = 0; i < s.length; i++) {
if(!obj_S[s[i]]) {
obj_S[s[i]] = 1;
}else {
obj_S[s[i]]++;
}
}
for(let i = 0; i < t.length; i++) {
if(!obj_T[t[i]]) {
obj_T[t[i]] = 1;
}else {
obj_T[t[i]]++;
}
}
for(const key in obj_T) {
if(obj_T[key] !== obj_S[key]) {
return key
}
}
}
// if more than 1 letter -> store the values and the return, logic stays the same.
console.log(findDifference('john', 'johny')) // --> y
console.log(findDifference('bbcc', 'bbbcc')) //--> b
实际上可以省略第一部分(第一个 for 循环)我对边缘情况的解决方案解决了整个问题,因为如果该值不存在,它将是 undefined 并且 count !== undefined 将返回字母...< /p>
答案 4 :(得分:0)
这将返回两个字符串之间的第一个差异
像lebronjames
和lebronnjames
这样的人是n
const string1 = 'lebronjames';
const string2 = 'lebronnjabes';
const findFirstDiff = (srt1, str2) =>
str2[[...string1].findIndex((el, index) => el !== string2[index])];
// equivalent of
const findFirstDiff2 = function(srt1, str2) {
return str2[[...string1].findIndex(function(el, index) {
return el !== string2[index]
})];
}
console.log(findFirstDiff2(string1, string2));
console.log(findFirstDiff(string1, string2));