LoginSignup
2
0

More than 5 years have passed since last update.

文字列の中にある全ての並んだパターンを作る

Posted at

タイトルって難しいな...

やりたいこと

abcabc

の中から

abcabc, abcab, bcabc, abca, bcab, cabc,...

といった感じにパターンを作りたい。

結論

var str = "abc".repeat(2);
var arr = []
for (var i = 0; i < str.length; i++) {
    for (var c = 0; c <= str.length - (str.length - i); c++) {
        if (str.length - i <= 1) {
            continue;
        }
        arr.push(str.substr(c, str.length - i));
    }
}

3行目の

for (var i = 0; i < str.length; i++) {

のiの初期値を1にすると最初のabcabcというただの元の文字列が除外できる。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0