Leetcode 141: Linked List Cycle

Problem Statement Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter....

January 10, 2023 · 2 min · 243 words · Me

Leetcode 242: Valid Anagram

Problem Statement Given two strings s and t, return true if t is an anagram of s, and false otherwise. 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: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Solution Python Implementation from collections import Counter class Solution: def isAnagram(self, s: str, t: str) -> bool: """ T: O(N) = max(S + T) S: O(N) """ s_mp = Counter(s) t_mp = Counter(t) return s_mp == t_mp Leetcode 242: Valid Anagram

January 10, 2023 · 1 min · 109 words · Me

Leetcode 125: Valid Palindrome

Problem Statement A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Example 1: Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. Example 2: Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome....

January 10, 2023 · 2 min · 217 words · Me

Leetcode 21: Merge Two Sorted Lists

Problem Statement You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example 1: """ 1-->2-->4-->5 ^list1 1-->3-->4 ^list2 1-->1-->2-->3-->4-->4-->5 ^curr """ Input: list1 = [1,2,4,5], list2 = [1,3,4] Output: [1,1,2,3,4,4,5] Example 2: Input: list1 = [], list2 = [] Output: [] Example 3: Input: list1 = [], list2 = [0] Output: [0] Solution Intuition & Patterns In order to keep track of the head of the merged list, we will create a dummy node which will function as a pre-head node....

January 10, 2023 · 2 min · 302 words · Me

Leetcode 20: Valid Parentheses

Problem Statement Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false Solution Intuition & Patterns Leetcode #20, Valid Parentheses, is a great introduction to LIFO (last in first out) stack problems....

January 10, 2023 · 3 min · 438 words · Me