1 minute read

FREQUENCY COUNTERS

ref: https://www.udemy.com/course/best-javascript-data-structures/

๋นˆ๋„์ˆ˜ ์„ธ๊ธฐ ํŒจํ„ด

  • This pattern uses objects or sets to collect values/frequencies of values
  • This can often avoid the need for nested loops or O(N^2) operations with arrays/strings

AN EXAMPLE called same

  • Write a function called same, which accepts two arrays. The function should return true if every value in the array has itโ€™s corresponding value squared in the second array. The frequency of values must be the same.
same([1, 2, 3], [4, 1, 9]); // true
same([1, 2, 3], [1, 9]); // false
same([1, 2, 1], [4, 4, 1]); // false (must be same frequency)

A NAIVE SOLUTION

  • Time Complexity - N^2
function same(arr1, arr2) {
  if (arr1.length !== arr2.length) {
    return false;
  }
  for (let i = 0; i < arr1.length; i++) {
    let correctIndex = arr2.indexOf(arr1[i] ** 2);
    if (correctIndex === -1) {
      return false;
    }
    arr2.splice(correctIndex, 1);
  }
  return true;
}

REFACTORED

  • Time Complexity - O(n)
function same(arr1, arr2) {
  if (arr1.length !== arr2.length) {
    return false;
  }
  let frequencyCounter1 = {};
  let frequencyCounter2 = {};
  for (let val of arr1) {
    frequencyCounter1[val] = (frequencyCounter1[val] || 0) + 1;
  }
  for (let val of arr2) {
    frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1;
  }
  for (let key in frequencyCounter1) {
    if (!(key ** 2 in frequencyCounter2)) {
      return false;
    }
    if (frequencyCounter2[key ** 2] !== frequencyCounter1[key]) {
      return false;
    }
  }
  return true;
}

same([1, 2, 3, 2], [9, 1, 4, 4]);

ANAGRAMS

  • Given two strings, write a function to determine if the second string is an anagram of the first. An anagram is a word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.
function validAnagram(a, b) {
  if (a.length !== b.length) {
    return false;
  }
  let lookup = {};

  for (const letter of a) {
    lookup[letter] = lookup[letter] ? (lookup[letter] += 1) : 1;
  }
  for (const letter of b) {
    if (lookup[letter]) {
      lookup[letter] -= 1;
    } else {
      return false;
    }
  }
  return true;
}

validAnagram("", ""); // true
validAnagram("aaz", "zza"); // false
validAnagram("anagram", "nagaram"); // true
validAnagram("rat", "car"); // false
validAnagram("awesome", "awesom"); // false
validAnagram("qwerty", "qeywrt"); // true
validAnagram("texttwisttime", "timetwisttext"); // true