Leetcode 383: Ransom Note

Leetcode Problem Statement Description Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise. Each letter in magazine can only be used once in ransomNote. Examples Example 1: Input: ransomNote = "a", magazine = "b" Output: false Example 2: Input: ransomNote = "aa", magazine = "ab" Output: false Example 3: Input: ransomNote = "aa", magazine = "aab" Output: true Constraints 1 <= ransomNote....

January 11, 2023 · 2 min · 384 words · Me

Leetcode 232: Implement Queue Using Stacks

Leetcode Problem Statement Description Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue....

January 11, 2023 · 4 min · 757 words · Me

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