Home > database >  Javascript find the most repetitive character occurrence from the string
Javascript find the most repetitive character occurrence from the string

Time:09-04

Let's say we have this string:

BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF
  • As you can see, here B is occurring 4 times at first but B is also present before DDDD.

  • Similarly, A is occurring 4 times at the beginning and later 6 times.

I want the expected output if I am searching B it should 4 times as the max occurrence B is 4. However if I am searching A then it should return 6 because the most occurrence for A is 6.

Here is my code I tried:

function checkRepeatativeString(str) {
    let hashMap = {};
    let seen = new Set();
    let counter = 1;
    let maxValue = 1;
    let isPreviousValueSame = false;
    let isNextValueSame = true;

    for (let i = 0; i < str.length; i  ) {
        /**
         * is previous value same
         */
        if (str[i] == str[i-1]) {
            isPreviousValueSame = true;
        }
        /**
         * is next value same
         */
        if (str[i] == str[i 1]) {
            isNextValueSame = true;
        }

        if (seen.has(str[i]) && isPreviousValueSame) {
            hashMap[str[i]][0]  ;
            hashMap[str[i]][1]  ;
            isPreviousValueSame = false;
        } else if(seen.has(str[i]) && !isNextValueSame) {
            maxValue = Math.max(hashMap[str[i]][1], maxValue);
            counter = 0;
            hashMap[str[i]] = [counter, maxValue];
        } else {
            maxValue = Math.max(maxValue, counter);
            seen.add(str[i]);
            hashMap[str[i]] = [counter, maxValue];
            isPreviousValueSame = false;
        }
    }
    return hashMap;
}

let str = "BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF";
console.log(checkRepeatativeString(str));

This code is working but if you look for B, I am getting stuck at the beginning of value.

My program returns out for B:

B: [ 1, 1 ]
     ^  ^

Inside array, 1 is a counter which scans the string and second 1 in array is a max value which should return the output. However my program is returning 1 for B. I am expecting 4 as max value.

Help would be appreciated~

CodePudding user response:

That looks overly complicated. Consider approaching the problem from a different angle - first split up the string into segments of repeating characters, and group them into an object based on the length of the longest substring for a given character.

const checkRepeatativeString = (str) => {
  const longestCounts = {};
  for (const consecutive of (str.match(/(.)\1*/g) || [])) {
    const char = consecutive[0];
    longestCounts[char] = Math.max(
      longestCounts[char] || 0, // Use the existing value in the object if it exists and is higher
      consecutive.length // Otherwise, use the length of the string iterated over
    );
  }
  return longestCounts;
};

let str = "BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF";
console.log(checkRepeatativeString(str));

Simpler code often means less surface area for bugs.

CodePudding user response:

Quick and dirty.

function maxConsecutiveCharacters(check, haystack) {
    if(check.length !== 1) return false;
    let result = 0;
    let buffer = 0;
    for(let i = 0; i < haystack.length; i  ) {
        if(haystack[i] === check) {
            buffer  ;
        }
        else {
            if(buffer > result) {
                result = buffer;
            }
                buffer = 0;
        }
            if(buffer > result) {
                result = buffer;
            }            
    }
    return result;
}
  • Related