< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Q1: Ransom Note
  • Q2: Contains Duplicate II
  • Q3: Two Sum
  • Q4: Top K Frequent Elements

Was this helpful?

  1. Hash Table

Problems

PreviousST - Linear ProbingNextWhat's Symbol Table and implementation

Last updated 9 months ago

Was this helpful?

Q1:

#include <unordered_map>

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char, int> mp;
        for(int i = 0; i < magazine.size(); i++) {
            if(mp.find(magazine[i]) != mp.end()) mp[magazine[i]]++;
            else mp[magazine[i]] = 1;
        }
        for(int i = 0; i < ransomNote.size(); i++) {
            if(mp.find(ransomNote[i]) != mp.end() && mp[ransomNote[i]] > 0)
                mp[ransomNote[i]]--;
            else
                return false;
        }
        return true;

    }
};
class Solution {
public:
    
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> mp;
        for(int i = 0; i < nums.size(); i++) {
            if(mp.find(nums[i]) != mp.end() && (i+1 - mp[nums[i]]) <= k) return true;
            mp[nums[i]] = i+1;  
        }
        return false;
    }
};
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> mp;
        for(int i = 0; i < nums.size(); i++) {
            for(int j = 0; j < i; j++) {
                if(mp.find(j) != mp.end()  && mp[j] + nums[i] == target)
                    return {i, j};
            }
            mp[i] = nums[i];  
        }
        return {};
    }
};
#include <unordered_map>
class Solution {
public:
    static bool compare(pair<int,int>& a, pair<int,int>& b) {
        return a.first > b.first;
    }
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> mp;
        int count = 0;
        for(int i = 0; i < nums.size(); i++) {
            if(mp.find(nums[i]) != mp.end())
                mp[nums[i]]++;
            else {
                mp[nums[i]] = 1;
                count++;
            }
        }
        
        vector<pair<int, int>> temp(count);
        temp.clear();

        for(auto it = mp.begin(); it != mp.end(); it++) {
            temp.push_back({it->second, it->first});
        }
        
        sort(temp.begin(), temp.end(), compare);
        
        vector<int> res(k);
        res.clear();

        for(int i= 0; i < k; i++) {
            res.push_back(temp[i].second);
        }
        return res;
    }
};

Q2:

Q3:

Q4:

🗓️
Ransom Note
Contains Duplicate II
Two Sum
Top K Frequent Elements