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
Click this if you're confused.
pacific-atlantic-water-flow
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can naively perform traversal on each tile, then check if that tile can flow to both pacific and atlantic oceans. However, this would have a time complexity of \u200AMathjax\u200A since we may traverse the majority of the grid for each...
1
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
time/space -> O(m*n)
pacific-atlantic-water-flow
0
1
\n\n# Code\n```\nclass Solution:\n def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:\n ROWS,COLS = len(heights),len(heights[0])\n pac,atl = set(),set()\n\n def dfs(r,c,visit,prevHeight):\n if (r < 0 or r == ROWS or c < 0 or c == COLS or \n heights[...
1
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
Python BFS
pacific-atlantic-water-flow
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBFS first, and then take the intersection.\n1. BFS from the nodes that are on the coast of the Pacific to find the nodes that can flow to the Pacific;\n2. BFS from the nodes that are on the coast of the Atlantic to find the nodes that can...
1
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
🥇 PYTHON || EXPLAINED || ; ]
pacific-atlantic-water-flow
0
1
**UPVOTE IF HELPFuuL**\n\n**HERE I HAVE CREATED TWO SEPRATE FUNCTION , ONE FUNCTION COULD BE MADE , THIS IS JUST DONE FOR CLARITY.**\n\nWe need to count the number of cells from which waer could flow to both the oceans.\nWater can go to **left,top** for pacific and to **right,down** for atlantic.\n\n**APPROACH**\n\nWe ...
23
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
Python very concise solution using dfs + set (128ms).
pacific-atlantic-water-flow
0
1
```python\ndef pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:\n if not matrix:\n return []\n p_land = set()\n a_land = set()\n R, C = len(matrix), len(matrix[0])\n def spread(i, j, land):\n land.add((i, j))\n for x, y in ((i+1, j), (i, j+1), (i-1, j), (i, j-1)):\n...
80
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
Simple Solution
pacific-atlantic-water-flow
0
1
\n\n# Code\n```\nclass Solution:\n def pacificAtlantic(self, h: List[List[int]]) -> List[List[int]]:\n n, m = len(h), len(h[0])\n isPacc = [[True if i == 0 or j == 0 else False for j in range(m)] for i in range(n)]\n isAtl = [[True if i == n - 1 or j == m - 1 else False for j in range(m)] for i ...
6
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
417: Time 96.50% and Space 94.12%, Solution with step by step explanation
pacific-atlantic-water-flow
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Check if the input matrix is empty. If it is, return an empty list.\n\n2. Determine the number of rows and columns in the input matrix.\n\n3. Create two visited matrices of the same size as the input matrix, one for each ...
12
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
Python Elegant & Short | DFS | 99.21% faster
pacific-atlantic-water-flow
0
1
![image](https://assets.leetcode.com/users/images/6e32f2ac-4bce-4a83-aa85-26bc06457eeb_1661969728.801651.png)\n\n```\nclass Solution:\n\t"""\n\tTime: O(n*m)\n\tMemory: O(n*m)\n\t"""\n\n\tMOVES = [(-1, 0), (0, -1), (1, 0), (0, 1)]\n\n\tdef pacificAtlantic(self, heights: List[List[int]]) -> Set[Tuple[int, int]]:\n\t\td...
14
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
95% Faster, Python
pacific-atlantic-water-flow
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 is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
Easy understand DFS solution beat 97%
pacific-atlantic-water-flow
0
1
![image](https://assets.leetcode.com/users/jefferyzzy/image_1584588025.png)\n\n```Python\ndef pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:\n if not matrix or not matrix[0]:return []\n m, n = len(matrix),len(matrix[0])\n p_visited = set()\n a_visited = set()\n dir...
31
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
Python DFS and BFS Solution
pacific-atlantic-water-flow
0
1
```\n\nDFS\n-------------------------------\nclass Solution:\n def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:\n rowlen = len(heights)\n collen = len(heights[0])\n pacific = set({})\n atlantic = set({})\n \n def dfs(row, col, s):\n x = heig...
1
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an `m x n` i...
null
Python | O(NM) one-liner Solution without modifying board
battleships-in-a-board
0
1
- Full solution:\n```python\nclass Solution:\n def countBattleships(self, board: List[List[str]]) -> int:\n count = 0\n \n for i, row in enumerate(board):\n for j, cell in enumerate(row):\n if cell == "X":\n if (i == 0 or board[i - 1][j] == ".") and\\...
17
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Simple python solution Beats 99%
battleships-in-a-board
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking us to count the number of battleships on the board. A battleship is represented by a \'X\' on the board, and it can be either a single \'X\' or a group of \'X\' that are horizontally or vertically adjacent to each ot...
2
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
419: Solution with step by step explanation
battleships-in-a-board
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function called countBattleships that takes a 2D board as input and returns an integer.\n\n2. Initialize a variable called count to 0, which will hold the number of battleships found.\n\n3. Loop over the rows and...
8
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Simple Easy to Understand Python O(1) extra memory
battleships-in-a-board
0
1
Loop through each element in the 2D array and if the element is \'X\', check up and to the left for \'X\'. If this exists then you do not add one because it is a part of another battleship you already counted.\n```\nclass Solution:\n def countBattleships(self, board: List[List[str]]) -> int:\n count = 0\n ...
9
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Easy Python Follow Up Solution with O(1) Extra Memory
battleships-in-a-board
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince we only need the total number of ships, we can only focus on counting the heads of each ship. Now how do we determine if a position X is head of the ship or part of the ship. Lets say the board is like this\n . . . . X . \n . . . . ...
4
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Python3 O(N ^ 2) solution with DFS (94.55% Runtime)
battleships-in-a-board
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/ac35edbf-0e0d-45e6-9079-1754dac3584f_1702458289.3082485.png)\nUtilize depth first search to sort out cells belong to the same boat\n\n# Approach\n<!-- Describe your approach to solving...
0
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Simple straight solution, beats 43%, 45%
battleships-in-a-board
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 an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
One-liner python solution. O(m*n) time and O(1) extra space.
battleships-in-a-board
0
1
# Intuition\nCount the top left hand corner of every battleship\n\n# Approach\nLoop through each cell on the board and only count an \'X\' cell if it does not have another \'X\' to it\'s direct top or left\n\n# Complexity\n- Time complexity:\nO(m*n)\n\n- Space complexity:\nO(1) extra space\n\n# Code\n```\nclass Solutio...
0
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
DFS approach with O(MN) space complexity
battleships-in-a-board
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nImplemented a one-direction DFS helper to traverse either right or cells below and mark them as visited. We don\'t need to care about cells on the left and above since they\'ll be visited by DFS or for loops already.\n\n# Complexity\n- Time complexity...
0
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Time: O(N), Space: O(1)
battleships-in-a-board
0
1
# Intuition\nIf any cell previous to current cell by 1 row or 1 column is X then we already have counted the ship on current cell.\n\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution:\n def countBattleships(self, board: List[List[str]]) -> int:\n\n M, N = len(bo...
0
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Simple Intuative DFS
battleships-in-a-board
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 an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Follow up solution: Python3
battleships-in-a-board
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n The problem is similar to number of islands. As we see "X", we \n increase the count of battleships and mark all the adjacents \n horizontal or vertical "X" as "."\n# Approach\n<!-- Describe your approach to solving the problem...
0
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Simple Python: Count top-left corners | O(m.n) TC + O(1) SC
battleships-in-a-board
0
1
# Intuition / Approach\nCount only the top-left corners of the battleships. A top-left corner of battleship is the cell which doesn\'t have any immediate adjacent \'X\' to its top or left.\n\n# Complexity\n- Time complexity:\n$$O(m.n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n def countBatt...
0
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
Easy python solution using BFS
battleships-in-a-board
0
1
# Intuition\nUse BFS to find the connected components from each ship\n\n# Approach\ninitialize `component`, `ships`, `visited` to store the number of ships, all possible ships and visited nodes respectively. for each ship apply `BFS` to find the connected component and increment the `component`. finally return the `com...
0
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return _the number of the **battleships** on_ `board`. **Battleships** can only be placed horizontally or vertically on `board`. In other words, they can only be made of the shape `1 x k` (`1` row, `k` columns) or `k x 1` (`k` rows, ...
null
✅ [video walkthrough] 🔥 beats 95.82% || short and intuitive solution
strong-password-checker
0
1
Please upvote if you find the walkthrough and/or code helpful :)\n\nhttps://youtu.be/9CxpKVR14ps\n\n```\nimport string\n\nclass Solution: \n def strongPasswordChecker(self, s):\n lowercase = set(string.ascii_lowercase)\n uppercase = set(string.ascii_uppercase)\n digits = set([str(elem) for el...
214
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Dynamic Programming
strong-password-checker
0
1
# Intuition\n\nThe state is a tuple (position in old password, new password length, mask of present characters, repeating suffix). Key insight is that optimal insertions and replacements require at most two distinct characters from each category (lowercase, uppercase, digits): `MAGIC = "abAB01"`.\n\n# Approach\n\nWhen ...
6
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Python 3 || 14 lines, w/ brief explanation || T/M: T/M: 91% / 91%
strong-password-checker
0
1
Here\'s the plan:\nWe divide the problem into three cases based on `n = len(password)`:\n- Case 1: `n < 6` : We add enough chars to ensure 1a) the length of the password is greater than 6 and 1b) each category of chars (uppercase, lowercase, digit) are present. \n- Case 2: `n > 20` : We eliminate `n - 20` chars star...
6
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Python shortest solution
strong-password-checker
0
1
# Intuition and Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThis solution provides a method to determine the minimum number of steps required to make a given password strong, according to the set of requirements described in the problem.\n\nThe solution first checks if the password ...
2
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
A readable solution in Python
strong-password-checker
0
1
In principle, this code just checks one rule after the other ithout taking major shortcuts besides one: The `len(s) < 6` case is not treated rigorously as it has the special case that as long as rules (1) and (2) are satisfied by inserting and replacing things, rule (3) is guaranteed to be satisfied.\nThis solution emp...
29
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
[Python3] greedy
strong-password-checker
0
1
Based on the excellent solution of @zhichenggu. \n\n```\nclass Solution:\n def strongPasswordChecker(self, password: str) -> int:\n digit = lower = upper = 1\n for ch in password: \n if ch.isdigit(): digit = 0 \n elif ch.islower(): lower = 0\n elif ch.isupper(): upper =...
1
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
python solution
strong-password-checker
0
1
# Intuition\nThe intuition behind this code is to check the strength of a password. A strong password is one that is not easily guessable and includes a mix of lowercase letters, uppercase letters, digits, and does not contain three repeating characters in a row.\n\n# Approach\nThe approach is to first check the presen...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Tiny, easy, and relatively efficient solution in Python3 (4 LoC, actual "shortest" solution)
strong-password-checker
0
1
*If you found this helpful, please upvote it. If you see a mistake please let me know via a comment and I will do my best to fix it* \uD83D\uDE0A.\n\n# Intuition\nTo solve this problem, we need to focus on 3 types of operations: \n- Deletions or insertions needed to fit the password length within the 6-20 character bou...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Python
strong-password-checker
0
1
Not the cleanest solution but it seems to work\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Solution:\n def strongPasswordChecker(self, password: str) -> int:\n hasdigits = any(char.isdigit() for char in password)\n hasupper = any(char.isupper() f...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
How strong is THIS password checker?
strong-password-checker
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
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Relatively readable ;)
strong-password-checker
0
1
# Intuition\n\nGather data first then do the necessary looping\n\n# Approach\nget to [6..20] and then do replaces as needed to avoid 3 in row and/or missing type of character\n\n# Complexity\n- Time complexity:\nmeh\n\n- Space complexity:\nlight\n\n# Code\n```\nclass Solution:\n def strongPasswordChecker(self, passw...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
36ms python3 solution
strong-password-checker
0
1
# Intuition\nTo create a strong password, the constraints require having at least one lowercase letter, one uppercase letter, one digit, a length between 6 and 20 characters, and no three repeating characters in a row. \n\n1. **Identifying Missing Types**: Start by identifying the missing character types (lowercase, up...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Python3 with explanation
strong-password-checker
0
1
# Intuition\nChecking the strength of a password typically involves ensuring it meets certain criteria such as containing numbers, uppercase letters, lowercase letters, and not having repeated characters. The given problem seems to be an advanced form of this check, where we not only have to identify if the password is...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
JS || java || Python || python3 || C++ || C || C# || PHP || Kotlin || 100% Accuracy, Runtime, Memory
strong-password-checker
1
1
# Please Upvote\n```javascript []\nconst strongPasswordChecker = (passwd) => {\n let steps = 0;\n let mustAdd = 0;\n\n if (!passwd.match(/[A-Z]/)) {\n mustAdd++;\n }\n if (!passwd.match(/[a-z]/)) {\n mustAdd++;\n }\n if (!passwd.match(/\\d/)) {\n mustAdd++;\n }\n\n let gr...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Strengthening Passwords: String Manipulation and Condition Checking
strong-password-checker
0
1
# Intuition\nThis problem involves string manipulation and checks for specific conditions. The first approach that comes to mind is to traverse the string while keeping track of the conditions that are already satisfied and the ones that aren\'t. The conditions to check are:\n\nLength of the password between 6 and 20 c...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
420. Strong Password Checker, RunTime: 40ms Beats 88.06%, Memory: 16.21mb Beats 89.05%
strong-password-checker
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the minimum number of steps required to make a given password strong based on specific conditions. We need to check the length of the password, whether it contains the required character types (lowercase lette...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Easy
strong-password-checker
0
1
\n# Code\n```\nclass Solution:\n def strongPasswordChecker(self, s: str) -> int:\n missing_type = 3\n if any(\'a\' <= c <= \'z\' for c in s):\n missing_type -= 1\n if any(\'A\' <= c <= \'Z\' for c in s):\n missing_type -= 1\n if any(c.isdigit() for c in s):\n ...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Strengthening Passwords: Unlocking Security with Intuition
strong-password-checker
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe task is to determine the minimum number of steps required to make a given password strong. A password is considered strong if it satisfies the following requirements:\n- It has at least 6 characters.\n- It has at least one lowercase l...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Effective Character Type and Length Management for Strong Password Validation
strong-password-checker
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires us to make a given password strong by following certain conditions. The conditions involve the length of the password, the types of characters it contains, and the absence of three repeating characters in a row. The i...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Concise solution with the only external dependency being the 're' library
strong-password-checker
0
1
# Code\n```\nimport re\n\nUPPER = re.compile("[A-Z]")\nLOWER = re.compile("[a-z]")\nDIGIT = re.compile("[0-9]") \n\n\ndef diversity_penalty(password):\n score = 0\n for r in [UPPER, LOWER, DIGIT]:\n if r.search(password):\n score += 1\n return 3 - score\n\n\ndef try_deleting(password, mod3):\...
0
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
re
strong-password-checker
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
A password is considered strong if the below conditions are all met: * It has at least `6` characters and at most `20` characters. * It contains at least **one lowercase** letter, at least **one uppercase** letter, and at least **one digit**. * It does not contain three repeating characters in a row (i.e., `"B**...
null
Trie Solution Not Working - TLE (Python) | O(32n)
maximum-xor-of-two-numbers-in-an-array
0
1
As the comments before the major loops and function in the below given code tells, the allover time complexity (TC) of this code is O(n), but why is it only able to pass 41 out of 45 testcases?!\n\n```python\nclass Trie:\n def __init__(self, val: int):\n self.val = val\n self.children = [None, None]\n ...
0
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
Python || Trie Solution|| Easy to Understand
maximum-xor-of-two-numbers-in-an-array
0
1
```\nclass Trie:\n def __init__(self):\n self.root={}\n self.m=0\n \n def insert(self,word):\n node=self.root\n for ch in word:\n if ch not in node:\n node[ch]={}\n node=node[ch]\n \n def compare(self,word,i):\n node=self.root\n ...
2
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
421: Solution with step by step explanation
maximum-xor-of-two-numbers-in-an-array
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We first find the maximum number in the input list nums using the built-in max function.\n\n2. If the maximum number is 0, it means that all the numbers in the list are also 0, and so the maximum XOR value will also be 0....
6
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
Fast Python solution. Beats 99%
maximum-xor-of-two-numbers-in-an-array
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea behind this solution is to use a bitwise operation to find the maximum XOR value of any two numbers in the list.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe solution starts by initializing the variab...
7
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
Python 3 | Standard Trie | Explanations
maximum-xor-of-two-numbers-in-an-array
0
1
### Explanation\n- Build a trie first\n- For each number `num` in `nums`, try to find number which can generate maximum value with `num` using `XOR`\n\t- From left to right, take the reverse bit if possble, otherwise take the same bit, e.g.\n\t- say if the k th bit from left for `num` is `1`, then to make it maximum, w...
21
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
Python Bitwise Solution
maximum-xor-of-two-numbers-in-an-array
0
1
```\nclass Solution:\n def findMaximumXOR(self, nums: List[int]) -> int:\n try: l = int(log2(max(nums)))\n except: l = 1\n\n mask, res = 0, 0\n\n for i in range(l, -1, -1):\n mask |= 1 << i\n S = set(mask & num for num in nums)\n temp = res | 1 << i\n\n ...
1
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
Clean Python Solution using TRIE with Comments Easy to Understand
maximum-xor-of-two-numbers-in-an-array
0
1
Insert all the elements of nums in a trie with bit values in respective position. where every node can have 2 children either with 0 key or 1 key. \n\nAs XOR is a **inequality detector** so we try to maximize the inequality between num and node. So that the XOR of num and value of node will give the max value.\n\nSo we...
6
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
70% TC and 50% SC easy python solution, with clear approach
maximum-xor-of-two-numbers-in-an-array
0
1
1. Insert every num in the tree, and make sure that they have equal number of bits, with leading zeros.\n2. Then try every num, and find what will be the max xor of it with any other num in the array. And come up with the max xor of all.\n3. And thats done, like, if the current digit is 1, chech if 0 is avlbl at that p...
1
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
Python Simple Solution (beginners) (video + code) (Trie)
maximum-xor-of-two-numbers-in-an-array
0
1
[](https://www.youtube.com/watch?v=wSgrc98d2lI)\nhttps://www.youtube.com/watch?v=wSgrc98d2lI\n```\nclass TrieNode:\n def __init__(self):\n self.children = {}\n \nclass Solution:\n def __init__(self):\n self.root = TrieNode()\n \n def insert_bits(self, num):\n bit_num = bin(num)[2...
10
Given an integer array `nums`, return _the maximum result of_ `nums[i] XOR nums[j]`, where `0 <= i <= j < n`. **Example 1:** **Input:** nums = \[3,10,5,25,2,8\] **Output:** 28 **Explanation:** The maximum result is 5 XOR 25 = 28. **Example 2:** **Input:** nums = \[14,70,53,83,49,91,36,80,92,51,66,70\] **Output:** 1...
null
423: Solution with step by step explanation
reconstruct-original-digits-from-english
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create an empty dictionary called char_counts to keep track of the count of each character in the input string s.\n2. Iterate over each character c in the input string s.\n3. For each character c, update its count in char...
8
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Fast Python solution O(n)
reconstruct-original-digits-from-english
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking to find the unique digits in the given string by counting the number of occurrences of each letter in the string and using that information to deduce the number of each digit.\n\n\n# Approach\n<!-- Describe your appr...
4
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Python/Go O(n) by dictionary [w/ Diagram]
reconstruct-original-digits-from-english
0
1
**Hint**:\n\n1. Rebuild **even digits** first, becuase each even digit has corresponding **unique character** naturally\n\n1. Then rebuild **odd digits** from observed character-occurrence mapping.\n\n---\n\n**Diagram** for even digits (i.e., 0, 2, 4, 6, 8 ) rebuilding based on character-occurrence mapping\n\n![image](...
17
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
[Java/C++/Python/JavaScript/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022
reconstruct-original-digits-from-english
1
1
```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *...
3
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Python, Simple
reconstruct-original-digits-from-english
0
1
# Idea\nCertain chars are unique to some strings, like \'w\' in \'two\' or \'z\' in \'zero\'. As we process the digits other chars become unique, for example \'r\' is unique to \'three\' after we\'ve removed all \'four\'s and \'zero\'s. In python3 the dictionary is ordered by key insertion time by default, so the solut...
7
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Python | Intuitive Solution With Explanation
reconstruct-original-digits-from-english
0
1
\n# Code\n```\nclass Solution:\n def originalDigits(self, s: str) -> str:\n # 0 -> 2 -> 4 -> 6 -> 8 -> 1 -> 3 -> 5 ->7 -> 9\n digitEnglishMap = {\n 0: {"z": 1, "e": 1, "r": 1, "o": 1},\n 1: {"o":1, "n":1, "e":1},\n 2: {"t":1, "w":1, "o":1},\n 3: {"t":1, "h":1...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
OOP solution
reconstruct-original-digits-from-english
0
1
\n# Code\n```\nclass Solution:\n def originalDigits(self, s: str) -> str:\n self.s = s\n self.result = \'\'\n self.digits = {\'zero\': 0, \'one\': 1, \'two\': 2, \'three\': 3, \'four\': 4, \'five\': 5, \'six\': 6, \'seven\': 7,\n \'eight\': 8, \'nine\': 9}\n\n return...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Easy but long solution
reconstruct-original-digits-from-english
0
1
# Intuition\nEliminate letters greedily in order of unique letters in the corresponding numbers. For example, the only number containing "w" between "zero" and "nine" is "two. Next, the only number containing "x" between "zero" and "nine" excluding "two" is "six". It works out nicely... Just need to sort at the end (or...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
python3 solution
reconstruct-original-digits-from-english
0
1
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach : using hashmap \n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your spa...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Simple intuition, easy to understand.
reconstruct-original-digits-from-english
0
1
# Intuition\nWe need to identify the order in which we can uniquely create a number.\nFor example, only zero has z, two has w, four has u, and six has x.\n\nonce we have built six then only seven has s in it.\nonce we build all the sevens then only five has v in it\nAlso we can observe only three has r in it and once w...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Easy python3
reconstruct-original-digits-from-english
0
1
# Code\n```\nclass Solution:\n def originalDigits(self, s: str) -> str:\n from collections import Counter\n maps_l = [{\'z\': 0, "w": 2, \'u\': 4, "x": 6, "g": 8}, {\'o\': 1, \'t\':3, \'f\': 5, \'s\': 7}]\n map_d = {0:\'zero\', 1: \'one\', 2 :\'two\', 3: \'three\', 4: \'four\', 5: \'five\', 6: \...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Python3 || Counter and Dictionary || 85% and 75% || Less than 20 lines of code
reconstruct-original-digits-from-english
0
1
# Intuition\nOn finding the count of each digits, we can find the required string.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIntially, we find the count of each letter in the string `s` using Counter() which `letters`. Then, we calculate the no. of digits present using the counter in `digi...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Simple Solution With Explanation
reconstruct-original-digits-from-english
0
1
# Intuition\nWe use unique characters in string representation of digits.\n\n# Approach\nWe count all characters in the string. Then we arrange digits according to unique characters in their string representation.\nFirstly we have ```(\'one\', \'two\', \'three\', \'four\', \'five\', \'six\', \'seven\', \'eight\', \'nin...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Linear Algebra Method, O(n * m) but highly generic
reconstruct-original-digits-from-english
0
1
# Why post an O(n m) solution?\n\nI dislike the solutions that have been published because they require you to spot some property of the english words `one two three four five six seven eight nine`. \n\nFor example, there\'s a couple of solutions which rely on letters being unique to words (eg. \'g\' only occurs in eig...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Reconstruct Original Digits
reconstruct-original-digits-from-english
0
1
# Intuition\nThe problem requires us to identify and count the occurrences of digits from a given string of English letters. We can use the unique patterns of English letters in each digit\'s spelling to our advantage to solve this problem.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Appro...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
common & ordinary solution
reconstruct-original-digits-from-english
0
1
\n\n# Code\n```\nclass Solution:\n def originalDigits(self, s: str) -> str:\n dic = defaultdict()\n for i in s:\n dic[i] = dic.get(i, 0) + 1\n \n zero = dic.get("z", 0)\n two = dic.get("w", 0)\n four = dic.get("u", 0)\n six = dic.get("x", 0)\n seven ...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Python meta-solution
reconstruct-original-digits-from-english
0
1
# Meta\n```\nN = {\'zero\', \'one\', \'two\', \'three\', \'four\', \'five\', \'six\', \'seven\', \'eight\', \'nine\'}\nwhile N:\n U = set()\n for n in N:\n this = set(n)\n other = set(\'\'.join(N - {n}))\n unique = this - other\n if unique:\n print(\'\'.join(unique), n, sep=...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Solution
reconstruct-original-digits-from-english
1
1
```C++ []\nclass Solution {\npublic:\n string originalDigits(string s) {\n vector<int> freq(26);\n for (char c : s) {\n freq[c - \'a\']++;\n }\n vector<int> digits(10);\n digits[0] = freq[\'z\' - \'a\'];\n digits[2] = freq[\'w\' - \'a\'];\n digits[4] = freq...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Another ugly python solution
reconstruct-original-digits-from-english
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 a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
[LC-423-M | Python3] The Representative Character
reconstruct-original-digits-from-english
0
1
The occurring times of a digit word can be calculated by using its representative character.\n\n```python3 []\nclass Solution:\n def originalDigits(self, s: str) -> str:\n \'\'\'\n Owning unique character ones:\n n(\'zero\') = n(\'z\')\n n(\'two\') = n(\'w\')\n n(\'four\') = n(\'u\...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Python easy Solution
reconstruct-original-digits-from-english
0
1
\n\n# Code\n```\nclass Solution:\n def originalDigits(self, s: str) -> str:\n from collections import Counter\n c = Counter(s)\n nums = [0] * 10\n nums[0] = c[\'z\']\n nums[2] = c[\'w\']\n nums[4] = c[\'u\']\n nums[6] = c[\'x\']\n nums[8] = c[\'g\']\n nu...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Python solution
reconstruct-original-digits-from-english
0
1
# Code\n```\nclass Solution:\n def originalDigits(self, s: str) -> str:\n charCount = {}\n targetCharCount = [0]*26\n res = [0]*10\n numString = ["zero","two","six","seven","eight","four","five","three","nine","one"]\n nums = [0,2,6,7,8,4,5,3,9,1]\n for i in range(10):\n ...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
REALLY BAD BEGINNER BRUTE FORCE SOLUTION XD (IDK WHAT IM DOING)
reconstruct-original-digits-from-english
0
1
\n# Code\n```\nclass Solution:\n def originalDigits(self, s: str) -> str:\n lst = []\n h = \'\'\n n = {\'e\':0, \'g\':0, \'f\':0, \'i\':0, \'h\':0, \'o\':0, \'n\':0,\'s\':0, \'r\':0, \'u\':0, \'t\':0, \'w\':0, \'v\':0, \'x\':0, \'z\':0}\n for x in s:\n if x in n:\n ...
0
Given a string `s` containing an out-of-order English representation of digits `0-9`, return _the digits in **ascending** order_. **Example 1:** **Input:** s = "owoztneoer" **Output:** "012" **Example 2:** **Input:** s = "fviefuro" **Output:** "45" **Constraints:** * `1 <= s.length <= 105` * `s[i]` is one of ...
null
Easy || 100% || Fully Explained || C++, Java, Python, JavaScript, Python3 || Sliding Window
longest-repeating-character-replacement
1
1
# **PROBLEM STATEMENT:**\nGiven a string s and an integer k. Choose any character of the string and change it to any other uppercase English character. Perform this operation at most k times.\nReturn the length of the longest substring containing the same letter you can get after performing the above operations.\n# **E...
152
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Python: Two-Pointers + Process for coding interviews
longest-repeating-character-replacement
0
1
Hello,\n\nHere is my solution with my process for coding interview. All feedbacks are welcome.\n\n1. Problem Summary / Clarifications / TDD:\n `Ouput(ABAB, 2): 4`\n `Ouput(AABABBA, 1): 4`\n `Ouput(BAAAABBA, 1): 5`\n `Ouput(BAAAABBA, 3): 8`\n `Ouput(BAAAABBBBBA, 1): 6`\n `...
334
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
O( n )✅ | Python (Step by step explanation)✅
longest-repeating-character-replacement
0
1
# Intuition\nThe problem involves finding the longest substring in a string where you can replace at most \'k\' characters to make all characters in the substring the same. \n\n# Approach\n1. We maintain a sliding window approach to find the longest valid substring.\n2. We use a dictionary \'alphabets\' to keep track o...
2
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
O( 26*n )✅ | Python (Step by step explanation)✅
longest-repeating-character-replacement
0
1
# Intuition\nThe problem requires finding the longest substring in a string where you can replace at most \'k\' characters to make all characters in the substring the same. \n\n# Approach\n1. We maintain a sliding window approach to find the longest valid substring.\n2. We use a dictionary \'alphabets\' to keep track o...
4
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Sliding Window---->Blind 75 Solution
longest-repeating-character-replacement
0
1
\n# Sliding Window ----->O(26*N)\n```\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n output,left,dic=0,0,{}\n for right in range(len(s)):\n dic[s[right]]=1+dic.get(s[right],0)\n while (right-left+1)-max(dic.values())>k:\n dic[s[left]]-=1...
16
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
✔️ 2 Ways to Solving Longest Repeating Character Replacement 🪔
longest-repeating-character-replacement
0
1
# Solution 1:\n\n# Intuition\nThe goal is to find the length of the longest substring in which we can replace at most k characters to make all characters in the substring the same.\n\n\n# Approach\nWe maintain a sliding window to keep track of the current substring.\nFor each character encountered, we update the count ...
2
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
424: Solution with step by step explanation
longest-repeating-character-replacement
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize variables window_start, max_length, max_count, and char_count. window_start is the start index of the sliding window. max_length is the length of the longest substring with repeating characters seen so far. max...
7
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Python Solution using Sliding Window
longest-repeating-character-replacement
0
1
```\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n ws=0\n freqmap={}\n maxlen=0\n maxfreq=0\n for we in range(len(s)):\n currchar=s[we]\n freqmap[currchar]=freqmap.get(currchar,0)+1\n maxfreq=max(maxfreq,freqmap[currchar...
1
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Sliding Window Python Solution
longest-repeating-character-replacement
0
1
```\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n ws=0\n d={}\n freq=0\n maxlen=0\n for we in range(len(s)):\n c=s[we]\n d[c]=d.get(c,0)+1\n freq=max(freq,d[c])\n if we-ws+1-freq>k:\n leftchar=...
1
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Python3 solution with explanation || quibler7
longest-repeating-character-replacement
0
1
# Algorithm \n\n- Initialise `count` dict to keep count of occuring char in given string. initialise `res` to keep track of length of maximum repeating window as we have to return this as ouput. set left and right pointer to 0 and `maxf` to keep count of freq of char that is occuring most in our sliding window.\n\n- Us...
3
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Python code explained with approach and time complexity
longest-repeating-character-replacement
0
1
Intuition:\n\n- Initialize two pointers l and r to 0, and a dictionary count to keep track of the frequency of characters in the current window.\n- Initialize maxf and res to 0. maxf stores the frequency of the most frequent character in the current window, and res stores the length of the longest substring containing ...
8
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Python Sliding Window
longest-repeating-character-replacement
0
1
```\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n d = {}\n slow = 0\n ans = 0\n for fast in range(len(s)):\n if s[fast] in d:\n d[s[fast]] += 1\n else:\n d[s[fast]] = 1\n \n # get max o...
3
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Python Easiest solution With Explanation | 96.68% Faster | Beg to Adv | Sliding Window
longest-repeating-character-replacement
0
1
\n```python\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n maxf = l = 0\n count = collections.Counter() # counting the occurance of the character in the string. \n\t\t# instead of using "count = collections.Counter()", we can do the following:-\n\t\t"""\n\t\tfor r, n in enu...
15
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
Sliding window - Python code with proper explanation
longest-repeating-character-replacement
0
1
```\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n """\n \n s = ABAB\n k = 2\n \n If we have no limit on k then we can say that \n (no of replacements to be done = \n length of string - count of character with maximum occurence)\n ...
3
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
[Python] Simple O(N) solution with explanation
longest-repeating-character-replacement
0
1
```\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n ## RC ##\n\t\t## APPROACH : SLIDING WINDOW ##\n # Logic #\n # 1. Increase the window if the substring is valid else,\n # 2. slide the window with the same length. No need to shrink the window\n \n\t\t##...
41
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
[Java/C++/Python/JavaScript/Kotlin/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022
longest-repeating-character-replacement
1
1
```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *...
6
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
✅ 🔥 O(n) Python3 || ⚡ easy solution
longest-repeating-character-replacement
0
1
\n```\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n left = right = 0\n freq = {}\n max_count = 0\n longest = 0\n while right < len(s):\n freq[s[right]] = freq.get(s[right], 0) + 1\n max_count = max(max_count, freq[s[right]])\n ...
3
You are given a string `s` and an integer `k`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most `k` times. Return _the length of the longest substring containing the same letter you can get after performing the above operations_. ...
null
[Python] Recursion; Explained
construct-quad-tree
0
1
This is a simple recursion problem.\n\nWe can go from top to bottom (i.e., 2 ^ n --> 1) and build the tree based on the return value of the four children.\n\n(1) if the number of element in the grid region is 1, this is a leaf node, we build a new node and return it;\n(2) check the four children, if they all are leaf ...
2
Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree. Return _the root of the Quad-Tree representing_ `grid`. A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes: * `val`: True if the node r...
null
[Python] Recursion; Explained
construct-quad-tree
0
1
This is a simple recursion problem.\n\nWe can go from top to bottom (i.e., 2 ^ n --> 1) and build the tree based on the return value of the four children.\n\n(1) if the number of element in the grid region is 1, this is a leaf node, we build a new node and return it;\n(2) check the four children, if they all are leaf ...
2
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**. You may assume that the given expression is always v...
null
Solution
construct-quad-tree
1
1
```C++ []\nclass Solution {\npublic:\n bool sameVal(vector<vector<int>>& grid, int x, int y, int size)\n {\n for(auto i{x}; i<x+size; ++i)\n {\n for(auto j{y}; j<y+size; ++j)\n {\n if(grid[i][j] != grid[x][y])\n {\n return false;...
1
Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree. Return _the root of the Quad-Tree representing_ `grid`. A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes: * `val`: True if the node r...
null
Solution
construct-quad-tree
1
1
```C++ []\nclass Solution {\npublic:\n bool sameVal(vector<vector<int>>& grid, int x, int y, int size)\n {\n for(auto i{x}; i<x+size; ++i)\n {\n for(auto j{y}; j<y+size; ++j)\n {\n if(grid[i][j] != grid[x][y])\n {\n return false;...
1
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, `'+'`, `'-'`, `'*'`, `'/'` operators, and open `'('` and closing parentheses `')'`. The integer division should **truncate toward zero**. You may assume that the given expression is always v...
null
Python Solution O(n^2) Solution Beats 94%
construct-quad-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThink of how you would do divide and conquer on an array if we had to divide the array in half everytime we do a recursion.\n\nWe will typically have a start index and number of elements from the start. \nFor example if our array is like ...
1
Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent `grid` with a Quad-Tree. Return _the root of the Quad-Tree representing_ `grid`. A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes: * `val`: True if the node r...
null