Skip to content

Instantly share code, notes, and snippets.

View coderRohan123's full-sized avatar
🎯
Focusing

Rohan Mallick coderRohan123

🎯
Focusing
View GitHub Profile
@coderRohan123
coderRohan123 / Matrix with Frequency of Numbers in a List.md
Created January 2, 2024 16:47
The code snippet takes a list of integers as input and groups them into sublists based on their frequency. It returns a list of sublists where each sublist contains integers with the same frequency.

Matrix with Frequency of Numbers in a List

Preview:
from typing import List


class Solution:
    def findMatrix(self, nums: List[int]) -> List[List[int]]:
        freq = [0] * (len(nums) + 1)
 ans = []
@coderRohan123
coderRohan123 / Frequency-based Integer List Generation.md
Created January 2, 2024 15:37
The code snippet takes an array of integers as input and returns a list of lists. Each inner list contains integers from the input array that have the same frequency in the array. The inner lists are ordered based on the frequency of the integers.

Frequency-based Integer List Generation

Preview:
class Solution {
    public List<List<Integer>> findMatrix(int[] nums) {
        int freq[] = new int[nums.length + 1];
​
        ArrayList<List<Integer>> ans = new ArrayList<>();
        for (int c : nums) {
            if (freq[c] >= ans.size()) {
@coderRohan123
coderRohan123 / Reverse Map using Generics.md
Created January 2, 2024 11:29
The code snippet creates a map with string keys and integer values, adds some key-value pairs to it, and then reverses the map by swapping the keys and values. The reversed map is then printed.

Reverse Map using Generics

Preview:
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("apple", 1);
@coderRohan123
coderRohan123 / Dictionary key with maximum value.md
Created January 2, 2024 11:16
This code snippet defines a function that takes a dictionary as input and returns the key with the maximum value in the dictionary.

Dictionary key with maximum value

Preview:
def max_value_key(my_dict):
    return max(my_dict, key=my_dict.get)
my_dict = {'a': 5, 'b': 9, 'c': 2}
print(max_value_key(my_dict))
)

'''Example:
@coderRohan123
coderRohan123 / Merge Dictionaries.md
Created January 2, 2024 10:19
The code merges two dictionaries (HashMaps) by adding the values of common keys and adding new keys from the second dictionary to the first. The merged dictionary is then printed.

Merge Dictionaries

Preview:
import java.util.HashMap;

public class MergeDictionaries {
    public static HashMap<String, Integer> mergeDictionaries(HashMap<String, Integer> dict1, HashMap<String, Integer> dict2) {
        HashMap<String, Integer> result = new HashMap<>(dict1);

        for (String key : dict2.keySet()) {
@coderRohan123
coderRohan123 / Merge two dictionaries and sum values for common keys..md
Created January 2, 2024 10:15
This code snippet merges two dictionaries by adding the values of common keys. It creates a new dictionary with the merged values and returns it.

Merge two dictionaries and sum values for common keys.

Preview:
def merge_dicts(dict1, dict2):
    result = dict1.copy()
    for key, value in dict2.items():
        result[key] = result.get(key, 0) + value
    return result
    
'''Example:
@coderRohan123
coderRohan123 / Word Frequency Counter.md
Created January 2, 2024 09:10
This code snippet takes a list of words as input and returns a dictionary that counts the frequency of each word in the list.

Word Frequency Counter

Preview:
def count_word_frequency(words):
    word_count = {}
    for word in words:
        word_count[word] = word_count.get(word, 0) + 1
    return word_count
words=["apple","banana","apple","orange","banana","apple","pine"]
print(count_word_frequency(words))
@coderRohan123
coderRohan123 / String Rotation Checker.md
Created January 1, 2024 16:19
This code checks if string A can be rotated to match string B by iterating through all possible rotations of A and comparing each rotation to B.

String Rotation Checker

Preview:
class Solution(object):
    def rotateString(self, A, B):
        if len(A) != len(B):
            return False
        if len(A) == 0:
            return True
@coderRohan123
coderRohan123 / String Rotation Checker.md
Created January 1, 2024 16:18
The code checks if string B can be obtained by rotating string A. It returns true if the lengths of A and B are equal and B is a substring of A concatenated with itself.

String Rotation Checker

Preview:
class Solution {
    public boolean rotateString(String A, String B) {
        return A.length() == B.length() && (A + A).contains(B);
    }
}

| Associated Context | |

@coderRohan123
coderRohan123 / Find Content Children.md
Created January 1, 2024 14:33
This code snippet sorts two lists, g and s, and then iterates through the sorted lists to find the maximum number of content children that can be satisfied with the given cookies. It returns the number of content children.

Find Content Children

Preview:
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        content_children = 0
        cookie_index = 0
        while cookie_index < len(s) and content_children < len(g):