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 Rotation 90.md
Last active January 1, 2024 09:33
This code snippet rotates a square matrix 90 degrees clockwise by reversing the rows and then transposing the matrix in-place.

Matrix Rotation 90

Preview:
def rotate(matrix):
    n = len(matrix)

    # Reverse the rows
    matrix.reverse()

    # Transpose the matrix in-place
@coderRohan123
coderRohan123 / Duplicate Number Checker.md
Created January 1, 2024 09:34
This code checks if a list of numbers contains any duplicates by using a set to keep track of numbers that have been seen before. It returns True if there are duplicates, and False otherwise.

Duplicate Number Checker

Preview:
# The original implementation has a time complexity of O(n), where n is the length of the input list nums.
# This is because each number in nums is checked against the set of seen numbers, which has an average time complexity of O(1) using hash-based set implementation.

# The implementation is already quite efficient, and there is not much room for performance improvement. However, we can make a small modification to potentially improve the average case performance.

# Revised implementation:
def contains_duplicate(nums):
@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):
@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 / 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 / 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 / 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 / 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 / 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 / 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);