How to determine if a string contains a sequence of repeated letters

前端 未结 7 1925
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 16:01

Using JavaScript, I need to check if a given string contains a sequence of repeated letters, like this:

\"aaaaa\"

How can I do t

7条回答
  •  孤街浪徒
    2021-01-17 16:07

    function check(str) {
        var tmp = {};
        for(var i = str.length-1; i >= 0; i--) {
            var c = str.charAt(i);
            if(c in tmp) {
                tmp[c] += 1;
            }
            else {
                tmp[c] = 1;
            }
        }
        var result = {};
        for(c in tmp) {
            if(tmp.hasOwnProperty(c)) {
                if(tmp[c] > 1){
                    result[c] = tmp[c];
                }
            }
        }
        return result;
    }
    

    then you can check the result to get the repeated chars and their frequency. if result is empty, there is no repeating there.

提交回复
热议问题