I want to find patterns in a string for compression.
Lets say I have a string, "abcdabcr".
I want to find repeating patterns of length 2 or more, eg abc, not overlapping.
How can I do this?
I have tried searching this, but it is hard to find answers.
I have this code:
function findPatterns(str) {
let sbf=[]
let patterns=[]
let chunks = str.match(/.{1,2}/gm)
chunks.forEach((c)=>{
if (sbf.includes(c)) {
patterns.push(c)
}
sbf.push(c)
})
return [...new Set(patterns)]
}
It works for finding patterns of length = 2, but not length >= 2.