All Posts

Leetcode 817: Linked List Components

You are given the head of a linked list containing unique integer values, and an array nums which is a subset of the linked list values. A connected component is defined as a sequence of consecutive values in the linked list that all appear in nums. Your task is to return the number of such connected components.

Leetcode 820: Short Encoding of Words

Given an array of words, you are to return the length of the shortest reference string that can encode these words. The reference string consists of the words concatenated with a ‘#’ character at the end, and indices are used to encode the words by identifying their position in the reference string. Each word is encoded by the substring in the reference string that starts at its position and ends at the next ‘#’.

Leetcode 822: Card Flipping Game

You are given two arrays, fronts and backs, representing the numbers on the front and back of each card. Each card is initially placed with the front number facing up. You may flip any number of cards. An integer is considered ‘good’ if it appears on the back of some card and is not visible on the front of any card after flipping. Your task is to return the minimum possible ‘good’ integer after flipping the cards. If there is no such ‘good’ integer, return 0.

Leetcode 823: Binary Trees With Factors

Given an array of unique integers, where each integer is greater than 1, we can create a binary tree using these integers. The value of each non-leaf node must be the product of the values of its children. The goal is to return the number of distinct binary trees that can be constructed. Since the result may be large, return the answer modulo 10^9 + 7.

Leetcode 833: Find And Replace in String

You are given a string s and k replacement operations. Each replacement operation consists of an index, a source substring, and a target substring. For each operation, check if the source substring occurs at the given index in s. If it does, replace the substring with the target. All operations are performed simultaneously, meaning they do not affect each other’s indexing. The task is to return the modified string after all operations are applied.

Leetcode 840: Magic Squares In Grid

Given a grid of integers, the task is to find how many 3x3 magic square subgrids are present in the given grid. A 3x3 magic square is a grid that contains distinct numbers from 1 to 9, such that each row, each column, and both diagonals have the same sum. The input grid can contain numbers ranging from 0 to 15, but only numbers between 1 and 9 are valid for forming a magic square.