X
X
Xakephp2014-09-24 08:51:25
PHP
Xakephp, 2014-09-24 08:51:25

How to split text without repetition?

We need a function that parses the text into all combinations without repetition
. For example, like this
1) the boat swam swam and drowned
2) the boat swam swam and
3) swam swam and drowned
4) the boat swam swam
5) swam and drowned
6) the boat swam
7) swam swam
8) swam and
9) and drowned
10) boat
11) swam
12) and
13) drowned
Maybe I even missed it, I don’t know for sure

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petrov, 2014-09-24
@Xakephp

var start = ('кораблик плавал плавал и утонул').split(' ');

for (var i = 0, result = [], _result = {}, stroke; i <= start.length; i++) {
  for (var j = 0; j < i; j++) {
    stroke = start.slice(j, j + start.length - i + 1).join(' ');

    if (!_result[stroke]) {
      _result[stroke] = result.push(stroke);
    }
  }
}

console.log(result);

jsfiddle.net/petroveg/agwtd8vz

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question