Monthly Archives: October 2021

C# || How To Flatten A Multilevel Doubly Linked List Using C#

The following is a module with functions which demonstrates how to flatten a doubly linked list using C#.


1. Flatten – Problem Statement

You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.

Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.

Example 1:


Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
Explanation:

The multilevel linked list in the input is as follows:

Example 1

After flattening the multilevel linked list it becomes:

Example 1

Example 2:


Input: head = [1,2,null,3]
Output: [1,3,2]
Explanation:

The input multilevel linked list is as follows:

1---2---NULL
|
3---NULL

Example 3:


Input: head = []
Output: []


2. Flatten – Solution

The following are two solutions which demonstrates how to flatten a doubly linked list.

Iterative

Recursive

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[1,2,3,7,8,11,12,9,10,4,5,6]
[1,3,2]
[]

C# || How To Traverse Binary Tree Zigzag Level Order Using C#

The following is a module with functions which demonstrates how to traverse binary tree zigzag level order using C#.


1. Zigzag Level Order – Problem Statement

Given the root of a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e., from left to right, then right to left for the next level and alternate between).

Example 1:

Example 1


Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]

Example 2:


Input: root = [1]
Output: [[1]]

Example 3:


Input: root = []
Output: []


2. Zigzag Level Order – Solution

The following is a solution which demonstrates how to traverse binary tree zigzag level order.

This solution uses Breadth First Search to explore items at each level.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[[3],[20,9],[15,7]]
[[1]]
[]

C# || How To Find Longest Duplicate Substring Using C#

The following is a module with functions which demonstrates how to find the longest duplicate substring using C#.


1. Longest Dup Substring – Problem Statement

Given a string s, consider all duplicated substrings: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap.

Return any duplicated substring that has the longest possible length. If s does not have a duplicated substring, the answer is “”.

Example 1:


Input: s = "banana"
Output: "ana"

Example 2:


Input: s = "abcd"
Output: ""


2. Longest Dup Substring – Solution

The following is a solution which demonstrates how find the longest duplicate substring.

This solution uses Binary Search and Rolling Hash.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


"ana"
""

C# || Group Anagrams – How To Group Array Of Anagrams Using C#

The following is a module with functions which demonstrates how to group an array of anagrams using C#.


1. Group Anagrams – Problem Statement

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:


Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Example 2:


Input: strs = [""]
Output: [[""]]

Example 3:


Input: strs = ["a"]
Output: [["a"]]


2. Group Anagrams – Solution

The following is a solution which demonstrates how to group an array of anagrams.

The idea of this solution is to generate a simple hash key for each anagram.

The hash key is made up consisting of a digit, and a letter. These two values represents the character count, and the letter found in the given string.

For example, given the input ["eat","tea","tan","ate","nat","bat"] we create a hash for each anagram as follows:


anagram: eat, hash: 1a1e1t
anagram: tea, hash: 1a1e1t
anagram: tan, hash: 1a1n1t
anagram: ate, hash: 1a1e1t
anagram: nat, hash: 1a1n1t
anagram: bat, hash: 1a1b1t

The hash key is generated similar to the counting sort technique. We use this hash to group the anagrams together.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[["eat","tea","ate"],["tan","nat"],["bat"]]
[[""]]
[["a"]]

C# || How To Traverse Binary Tree Level Order Using C#

The following is a module with functions which demonstrates how to traverse binary tree level order using C#.


1. Level Order – Problem Statement

Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).

Example 1:

Example 1


Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]

Example 2:


Input: root = [1]
Output: [[1]]

Example 3:


Input: root = []
Output: []


2. Level Order – Solution

The following is a solution which demonstrates how to traverse binary tree level order.

This solution uses Breadth First Search to explore items at each level.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[[3],[9,20],[15,7]]
[[1]]
[]

C# || How To Determine When A Fresh Orange Becomes Rotten Using C#

The following is a module with functions which demonstrates how to determine when a fresh orange becomes rotten using C#.


1. Oranges Rotting – Problem Statement

You are given an m x n grid where each cell can have one of three values:

  • 0 representing an empty cell,
  • 1 representing a fresh orange, or
  • 2 representing a rotten orange.

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

Example 1:

Example 1


Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4

Example 2:


Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.

Example 3:


Input: grid = [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.


2. Oranges Rotting – Solution

The following is a solution which demonstrates how to determine when a fresh orange becomes rotten.

This solution uses Breadth First Search when looking for fresh cells to turn rotten.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


4
-1
0

C# || 3Sum Closest – How To Get 3 Numbers In Array Closest Equal To Target Value Using C#

The following is a module with functions which demonstrates how to get 3 numbers in an array closest equal to target value using C#.


1. 3 Sum Closest – Problem Statement

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

Return the sum of the three integers.

You may assume that each input would have exactly one solution.

Example 1:


Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Example 2:


Input: nums = [0,0,0], target = 1
Output: 0


2. 3 Sum Closest – Solution

The following is a solution which demonstrates how to get 3 numbers in an array closest equal to target value.

This solution uses the two pointer technique to find combinations.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


2
0

C# || 3Sum – How To Get All Triplet Combinations In Array Equal To Target Value Using C#

The following is a module with functions which demonstrates how to get all triplet combinations in an array equal to a target value using C#.


1. 3 Sum – Problem Statement

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

Example 1:


Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]

Example 2:


Input: nums = []
Output: []

Example 3:


Input: nums = [0]
Output: []


2. 3 Sum – Solution

The following is a solution which demonstrates how to get all triplet combinations in an array equal to a target value.

This solution uses the two pointer technique to find combinations.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[[-1,-1,2],[-1,0,1]]
[]
[]

C# || Two Sum II – How To Get Two Numbers In Sorted Array Equal To Target Value Using C#

The following is a module with functions which demonstrates how to get two numbers in a sorted array equal to target value using C#.


1. Two Sum II – Problem Statement

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.

Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.

The tests are generated such that there is exactly one solution. You may not use the same element twice.

Example 1:


Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].

Example 2:


Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].

Example 3:


Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].


2. Two Sum II – Solution

The following is a solution which demonstrates how to get two numbers in sorted array equal to target value.

In this solution, Binary Search is used to find the two numbers.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[1,2]
[1,3]
[1,2]

C# || Two Sum – How To Get Two Numbers In Array Equal To Target Value Using C#

The following is a module with functions which demonstrates how to get two numbers in array equal to target value using C#.


1. Two Sum – Problem Statement

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:


Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:


Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:


Input: nums = [3,3], target = 6
Output: [0,1]


2. Two Sum – Solution

The following is a solution which demonstrates how to get two numbers in array equal to target value.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[0,1]
[1,2]
[0,1]

C# || How To Generate Unique Subsets From Array With Duplicate Values Using C#

The following is a module with functions which demonstrates how to generate unique subsets from an array with duplicate values using C#.


1. Subsets With Dup – Problem Statement

Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:


Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]

Example 2:


Input: nums = [0]
Output: [[],[0]]


2. Subsets With Dup – Solution

The following is a solution which demonstrates how to generate unique subsets from an array with duplicate values.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[[],[1],[1,2],[1,2,2],[2],[2,2]]
[[],[0]]

C# || How To Generate Subsets From Array With Distinct Values Using C#

The following is a module with functions which demonstrates how to generate subsets from an array with distinct values using C#.


1. Subsets – Problem Statement

Given an integer array nums of unique elements, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:


Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

Example 2:


Input: nums = [0]
Output: [[],[0]]


2. Subsets – Solution

The following is a solution which demonstrates how to generate subsets from an array with distinct values.

This solution uses bit manipulation to generate 2^n possibilities based of the array length, and then adds items to the result list if the array index is a valid bit based off of the subset possibilities.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
[[],[0]]

C# || How To Generate Next Permutation From Array Using C#

The following is a module with functions which demonstrates how to generate the next permutation from an array using C#.


1. Next Permutation – Problem Statement

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).

The replacement must be in place and use only constant extra memory.

Example 1:


Input: nums = [1,2,3]
Output: [1,3,2]

Example 2:


Input: nums = [3,2,1]
Output: [1,2,3]

Example 3:


Input: nums = [1,1,5]
Output: [1,5,1]

Example 4:


Input: nums = [1]
Output: [1]


2. Next Permutation – Solution

The following is a solution which demonstrates how to generate the next permutation from an array

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[1,3,2]
[1,2,3]
[1,5,1]
[1]

C# || How To Generate Unique Permutations From Array With Duplicate Values Using C#

The following is a module with functions which demonstrates how to generate unique permutations from an array with duplicate values using C#.


1. Permute Unique – Problem Statement

Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.

Example 1:


Input: nums = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]

Example 2:


Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]


2. Permute Unique – Solution

The following is a solution which demonstrates how to generate unique permutations from an array with duplicate values.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[[1,1,2],[1,2,1],[2,1,1]]
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]