AndyHu
AndyHu

Reputation: 1418

Find the greatest number of repeated letter in a word in Javascript

How can I use regex to find the greatest number of repeated letter in a word in JavaScript? The repeated letter could be separated with other letters like "memo":'m' being separated with 'e'. For example: the word "greatest" should return 2 cause either 'e' or 't' repeated two times and two is maximum repeated times. I've tried pattern like /([a-z]{1,})\1/ but it's not working with this case. Thank you in advance.

Upvotes: 2

Views: 2404

Answers (3)

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

Here is another way to do this with JavaScript:

function greatestRepeat(str) {
    var maxRepeat = 0;
    var letterRepeated = null;
    for(var i=0; i<str.length; i++) {
    	var currentLetter = str.charAt(i);
        var timesRepeated = str.split(currentLetter).length-1;
        if(timesRepeated > maxRepeat) {
            maxRepeat = timesRepeated;
            letterRepeated = currentLetter;
        }
    }
    return letterRepeated;
}
console.log(greatestRepeat("memo"));

As I mentioned in the comments, this cannot be done purely with regular expressions. As @MillieSmith stated this is because regular languages are context free.

Upvotes: 2

user663031
user663031

Reputation:

How can I use regex to find the greatest number of repeated letter in a word in JavaScript?

You cannot, unless you are willing to restrict the maximum number of repetitions, in which case you can do it with the following:

regexp = /(?:.*(.)(?:.*\1){2})|(?:.*(.)(?:.*\2){1})/
          ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
          MATCH 2 REPETITIONS  MATCH ONE REPETITION

This is for the case of a maximum of two repetitions (three occurrences of the same character). What this does is find the first match for a sequence of a particular character ((.)), followed by anything and that character again (.*\1), repeated twice; in other words, the character occurs three times. If that does not match, then it finds the first match for a sequence of a particular character followed by anything and that character again, repeated once; in other words, the character occurs twice. You can extend this up to your preferred maximum number of allowed repetitions (9, since AFAIK back references only support single digits).

If you use this as in

match = 'aabbbcc'.match(regexp)

then the correct answer ("b") will be in match[1]. If there are no repetitions, the call to match will fail.

Upvotes: 3

Max
Max

Reputation: 2816

Here is the javascript way of accomplishing this. Doing this with regular expressions is not possible.

var exp = 'hello world' ;
var expCounts = {};
var maxKey = '';
for(var i = 0; i < exp.length; i++) {
    var key = exp[i];
    if(!expCounts[key]){
     expCounts[key] = 0;
    }
    expCounts[key]++;
    if(maxKey == '' || expCounts[key] > expCounts[maxKey]){
        maxKey = key;
    }
}

console.log(maxKey + ":" + expCounts[maxKey]);    //logs l:3

JSFiddle

https://jsfiddle.net/v72qa872/

Upvotes: 3

Related Questions