Here's the problem. Say I have these strings:
- apple ipad mini 32gb
- apple ipad mini 64gb
- apple ipad air 64gb
- apple ipad air 32gb
- panasonic gh4
- samsung s2 galaxy
- samsung s2 galaxy red
- samsung s3 galaxy
I want these strings to be grouped like this:
- apple ipad mini: [apple ipad mini 32gb, apple ipad mini 64gb]
- apple ipad air: [apple ipad air 64gb, apple ipad 32gb]
- panasonic gh4: [panasonic gh4]
- samsung s2 galaxy: [samsung s2 galaxy, samsung s2 galaxy red]
- samsung s3 galaxy
The point is to separate name of the item from its attributes(color, memory capacity etc).
I used this algorithm for finding longest common substring: link
Can you guys share your ideas? No code or implementation needed. Thank you.
Edited:
this.data = _.sortBy(this.data, function(item) {
return item.title;
});
var i = 0;
var groups = {};
var len = this.data.length - 1;
while(i < len) {
var key = this.lcs(this.data[i][this.attr], this.data[i+1][this.attr]) || this.data[i][this.attr];
groups[key] = true;
i++;
while(this.data[i][this.attr].startsWith(key) && i < len) {
i++;
}
}
console.log(groups)
This works great(tested only adding keys). But I want to add samsung s3 galaxy to list too. Thanks for help guys!