-
Leetcode 395 : Longest Substring with At Least K Repeating Characters (Python 3)
Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times. Approach - We will split our string when we find if a character is less than k, for splitting we use two variables i and j in the last while loop as you can see, we keep a SET of characters whose count is less than k.
Read more… -
Leetcode 207 Course Schedule Soln in Python
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses? Approach - Its a graph question, we simply have to find if there is a cycle in the Graph.
Read more… -
Populating Next Right Pointers in Each Node Leetcode 116 Python
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Approach - Here we simply keep a track of all the nodes in a particular level from left to right, for this purpose we use a hash-map or dictionary in python called level nodes, After creating our hash-map we simply traverse each level which has its value as an array and we point each node to next node in that level.
Read more… -
Lowest Common Ancestor of a Binary Tree Leetcode 236 in Python 3
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself). Approach - Here we will simple keep a track of parent of each node through an array.
Read more… -
Set Matrix Zero, LeetCode 73 Solution in Python 3
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [ [1,1,0], [1,0,1], [1,1,1] ] Output: [ [0,0,0], [0,0,0], [1,0,0] ] Here the approach is very simple. We just have to traverse the matrix and note down the rows and columns indexes which are required to be zero, for that we will use set data structure as it has a constant O(1) search complexity.
Read more… -
Hello World
Hello World, I am Aditya Saini, This is my personal blog where I would be covering topics related to Indian Education, Computer Science, Mathermatics and Programming and I could possibly discuss some of the hot topics that are currently prevailing in India. My English is okay and I will try my best to provide as much knowledge as I have assembled till now thorughout my lifetime. I was born in India and I proud of its rich varied and heritage and I try to be worthy of it.
Read more…