Created
May 19, 2019 12:27
-
-
Save stevencch99/87f9255ae9728d47426fba7c05a96029 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function primeString(s, count = 1) { | |
// 如果字串中有重複的模式,最少也會重複 1 次,故迭代超過字串長度的一半還沒找到就不用找了 | |
if(count > s.length / 2) { return true } | |
// 每次迭代將字串的一小塊,設定為分割陣列的符號 chunk | |
let chunk = s.slice(0, count) | |
// 被 chunk 切過的部分會變成陣列中的空元素,此行判斷是不是能夠將陣列切成重複的小塊 | |
// 如果切完後全部的陣列元素都是空的,代表字串被 chunk 整除,恭喜找到規律。 | |
let grouped = s.split(chunk).every(i => i === '') | |
// 若上一行的結果為否表示沒有找到重複的規律,則呼叫自己,用更大的單位(count + 1)繼續切, | |
// 直到超過字串長度的一半都沒找到,代表輸入為 prime string | |
return grouped ? false : primeString(s, count + 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment