Skip to content

Instantly share code, notes, and snippets.

@wovalle
Created May 23, 2020 18:53
Show Gist options
  • Save wovalle/4e84585dc031aea97ca66eb93cd0414d to your computer and use it in GitHub Desktop.
Save wovalle/4e84585dc031aea97ca66eb93cd0414d to your computer and use it in GitHub Desktop.
1337
May Challenge: https://gist.github.com/wovalle/a86a76bc20d8c886342040f54875f291
@wovalle
Copy link
Author

wovalle commented Jun 6, 2020

// https://leetcode.com/problems/letter-combinations-of-a-phone-number/solution/
const letterCombinations = (str) => {
  if (str.trim() === '') return []
  
  const map = {
    1: '',
    2: 'abc',
    3: 'def',
    4: 'ghi',
    5: 'jkl',
    6: 'mno',
    7: 'pqrs',
    8: 'tuv',
    9: 'wxyz',
    0: " ",
    "#": "^",
    "*": "+"
  }
  
  const phoneNumbersArray = str.split('')
  const phoneNumbersMap = phoneNumbersArray.map(n => map[n])
  let result = ['']
  
  for (const position of phoneNumbersMap) {
    let localResult = []
    
    for (const resultCombination of result) {
      for (const currentLetter of position) {
          localResult.push(resultCombination + currentLetter)
      }
    }
    
      
    result = localResult
  }
  
  return result
}

@wovalle
Copy link
Author

wovalle commented Jun 6, 2020

// https://leetcode.com/problems/lru-cache
class LRUCache {
    constructor(capacity) {
        this.capacity = capacity
        this.keys = new Set()
        this.store = new Map()
    }

    get(key) {
        if (this.keys.has(key)) {
            this.keys.delete(key)
            this.keys.add(key)
            return this.store.get(key)
        }
        else {
            return -1
        }

    }

    put(key, value) {
        if (this.get(key) !== -1) {
            this.store.set(key, value)
            return;
        }
        
        if (this.keys.size === this.capacity) {
            const nextKey = this.keys.values().next().value
            this.keys.delete(nextKey)
            this.store.delete(nextKey)
        }
        
        this.keys.add(key)
        this.store.set(key, value)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment