title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
204: Solution with step by step explanation | count-primes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can use the Sieve of Eratosthenes algorithm to count the number of prime numbers less than n. The basic idea behind this algorithm is to generate a list of numbers from 2 to n-1, then mark all the multiples of 2 as not pr... | 18 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
204: Solution with step by step explanation | count-primes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can use the Sieve of Eratosthenes algorithm to count the number of prime numbers less than n. The basic idea behind this algorithm is to generate a list of numbers from 2 to n-1, then mark all the multiples of 2 as not pr... | 18 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
[Python3] Simple Code; How to Make Your Code Faster. | count-primes | 0 | 1 | # Algorithm:\nMy code is based on the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) which is efficient yet very simple. Make sure to read the link as well as the hints in the description of this question to better understand the Mathmatics of the code. Here, I do not want to explain the a... | 77 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
[Python3] Simple Code; How to Make Your Code Faster. | count-primes | 0 | 1 | # Algorithm:\nMy code is based on the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) which is efficient yet very simple. Make sure to read the link as well as the hints in the description of this question to better understand the Mathmatics of the code. Here, I do not want to explain the a... | 77 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Explanation of why sqrt(n), i*i and 2*i | count-primes | 0 | 1 | ### Explanation\n```\nSuppose n = 26.\n1. Now Create a list/array/vector Sieve of n numbers and mark them all True.\n\n T T T T T T T T T T T T T T T T T T T T T T T T T T T\n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26\n ... | 2 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Explanation of why sqrt(n), i*i and 2*i | count-primes | 0 | 1 | ### Explanation\n```\nSuppose n = 26.\n1. Now Create a list/array/vector Sieve of n numbers and mark them all True.\n\n T T T T T T T T T T T T T T T T T T T T T T T T T T T\n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26\n ... | 2 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Very easy solution using Sieve of Eratosthenes in Python | count-primes | 0 | 1 | \n\n\n```\nclass Solution:\n def countPrimes(self, n: int) -> int:\n\n arr = [True] * n\n\n if n == 0 or n == 1:\n return 0\n\n arr[0], arr[1] = False, False\n\n for i ... | 12 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Very easy solution using Sieve of Eratosthenes in Python | count-primes | 0 | 1 | \n\n\n```\nclass Solution:\n def countPrimes(self, n: int) -> int:\n\n arr = [True] * n\n\n if n == 0 or n == 1:\n return 0\n\n arr[0], arr[1] = False, False\n\n for i ... | 12 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Count Primes for Kids! ¯\_( ͡❛ ͜ʖ ͡❛)_/¯ | count-primes | 0 | 1 | \n Starting from first prime, every additional multiple of this prime is changed to False.\n\t \n First Prime : 2 -> remains prime\n 4 , 6, 8, 10, 12, 14, 16, 18, 20, ... < n\n \n\t We continue to do this for each prime.\n \n 3 -> remai... | 39 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Count Primes for Kids! ¯\_( ͡❛ ͜ʖ ͡❛)_/¯ | count-primes | 0 | 1 | \n Starting from first prime, every additional multiple of this prime is changed to False.\n\t \n First Prime : 2 -> remains prime\n 4 , 6, 8, 10, 12, 14, 16, 18, 20, ... < n\n \n\t We continue to do this for each prime.\n \n 3 -> remai... | 39 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
python3 | two solution |99.47% faster and 65% faster | count-primes | 0 | 1 | * **99.47% faster**\n```\nfrom numpy import ones, bool\n\nclass Solution:\n def countPrimes(self, n: int) -> int:\n primes = ones(n, dtype=bool)\n primes[:2] = 0\n primes[4::2] = 0\n \n\n # Prime Sieve\n for i in range(3,ceil(sqrt(n)),2):\n if primes[i]:\n ... | 8 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
python3 | two solution |99.47% faster and 65% faster | count-primes | 0 | 1 | * **99.47% faster**\n```\nfrom numpy import ones, bool\n\nclass Solution:\n def countPrimes(self, n: int) -> int:\n primes = ones(n, dtype=bool)\n primes[:2] = 0\n primes[4::2] = 0\n \n\n # Prime Sieve\n for i in range(3,ceil(sqrt(n)),2):\n if primes[i]:\n ... | 8 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
[Python] Algorithm explained with comments | count-primes | 0 | 1 | ```\nclass Solution:\n def countPrimes(self, n: int) -> int:\n ## RC ##\n ## APPROACH : Sieve of Eratosthenes ##\n \n # 1. Checking till sqrt(n) is enough for prime numbers i.e i*i < n\n # 2. mark all as prime.\n # 3. as you move along (i to i*i<n) mark every multiple... | 17 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
[Python] Algorithm explained with comments | count-primes | 0 | 1 | ```\nclass Solution:\n def countPrimes(self, n: int) -> int:\n ## RC ##\n ## APPROACH : Sieve of Eratosthenes ##\n \n # 1. Checking till sqrt(n) is enough for prime numbers i.e i*i < n\n # 2. mark all as prime.\n # 3. as you move along (i to i*i<n) mark every multiple... | 17 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Very Easy || 100% || Fully Explained || Java, C++, Python, Javascript, Python3 (Using HashMap) | isomorphic-strings | 1 | 1 | **Two strings s and t are isomorphic if the characters in s can be replaced to get t.**\n-----------------------------------------------------------------------------------------------------\n--------------------------------------------------------------------------------------------------------------------------------... | 317 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
Python Easy | isomorphic-strings | 0 | 1 | ```\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n d = {}\n \n seen = set()\n for i, j in zip(s, t):\n if i in d:\n if d[i] != j:\n return False\n\n \n else:\n if j in seen:\n ... | 1 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
One Line of Code and Using Hashtable Python | isomorphic-strings | 0 | 1 | # One Line of Code Python Solution\n```\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n return len(set(s))==len(set(t))==len(set(zip(s,t)))\n```\n# please upvote me it would encourage me alot\n\n```\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n dic1,dic2={... | 69 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
Convert symbol to incremental integer with trailing space | isomorphic-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
Simple Python 3 Solution || Runtime beats 91% || 🤖🧑💻💻 | isomorphic-strings | 0 | 1 | If you guys have better solution please comment the answer || and please UPVOTE \uD83D\uDE4C\n\n# Code\n```\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n n = len(s)\n mp = {}\n seen = set()\n for i in range(n):\n if s[i] not in mp:\n if t[i... | 3 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
2 liner with 99% efficiency and explanation. | isomorphic-strings | 0 | 1 | Runtime: 32 ms, faster than 99.09% of Python3 online submissions for Isomorphic Strings.\nMemory Usage: 14.2 MB, less than 96.77% of Python3 online submissions for Isomorphic Strings\n```\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n zipped_set = set(zip(s, t))\n return len(zippe... | 120 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
205: Solution with step by step explanation | isomorphic-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution uses two hash maps to keep track of the mapping of characters from string s to string t and vice versa. It iterates over the characters in the strings, and if a character is not already in either hash map, it a... | 13 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
Python | 1 line | beats 99% | isomorphic-strings | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n return [s.index(c) for c in s] == [t.index(c) for c in t]\n``` | 1 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
Python solution using hash function to compute an index with a key into an array of slots | isomorphic-strings | 0 | 1 | ```\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n if len(s)!=len(t) or len(set(s))!=len(set(t)):\n return False\n hashmap={}\n \n for i in range(len(s)):\n if s[i] not in hashmap:\n hashmap[s[i]]=t[i]\n \n i... | 3 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
[Python] Easy Approach ✔ | isomorphic-strings | 0 | 1 | \tclass Solution:\n\t\tdef isIsomorphic(self, s: str, t: str) -> bool:\n\t\t\tif len(set(s)) != len(set(t)):\n\t\t\t\treturn False\n\t\t\thash_map = {}\n\t\t\tfor char in range(len(t)):\n\t\t\t\tif t[char] not in hash_map:\n\t\t\t\t\thash_map[t[char]] = s[char]\n\t\t\t\telif hash_map[t[char]] != s[char]:\n\t\t\t\t\tret... | 22 | Given two strings `s` and `t`, _determine if they are isomorphic_.
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same characte... | null |
[VIDEO] Step-by-Step Visualization of O(n) Solution | reverse-linked-list | 0 | 1 | https://youtu.be/VtC4GUR31wQ\n\nWe\'ll be using three pointers:\n`new_list`: always points to the head of the new reversed linked list\n\n`current`: traverses the linked list and reverses each node\'s `next` pointer (by pointing it to `new_list`)\n\n`next_node`: keeps track of the next node in the original linked list ... | 5 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
Easy || 0 ms || 100% || Fully Explained || Java, C++, Python, JS, C, Python3 (Recursive & Iterative) | reverse-linked-list | 1 | 1 | # **Java Solution (Recursive Approach):**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Reverse Linked List.\n```\nclass Solution {\n public ListNode reverseList(ListNode head) {\n // Special case...\n if (head == null || head.next == null) return head;\n // Create a new nod... | 327 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
✅Best Method 🔥 100% || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥 | reverse-linked-list | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe given code snippet implements the iterative approach to reverse a linked list. The intuition behind it is to use three pointers: `prev`, `head`, and `nxt`. By reversing the pointers between the nodes, we can reverse the linked list.\n... | 39 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
O( n )✅ | Recursive approach✅ | Python (Step by step explanation)✅ | reverse-linked-list | 0 | 1 | # Intuition\nThe code aims to reverse a singly-linked list. It utilizes a recursive approach to achieve this.\n\n# Approach\nThe approach used in the code is a recursive algorithm for reversing a singly-linked list. The code defines a function `reverseList` that takes the head of the linked list as input. It proceeds a... | 9 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
eas | reverse-linked-list | 0 | 1 | just read, reverse and create link list\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseList(self, ll: Optional[ListNode]) -> Optional[ListNode]:\n def CreateList(args... | 1 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
python3 , can i improve? | reverse-linked-list | 0 | 1 | I appreciate any helpful comments!\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOf course, the simplest approach is to just a simple process.\nConvert, reverse, convert, and I\'m done, right?\nYes, but slowly.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->... | 1 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
Conversion of linked list to list using Python | reverse-linked-list | 0 | 1 | # Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n lst=[]\n while head:\n lst.appen... | 2 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
Python Constant space solution | reverse-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is track the previous node and keep changing the pointer of current node to previous in iterative way.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your t... | 2 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
3 pointer approch faster than 99% solutions. | reverse-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[5,4,3,2,1\]
**Example 2:**
**Input:** head = \[1,2\]
**Output:** \[2,1\]
**Example 3:**
**Input:** head = \[\]
**Output:** \[\]
**Constraints:**
* The number... | null |
Click this if you're confused. | course-schedule | 0 | 1 | # Intuition\nIf we model the course prereqs as a directed graph where each vertex is a course and each edge A -> B means A requires the completion of B, then we can solve this by detecting a cycle in the graph. If we find a cycle, it means that a course A must be completed to complete A\'s prerequisites\u2014impossible... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Most Unique || Verly Simple | course-schedule | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
EASY PYTHON SOLUTION || DIRECTED GRAPH || AND OPERATION | course-schedule | 0 | 1 | \n# Code\n```\nclass Solution:\n def func(self,node,graph,visited):\n visited[node]=0\n ans=1\n for i in graph[node]:\n if visited[i]==-1:\n ans&=self.func(i,graph,visited)\n else:\n ans&=visited[i]\n if ans==1:\n visited[node... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Python3 Solution | course-schedule | 0 | 1 | \n```\nfrom collections import defaultdict,deque\nclass Solution:\n def canFinish(self,numCourses:int,prerequisites:List[List[int]])->bool:\n graph=defaultdict(list)\n for course,prereq in prerequisites:\n graph[prereq].append(course)\n\n\n indegrees=[0]*numCourses\n for prereq... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
✅Beat's 100% || TOPO || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥 | course-schedule | 1 | 1 | # Intuition:\nThe intuition behind this approach is that if there is a cycle in the graph, there will be at least one node that cannot be visited since it will always have a nonzero indegree. On the other hand, if there are no cycles, all the nodes can be visited by starting from the nodes with no incoming edges and re... | 125 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
[VIDEO] Visualization of Kahn's Algorithm to Detect a Cycle | course-schedule | 0 | 1 | https://youtu.be/EUDwWbvtB_Q?si=XJw7OI9vrkDti9Iu\n\nThe fact that the prerequisites are given in pairs is a hint that we can express this problem as a graph. A directed edge between node `u` and node `v` indicates that course `u` is a prerequisites of course `v`. The only scenario where we wouldn\'t be able to comple... | 9 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Beating 98.18% Python Easy Solution | course-schedule | 0 | 1 | \n\n\n# Code\n```\nclass Solution:\n def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:\n # Topological Sort\n alist = collections.defaultdict(list)\n orde... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Python 99% time and 100% space. Collection of solutions with explanation | course-schedule | 0 | 1 | Firstly, we need to finish prerequisites courses before taking a main course so this is a directed graph problem. In other words, this problem can be restated to **Detect cycle in a directed graph** or similarly **Check if a graph is acyclic**. (For undirected graphs, check out this [article](https://leetcode.com/probl... | 367 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Python 🔥Java 🔥C++ 🔥Simple Solution 🔥Easy to Understand | course-schedule | 1 | 1 | # An Upvote will be encouraging \uD83D\uDC4D\n\n# Video Solution\n\n# Search \uD83D\uDC49 `Course Schedule By Tech Wired`\n\n# OR\n\n# Click the link in my profile\n\n\n\n- To solve the problem, we use a depth-first search (DFS) algorithm to traverse the graph and detect cycles. During the DFS traversal, we keep track ... | 3 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
[Python 3] Topological Sort | course-schedule | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Course Schedule | Topological Sort | Beats 95.3% | Memory 43MB | course-schedule | 1 | 1 | \n\n# Complexity\n- Time complexity: O(V+E)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(V+E)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public boolean canFinish(int numCourses, int[][] prerequisites) {\n int n = prerequisites.... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
207: Solution with step by step explanation | course-schedule | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem is essentially checking if there is a cycle in a directed graph. Each course can be represented as a node, and each prerequisite relationship can be represented as a directed edge from the prerequisite course to... | 23 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Simple python topsort | course-schedule | 0 | 1 | ## Intuition\n\nThe problem can be viewed as finding whether a Directed Acyclic Graph (DAG) exists from the given prerequisites. If there\'s a cycle in the graph, then it\'s impossible to finish all courses; otherwise, it\'s possible. The idea is to use the topological sort algorithm to traverse through the graph.\n\n#... | 2 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Ex-Amazon explains a Python solution with a video! | course-schedule | 0 | 1 | # Video solution\n\nhttps://youtu.be/-Me_If-_jRs\n\n\u25A0 Please subscribe to my channel from here. I have more than 200 Leetcode videos.\nhttps://www.youtube.com/@KeetCodeExAmazon\n\n---\n\n# Approach\n\n1. Create a class named `Solution` (assuming it is part of a larger program).\n2. Define a method within the `Solu... | 5 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Awesome DFS Python3 | course-schedule | 0 | 1 | # DFS: Python3\n```\nclass Solution:\n def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:\n dic={i:[] for i in range(numCourses)}\n for crs,pre in prerequisites:\n dic[crs].append(pre)\n visit=set()\n def dfs(crs):\n if crs in visit:\n ... | 5 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Simple solution using Topological Sort (Kahn's algorithm) , in Python | course-schedule | 0 | 1 | # Code\n```\nfrom queue import Queue\n\nclass Solution:\n def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:\n # TOPOLOGICAL SORT ... KAHN\'S ALGORITHM\n # by ensuring the given .. is a Directed Acyclic Graph(DAG) or not we can find the answer\n\n graph = {}\n f... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topologica... |
Solution | implement-trie-prefix-tree | 1 | 1 | ```C++ []\nclass TrieNode {\npublic:\n TrieNode *child[26];\n bool isWord;\n TrieNode() {\n isWord = false;\n for (auto &a : child) a = nullptr;\n }\n};\nclass Trie {\n TrieNode* root;\npublic:\n Trie() {\n root = new TrieNode();\n }\n void insert(string s) {\n TrieNo... | 491 | A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try ") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
* `Trie()` ... | null |
Simple Trie Implementation using only standard dictionaries | implement-trie-prefix-tree | 0 | 1 | # Intuition\nSimple Trie implementation using only dictionary of dictionaries.\n\n# Code\n```\nclass Trie:\n\n def __init__(self):\n self.nodes = dict()\n\n def insert(self, word: str) -> None:\n node = self.nodes\n for s in word:\n node[s] = node.get(s, dict())\n node =... | 2 | A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try ") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
* `Trie()` ... | null |
Short python solution | implement-trie-prefix-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try ") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
* `Trie()` ... | null |
Day 76 || Easiest Beginner Friendly Sol | implement-trie-prefix-tree | 1 | 1 | **NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Intuition of this Problem :\n (pronounced as "try ") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
* `Trie()` ... | null |
🔥Trie Basics Explained + Video | Java C++ Python | implement-trie-prefix-tree | 1 | 1 | # What\'s a Trie\nJust like in a binary tree, instead of having 2 children, trie has 26 children. The idea is each child corresponds to each letter in alphabet, eg child1 --> a child2 -->b \nInstead of individual 26 children we store them in an array. \n (pronounced as "try ") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
* `Trie()` ... | null |
Easiest understandable code u will ever find #python3 | implement-trie-prefix-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try ") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
* `Trie()` ... | null |
Python - Simple TrieNode and Trie Implementation | implement-trie-prefix-tree | 0 | 1 | ```\nclass TrieNode:\n def __init__(self):\n # Stores children nodes and whether node is the end of a word\n self.children = {}\n self.isEnd = False\n\nclass Trie:\n def __init__(self):\n self.root = TrieNode()\n\n def insert(self, word: str) -> None:\n cur = self.root\n ... | 39 | A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try ") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
* `Trie()` ... | null |
Python3 Easy Sliding Window Solution | minimum-size-subarray-sum | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def minSubArrayLen(self, target: int, nums: List[int]) -> int:\n\n l, r = 0, 0\n rsum = 0\n minlength = float(\'inf\')\n\n for r in range(len(nums)):\n\n rsum += nums[r]\n\n while rsum >= target:\n minlength = min(min... | 1 | Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a_ _subarray_ _whose sum is greater than or equal to_ `target`. If there is no such subarray, return `0` instead.
**Example 1:**
**Input:** target = 7, nums = \[2,3,1,2,4,3\]
**Output:** 2
**Explanation:** Th... | null |
Python || Sliding Window || Beats 99% || Easy & Understandable | minimum-size-subarray-sum | 0 | 1 | # Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minSubArrayLen(self, target: int, nums: List[int]) -> int:\n start = 0 # initializing window ... | 2 | Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a_ _subarray_ _whose sum is greater than or equal to_ `target`. If there is no such subarray, return `0` instead.
**Example 1:**
**Input:** target = 7, nums = \[2,3,1,2,4,3\]
**Output:** 2
**Explanation:** Th... | null |
Ordinary Solution | minimum-size-subarray-sum | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$.\n\n- Space complexity: $$O(1)$$.\n\n# Code\n```\nclass Solution:\n def minSubArrayLen(self, target: int, nums: List[int]) -> int:\n res, curSum, l = len(nums)+1, 0, 0\n \n for r, n in enumerate(nums):\n curSum += n\n while curSum >... | 5 | Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a_ _subarray_ _whose sum is greater than or equal to_ `target`. If there is no such subarray, return `0` instead.
**Example 1:**
**Input:** target = 7, nums = \[2,3,1,2,4,3\]
**Output:** 2
**Explanation:** Th... | null |
Simple Solution | Beginner friendly| Easy to Understand| cJava |Python 3 | C++ | Koylin | Javascript | minimum-size-subarray-sum | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUses a two-pointer approach to find the minimum subarray length whose sum is greater than or equal to the target value. The two pointers, l and r, represent the left and right boundaries of the subarray, respectively.\n\n# Approach\n<!-- ... | 10 | Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a_ _subarray_ _whose sum is greater than or equal to_ `target`. If there is no such subarray, return `0` instead.
**Example 1:**
**Input:** target = 7, nums = \[2,3,1,2,4,3\]
**Output:** 2
**Explanation:** Th... | null |
Python 3 || 9 lines, sliding window || T/M: 100% / 100% | minimum-size-subarray-sum | 0 | 1 | Here\'s how the code works:\n\n1. We check an edge case. If the `sum(nums)` is less than `target`, then there is no subarray that satisfies the condition, so we return 0.\n\n2. We initialize three variables: `s` for keeping track of the current sum, `l` as the left index of the sliding window, and `ans` to update the m... | 12 | Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a_ _subarray_ _whose sum is greater than or equal to_ `target`. If there is no such subarray, return `0` instead.
**Example 1:**
**Input:** target = 7, nums = \[2,3,1,2,4,3\]
**Output:** 2
**Explanation:** Th... | null |
Simple intuition. | course-schedule-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe simply perform recursive DFS for each course to determine whether it\'s completable. Our graph consists of courses for nodes and edges from a course to its prerequisites. The DFS performs cycle detection by adding courses we\'re curren... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Cycle Detection with Topological Sort (Python 3) | course-schedule-ii | 0 | 1 | # Code\n```\nclass Solution:\n def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:\n adjList = [[] for _ in range(numCourses)]\n visited = [0] * numCourses # 0: unprocessed, 1: processing, 2: processed\n courses = []\n\n # build adjacency list\n for p... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
✅ PYTHON || Beginner Friendly || O(V+E) 🔥🔥🔥 | course-schedule-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem is asking for the order in which courses can be taken given a list of prerequisites. \n- We can model the problem as a directed graph, where nodes represent courses and directed edges represent prerequisites.\n- The goal is ... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Python3 Topological Sort solution intuition and imagination of solution 96.9% faster | course-schedule-ii | 0 | 1 | # Intuition\nHow to proceed with the Question is to think for yourself, where do I start with my first course?\n \nAnalogy will come in imagination to a graph problem itself as direct the node starting from which course needs to be completed first to get to the other course.\n\nFor example: prerequisites = [[1,0],[2,0]... | 2 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
210: Solution with step by step explanation | course-schedule-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem can be solved using topological sort. First, we need to build a graph to represent the courses and their prerequisites, and then perform topological sort on the graph to get the order of courses.\n\nHere is the ... | 18 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Python BFS beats 98% with Detailed Explanation and Comments! | course-schedule-ii | 0 | 1 | The high level for this solution: We create a graph and prereq dict to track which nodes we have visited and if we\'re able to travel to certain nodes (if there are no preqs for the intended next course - aka we\'ve taken the prereqs or there are no prereqs.).\nEg. 4, [[1,0],[2,0],[3,1],[3,2]]\nStarting: q: deque([0])\... | 74 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Python || 96.95% Faster || BFS || DFS || Explained | course-schedule-ii | 0 | 1 | **In this question we have applied topological sorting method**\n\n**DFS Solution:**\n```\ndef findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:\n def dfs(sv,visited):\n if visited[sv]==-1:# this course had not been added into the res, but visited again; there is a cycle!\... | 8 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Python 3 - DFS | course-schedule-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
✅[Python] Simple and Clean, beats 90.89%✅ | course-schedule-ii | 0 | 1 | ### Please upvote if you find this helpful. \u270C\n<img src="https://assets.leetcode.com/users/images/b8e25620-d320-420a-ae09-94c7453bd033_1678818986.7001078.jpeg" alt="Cute Robot - Stable diffusion" width="200"/>\n\n*This is an NFT*\n\n\n# Intuition\nThe problem of finding the order of courses to finish all courses c... | 4 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Python BFS/DFS || Image + Intuition Explanation | course-schedule-ii | 0 | 1 | \n\n\n\n\n# Complexity\n- Time complexity: O(V + E)\n<!-- Add your time complexity here, e.g.... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Topological Sorting Python | course-schedule-ii | 0 | 1 | ```\nclass Solution:\n def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:\n dic={i:[] for i in range(numCourses)}\n for crs,pre in prerequisites:\n dic[crs].append(pre)\n output=[]\n visit,cycle=set(),set()\n def dfs(crs):\n if ... | 4 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
Course Schedule II | Topological Sort | Beats 97% 🔥🔥🔥 | course-schedule-ii | 1 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nSame as Course Schedule I. The only tweek is to return array instead of boolean.\n\n# Complexity\n- Time complexity: O(V+E)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(V+E)\n<!-- Add your space complexity here, e.... | 1 | There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
* For example, the pair `[0, 1]`, indicates that to take cou... | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. Topolo... |
python3 Solution | design-add-and-search-words-data-structure | 0 | 1 | \n```\nclass WordDictionary:\n\n def __init__(self):\n self.d={}\n \n def addWord(self, word: str) -> None:\n cur=self.d\n if not self.search(word):\n for i in range(len(word)):\n if word[i] not in cur:\n cur[word[i]]={}\n cur... | 1 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
✅✅Python Simple Solution 🔥🔥 Easy to Understand🔥🔥 | design-add-and-search-words-data-structure | 0 | 1 | # Please UPVOTE \uD83D\uDC4D\n\n**!! BIG ANNOUNCEMENT !!**\nI am Giving away my premium content videos related to computer science and data science and also will be sharing well-structured assignments and study materials to clear interviews at top companies to my first 1000 Subscribers. So, **DON\'T FORGET** to Subscri... | 74 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Easy & Clear Solution Python 3 | design-add-and-search-words-data-structure | 0 | 1 | \n\n# Code\n```\n\nfrom copy import copy\nclass WordDictionary:\n\n def __init__(self):\n self.words = defaultdict(list)\n \n def addWord(self, word: str) -> None:\n self.words[len(word)].append(word)\n \n\n def search(self, word: str) -> bool:\n n=len(word)\n c=copy(s... | 2 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python Trie Solution 6170 ms | design-add-and-search-words-data-structure | 0 | 1 | My solution is very similar to other people\'s answers so I will just say how mine is different.\n\nI will remember the length of the longest given word, so if someone tries to \'look up\' a word that consists of more letters than the longest of our available words, then we can automatically return False.\n\nThis way o... | 4 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
python3 solution with comments | design-add-and-search-words-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python 3 || T/M: 100% / 99% | design-add-and-search-words-data-structure | 0 | 1 | [UPDATED 5/12/2023]\n\nSince the initial post, this problem\'s constraints and test cases were amended such that, as two WLB comments below correctly point out, the code I posted initially TLEs now.\n\nI had another version (now posted below) which was initially slower than the initial post, but now is the faster code.... | 27 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
python super easy trie + dfs | design-add-and-search-words-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python Solution | design-add-and-search-words-data-structure | 0 | 1 | ```\nclass TrieNode: # Time: O(1) and Space: O(n)\n def __init__(self):\n self.children = {}\n self.word = False # flagging every word false and last word will be flagged true to differentiate\n\n\nclass WordDictionary:\n\n def __init__(self):\n self.root = TrieNode()\n\n def addWord(sel... | 1 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Day 78 || Trie + DFS || Easiest Beginner Friendly Sol | design-add-and-search-words-data-structure | 1 | 1 | **NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Intuition of this Problem :\n\n\n**The code impleme... | 21 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python: TLE >>> Faster than 99% || Trie || Optimization | design-add-and-search-words-data-structure | 0 | 1 | For those who are encountering TLE, add some more check conditions\n* While inserting words in the Trie, keep the max length of all the words.\n* While searching in the Trie, if length of the word to be searched is greater than max length, return False.\n\n**Python Code:**\n\n```\nclass TrieNode:\n def __init__(self... | 5 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python DFS | NO TLE ERROR 95%+ Time | design-add-and-search-words-data-structure | 0 | 1 | The key to using DFS and not getting a TLE error is the max_length attribute.\n```\nfrom collections import defaultdict\n\nclass TrieNode:\n def __init__(self):\n self.edges = {}\n self.isWord = False\n\nclass WordDictionary:\n\n def __init__(self):\n self.head = TrieNode()\n self.max_... | 4 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python (Faster than 98%) | Prefix Tree (trie) with DFS | Optimization | design-add-and-search-words-data-structure | 0 | 1 | **This solution gives TLE. If you keep submitting, it will pass all test cases (rarely).**\n\n\n```\nclass TrieNode():\n def __init__(self):\n self.children = {}\n self.endOfWord = False\n\nc... | 3 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
✔️ [Python3] TRIE (っ•́。•́)♪♬, Explained | design-add-and-search-words-data-structure | 0 | 1 | **UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nThe most efficient underlying data structure here would be the Trie. First of all, it will let us economize space by storing reusable prefixes instead of whole words. Second, the time complexity for the search will b... | 23 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python Nested Dictionary 95% | design-add-and-search-words-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt\'s obviously a trie, so we can use a nested dictionary to implement it. It\'s gonna look something like this:\n```\n{\n \'b\': {\'a\': {\'d\': {\'END\': {}}}},\n \'d\': {\'a\': {\'d\': {\'END\': {}}}},\n \'m\': {\'a\': {\'d\':... | 2 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
211: Beats 99.15% and Beats 97.15%, Solution with step by step explanation | design-add-and-search-words-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe WordDictionary class is a data structure that supports adding new words and searching if a string matches any previously added string.\n\nThe class has three methods:\n\n- __init__(self): Initializes the object and creat... | 8 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
INDUSTRY STANDARD CODE | BEST CLEAN CODE | TRIE | MOST UPVOTED | design-add-and-search-words-data-structure | 1 | 1 | # INDUSTRY STANDARD CODE | BEST CLEAN CODE | TRIE | UPVOTED\n```WordDictionary []\nclass WordDictionary {\n private final Node root;\n\n public WordDictionary() {\n root = new Node();\n }\n\n public void addWord(String word) {\n Node node = this.root;\n for (char c : word.toCharArray())... | 3 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Simple Python3 Solution || =~ 100% Faster || 11 Lines Code || Commented || {Dictionary} | design-add-and-search-words-data-structure | 0 | 1 | # Intuition\nImplemented a trie-like approach where we are storing the length of each word in a dictionary and when searching, we are replacing the \'.\' with each letter in the alphabet and checking if the word exists in the dictionary.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Comple... | 3 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Easy python : pure dict trie + dfs | design-add-and-search-words-data-structure | 0 | 1 | # Intuition\nWe don\'t need to create a class like `Node` to build our trie, just using `dict`. \n\nFor `search(self, word: str)`, when meeting `.`, we need to check the next level recursively. When meeting a letter, check whether it is in `cur`, if it is, check its branch recursively.\n\n\n# Code\n```\nclass WordDicti... | 3 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Python3 simple solution! | design-add-and-search-words-data-structure | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 5 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
Trie Approach ---->Prefix Tree | design-add-and-search-words-data-structure | 0 | 1 | \n# Prefix Tree: Trie concept\n```\nclass TrieNode:\n def __init__(self):\n self.children={}\n self.end=False\n\nclass WordDictionary:\n def __init__(self):\n self.root=TrieNode()\n\n def addWord(self, word: str) -> None:\n current=self.root\n for w in word:\n if ... | 2 | Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the `WordDictionary` class:
* `WordDictionary()` Initializes the object.
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
* `bool search(word)` Returns ... | You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. |
DFS Trie Solution | word-search-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n- A trie is a good data structure when dealing with lists of strings, especially when working at the character level. \n\n- Depth-first search () is a good method for finding non-repeating vertex sequences and associates well with trees... | 0 | Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
**Exampl... | You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How a... |
Python - Clean Trie with DFS | word-search-ii | 0 | 1 | ```\nclass Node:\n def __init__(self):\n self.children = {}\n self.word = False\n\n\nclass Trie:\n def __init__(self):\n self.root = Node()\n\n def insert(self, word: str) -> None:\n root = self.root\n\n for ch in word:\n if ch not in root.children:\n ... | 3 | Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
**Exampl... | You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How a... |
212: Beats 94%, Solution with step by step explanation | word-search-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nBeats\n94%\n\n- Space complexity:\nBeats\n81.69%\n\n# Code\n```\nclass Solution:\n def findWords(self, board: List[List[str]], w... | 14 | Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
**Exampl... | You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How a... |
[Python] Accepted 2022 Backtracking + Tries | word-search-ii | 0 | 1 | 1. build tries\n2. do backtracking based on each node.child\n\nIt\'s my first post, seeking for votes. Thank you all!\n\n```\nclass Node:\n def __init__(self):\n self.child = {}\n self.end = None # word\n \nclass Tries:\n def __init__(self):\n self.root = Node()\n \n def inser_w... | 3 | Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
**Exampl... | You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How a... |
✔️ | Python | Trie Easy Solution | word-search-ii | 0 | 1 | # Code\n```\nclass Solution:\n def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:\n res = []\n Trie = lambda : defaultdict(Trie)\n root = Trie()\n def add(s):\n cur = root\n for c in s: cur = cur[c]\n cur[\'$\'] = s\n ... | 3 | Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
**Exampl... | You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How a... |
Python using (Trie) | Backtracking | word-search-ii | 0 | 1 | Comment if you have any queries. \n# Code\n```\n\n// I used single class to implemnet trie,\n// It can be easier to understand by implementing Trie Node and\n// Trie implementation in different classes as Leetcode# 208\n\nclass TrieNode:\n def __init__(self):\n self.children = {}\n self.word_end = Fals... | 1 | Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
**Exampl... | You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How a... |
Python Trie+DFS || ⚡⚡ 53 ms Beats 99.89% ⚡⚡ || ⭐⭐Optimized for LeetCode Tests and well commented⭐⭐ | word-search-ii | 0 | 1 | ### Solution using Trie + DFS + optimizations directed at the LeetCode tests.\n\n- Use a trie to do quick lookups of the words, since the words are max 10 characters long, looking up a word in the trie can be done in O(1). \n- Iterate over all the positions in the matrix doing DFS starting at that position and moving t... | 9 | Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
**Exampl... | You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How a... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.