Reputation:
Let us assume I got two strings. In my case the strings are "stringA" and "stringB":
Example 1:
let stringA = "1ABC DEFGHI";
let stringB = "XYZABC DEFGHI";
Even if the two strings are not exactly the same, they still contain a large amount of letter sequences, which are identical in both. In the case above it is the string "ABC DEFGHI"
that occurs in both of them.
Example 2: And heres another example:
let stringA = "0142 41193566"
let stringB = "+49 142 41193566"
In this case the result should be 142 41193566
because this string occurs in both of them.
I would describe the operation as a kind of mask operation, but I have not progressed so far in implementing it. Unfortunately, this code snippet is everything I can offer so far.
let stringA = "0142 41193566"
let stringB = "+49 142 41193566"
let stringC = "....ThISisATest"
let stringD = "+Th.ISisATest33"
let detectBiggestOccurrence = function(stringA, stringB) {
let result = []
for (let c in stringA) {
if (stringB.includes(stringA[c])) {
let index = stringB.indexOf(stringA[c])
result+=stringB[index]
}
}; return result
}
let resultWorking = detectBiggestOccurrence(stringA, stringB)
console.log("working:", resultWorking)
let resultNotWorking = detectBiggestOccurrence(stringC, stringD)
console.log("not working:", resultNotWorking)
Issue: The code above is working for the first call (detectBiggestOccurrence(stringA, stringB)
) but it does not work for the second one (detectBiggestOccurrence(stringC, stringD)
).
Upvotes: 3
Views: 124
Reputation: 28196
Here is a modified version of Pierre Capo's answer. It will return a correct result, even if a "problematic" string should be tested (see my comment under Pierre's answer).
function maxmatch(a,b){
var i=0,res='',pat=a[i];
while (i<a.length) {
if (b.includes(pat)) {
if (pat.length>res.length) res=pat;
pat+=a[++i];
}
else {
if (pat.length>1) pat=pat.slice(1);
else pat=a[++i];
}
}
return res;
}
let testStrings=[["1ABC DEFGHI","XYZABC DEFGHI"],
["1ABC DEFGHI","XYZBC DEFGHI ABC"],
["0142 41193566","+49 142 41193566"],
["....ThISisATest","+Th.ISisATest33"]];
testStrings.forEach(t=>console.log(maxmatch(...t)))
When applied to the test strings (please notice: I added a modified version of the first test string) they will all return the correct answer:
ABC DEFGHI
BC DEFGHI
142 41193566
ISisATest
Upvotes: 0
Reputation: 1053
The approach that I've used to solve your problem :
function detectBiggestOccurrence(stringA, stringB){
var mask ="";
var response ="";
stringA.split('').forEach( el => {
mask +=el;
if(stringB.includes(mask)){
if(mask.length > response.length){ response = mask; }
}
else {
mask =el;
}
})
return response;
}
let stringA = "1ABC DEFGHI";
let stringB = "XYZABC DEFGHI";
console.log(detectBiggestOccurrence(stringA, stringB));
let stringC = "0142 41193566";
let stringD = "+49 142 41193566";
console.log(detectBiggestOccurrence(stringC, stringD));
let stringE = "....ThISisATest"
let stringF = "+Th.ISisATest33"
console.log(detectBiggestOccurrence(stringE, stringF));
Upvotes: 2