Home > Software design >  I am trying to solve this version of wildcard problem using typescript
I am trying to solve this version of wildcard problem using typescript

Time:12-23

I am trying to solve this version of the wildcard problem using typescript. However, some of the test cases I provided are not passing. this is the summary of how the code work.

A balanced string is one in which every character in the string appears an equal number of times as every other character

For example, "ab", "aaabbb" and "ababaabb" are balanced, but "abb" and "abbaa" are not.

function balanced(s: string): boolean {
  const MAX = 1000;
  let wildcards: number = 0;
  const map: { [key: string]: number } = {};
  let characterCount = 0;
  for (const char of s) {
    if (char === '*') {
      wildcards  ;
    } else {
      if (!map[char]) {
        map[char] = 0;
      }
      map[char]  ;
      if (map[char] > characterCount) {
        characterCount = map[char];
      }
    }
  }
  const mapSize = Object.keys(map).length;
  if (mapSize === MAX && characterCount === 1) {
    return wildcards % MAX === 0;
  }
  if (mapSize < MAX && characterCount === 1 && wildcards <= (MAX - mapSize)) {
    return true;
  }
  if (wildcards === s.length) {
    return true;
  }

  for (const char in map) {
    while (map[char]   1 <= characterCount) {
      map[char]  ;
      wildcards--;
    }
    if (wildcards < 0) return false;
  }
  if (wildcards % characterCount === 0) {
    return true;
  }
  return false;
}

I am testing the code against the below inputs

1: "a" should return true
2: "ab" should return true
3: "abc" should return true
4: "abcb" should return false
5: "Aaa" should return false
6: "abcb*" should return false
7: "abcb**" shoud return true
8: "***********" should return true
9: "" should return true
10: "abd*xdx*yba*" should return true
11: "aabb***" should return false
12: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" should return false
13: "JB**JTIT*****EY" should return false

CodePudding user response:

12: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" should return false

no, it should return true All other tests pass

https://stackblitz.com/edit/vitest-dev-vitest-afgmgj?file=balanced.test.ts&initialPath=__vitest__

CodePudding user response:

I found a similar problem. I just twisted the solution a little bit.

const factorPairs = (n: number) =>
  [...Array(Math.floor(Math.sqrt(n)))].map((_, i) => i   1)
    .flatMap(f => n % f == 0 ? [[f, n / f], [n / f, f]] : []);

const balanced = (s: string): boolean => {
  if (!s) return true;
  const ss = s.split('');
  const chars = [...new Set(ss.filter(s => s !== '*'))];
  const counts = ss.reduce(
    (counts, s) => s === '*' ? counts : ((counts[s]  = 1), counts),
    Object.fromEntries(chars.map(l => [l, 0]))
  );
  const maxCount = Math.max(...Object.values(counts));
  return factorPairs(ss.length).some(
    ([count, letters]) =>
      letters <= 52 &&
      letters >= chars.length &&
      count >= maxCount
  );
};

link to original solution

  • Related