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
390. Elimination Game (Test Cases )
elimination-game
0
1
# Intuition\n We are trying to find the last remaining number in a sequence of numbers from 1 to n after applying a specific elimination algorithm. \n The algorithm involves repeatedly removing numbers alternately from left to right and from right to left until only one number remains. \n\n# Approach\nWe use a recursiv...
0
You have a list `arr` of all integers in the range `[1, n]` sorted in a strictly increasing order. Apply the following algorithm on `arr`: * Starting from left to right, remove the first number and every other number afterward until you reach the end of the list. * Repeat the previous step again, but this time fro...
null
O(log(n)) py sol.
elimination-game
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:O(log(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...
0
You have a list `arr` of all integers in the range `[1, n]` sorted in a strictly increasing order. Apply the following algorithm on `arr`: * Starting from left to right, remove the first number and every other number afterward until you reach the end of the list. * Repeat the previous step again, but this time fro...
null
391: Time 95.62%, Solution with step by step explanation
perfect-rectangle
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize the area of all the rectangles to 0 and create a set to keep track of the corners of the rectangles.\n```\narea = 0\ncorners = set()\n```\n2. Iterate over each rectangle in the input list and do the following:\...
4
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
[Python] Fast and clear solution with explanation
perfect-rectangle
0
1
We need to check two things:\n- the external corners must appear only once, and the ones inside have to be an even number (we filter them with xor).\n- the total area of all the rectangles together, has to be equal to the area created by the external corners\n```\nclass Solution:\n def isRectangleCover(self, rectang...
47
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Python3 short + Very Easy with comments O(N)
perfect-rectangle
0
1
```\ndef isRectangleCover(self, rectangles: List[List[int]]) -> bool:\n # simple problem\n # Check if area of each adds up to the final one\n # If not no solution but if it does then you need to make sure the number of corners are 4 only.\n # If the corners are more than that that means some...
4
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
python solution
perfect-rectangle
0
1
```\nclass Solution(object):\n def isRectangleCover(self, rectangles):\n """\n :type rectangles: List[List[int]]\n :rtype: bool\n """\n hs = set()\n area = 0\n for rec in rectangles:\n top_left = (rec[0], rec[1])\n top_right = (rec[0], rec[3])\n ...
20
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
O(n) solution
perfect-rectangle
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea for this problem is to check if a set of rectangles can be covered by a single rectangle.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo do this, we can use the following steps:\n\n- If the input list of...
3
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Think of few Conditions, only a perfect rectangle passes
perfect-rectangle
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n Think of few Conditions, only a perfect rectangle passes\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n Idea : Rectangle is perfect then it follows below conditions.\n 1. Area of larger rect...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Full logic solution, beats 65%, 7%
perfect-rectangle
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. -->\nbased on below assumption:\n1. the area of the new rectangle == the summary of all rectangles.\n2. only has 4 conners\n3. for each point that isn\'t a conner, it has 2...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
😚Python3🐽391. Perfect Rectangle🤿Geometry Math🕌
perfect-rectangle
0
1
![image.png](https://assets.leetcode.com/users/images/83395c3b-bc32-4d3c-ab8d-a9ac29319b58_1696047884.6106713.png)\n\n# Intuition\nGeometry, Math, Line Sweep\n\n# Approach\ncount overlapping situation of each dot\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Soluti...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Python, O(n), Easy Explanation, Diagrams, Breakdown by Code Section
perfect-rectangle
0
1
(read on a standalone page [here](https://hakanalpay.com/leetcode/391-perfect-rectangle))\n\n### Problem Insight:\n\n1. The combined area of all small rectangles should equal the area of the large rectangle.\n\n![Group 8.jpg](https://assets.leetcode.com/users/images/c89c1ba0-dcba-4787-a40a-cc936e3a9c47_1694551363.57513...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Clean Python Sweel Line O(N log N)
perfect-rectangle
0
1
In this solution I wanted to practice sweep line algorithm. Even though most solutions count corners, this solution beats 20% of them. The idea is to sweep the plane from left to right with an imaginary line parallel to y axis.\n\n1. We create a list of "events" that will occur with this imaginary line. \n1.1. When the...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Solution
perfect-rectangle
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 array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Perfect Rectangle
perfect-rectangle
0
1
# Intuition\nAll rectangles form an exact rectangle cover if:\n1. The area of the rectangle cover is equal to the area of all rectangles.\n2. Any point should appear even times as (a corner of a small triangle), otherwise there will be overlaps or gaps. So we can maintain a set to store the corner points. If the number...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Python solution | Beats 100%
perfect-rectangle
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 array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Geometry and set
perfect-rectangle
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 array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
(Python) Exact Cover Rectangle
perfect-rectangle
0
1
# Intuition\nTo check if the given rectangles form an exact cover of a rectangular region, we need to make sure that:\n\n1. All rectangles together completely cover the rectangular region.\n2. No part of the rectangular region is covered twice or more.\n# Approach\nWe can keep track of the area of the union of all rect...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Solution
perfect-rectangle
1
1
```C++ []\n#define MOD 113\n#define P 31\n\nclass Solution {\n int corners[MOD];\n int sum = 0;\n inline long long area(const vector<int> &r) {\n return (long long)(r[2] - r[0]) * (r[3] - r[1]);\n }\n inline void add(const pair<int, int> &p) {\n int& v = corners[((p.first * P + p.second) % ...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Python approach
perfect-rectangle
0
1
# Intuition\nThe main point here is to compare the corner and area. The area is the same as the rectangle area, which will pass. The second point is the corner, and if we meet the corner once, we record the corner. If we meet it the second time, we delete the corner. The similar continues; if we meet the corner the thi...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Python (Simple Maths)
perfect-rectangle
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 array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Perfect Rectangle-Solution
perfect-rectangle
0
1
# Code\n```\nclass Solution:\n def isRectangleCover(self, rectangles: List[List[int]]) -> bool:\n corner=set()\n c=lambda k:k[0]+k[1]\n a = lambda: (Y-y) * (X-x)\n area=0\n for x, y, X, Y in rectangles:\n area += a()\n corner ^= {(x, Y), (X, y), (x, y), (X, Y)...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
PYTHON 90% FASTER EASY SOLUTION
perfect-rectangle
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 array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
Python Solution (fast)
perfect-rectangle
0
1
![Screenshot 2022-12-19 at 10.44.07.png](https://assets.leetcode.com/users/images/a78c1d6c-b4a4-4bd2-8518-10df8a17e3f8_1671439487.2850132.png)\n\n```\nclass Solution:\n def isRectangleCover(self, rectangles: List[List[int]]) -> bool:\n X1, Y1 = float(\'inf\'), float(\'inf\')\n X2, Y2 = -float(\'inf\'),...
0
Given an array `rectangles` where `rectangles[i] = [xi, yi, ai, bi]` represents an axis-aligned rectangle. The bottom-left point of the rectangle is `(xi, yi)` and the top-right point of it is `(ai, bi)`. Return `true` _if all the rectangles together form an exact cover of a rectangular region_. **Example 1:** **Inp...
null
🗓️ Daily LeetCode Challenge September, Day 22 | C++, C, Python3, Kotlin
is-subsequence
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nChecking One String character wise in the other.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n**C++**\n```\nclass Solution {\npublic:\n bool isSubsequence(string s, string t...
1
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
✅ 93.76% Two Pointers & DP
is-subsequence
1
1
# Explanation of the "Is Subsequence" Problem and Solutions\n\n## Introduction & Problem Statement\n\nGiven two strings, `s` and `t`, the challenge is to determine if `s` is a subsequence of `t`. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted with...
84
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
isSubsequence Solution in Python3
is-subsequence
0
1
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInside the "isSubsequence" method, there are two variables, \'s_index\' and \'t_index,\' both initially set to 0. These variables will be used to keep track of the positions within the strings \'s\' and \'t.\'\n\nThe main part of the code is a whi...
1
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
【Video】How I think about a solution - O(t) time, O(1) space - Python, JavaScript, Java, C++
is-subsequence
1
1
This artcle starts with "How I think about a solution". In other words, that is my thought process to solve the question. This article explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope this aricle is helpful for someone.\n\n# Intuition\nCheck both strings one by ...
41
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
easy 100% solution
is-subsequence
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
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
5 Liner Solution in Java, Python,C++ ||Beats 100%||Easy To Understand
is-subsequence
1
1
***Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nAny suggestions and improvements are always welcome*.**\n___________________\n_________________\n***Q392. Is Subsequence***\n________________________________________________________________________________________________...
69
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
Python short and clean. Functional programming.
is-subsequence
0
1
# Approach for original question:\nStandard two-pointer approach.\n\n# Complexity\n- Time complexity: $$O(m + n)$$\n\n- Space complexity: $$O(1)$$\n\nwhere,\n`m is length of t`,\n`n is length of s`.\n\n# Code\nImperative multi-liner:\n```python\nclass Solution:\n def isSubsequence(self, s: str, t: str) -> bool:\n ...
1
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
C++|Easy Explanation|Beats 100%🚀|Two Pointer
is-subsequence
1
1
# Approach\n\n# -Short Explanation\nThis code efficiently checks if one string is a subsequence of another by iterating through both strings and incrementing the pointers and a counter variable as characters are matched. It returns true if s is a subsequence of t, and false otherwise.\n\n---\n\n\n# -Step By Step Explan...
2
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_. A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i...
null
393: Time 93.77%, Solution with step by step explanation
utf-8-validation
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a counter variable count to keep track of how many continuation bytes are expected.\n```\ncount = 0\n```\n2. Iterate through each integer in the input data list data.\n```\nfor num in data:\n```\n3. If the coun...
4
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
393: Time 93.77%, Solution with step by step explanation
utf-8-validation
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a counter variable count to keep track of how many continuation bytes are expected.\n```\ncount = 0\n```\n2. Iterate through each integer in the input data list data.\n```\nfor num in data:\n```\n3. If the coun...
4
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Python3 | DP | Memoization | Neat Solution | O(n)
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n n = len(data)\n l = [2**i for i in range(7, -1, -1)]\n \n def isXByteSeq(pos, X):\n f = data[pos]\n rem = data[pos+1:pos+X]\n ret = (f&l[X]) == 0\n for i in range(X):\n ...
4
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Python3 | DP | Memoization | Neat Solution | O(n)
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n n = len(data)\n l = [2**i for i in range(7, -1, -1)]\n \n def isXByteSeq(pos, X):\n f = data[pos]\n rem = data[pos+1:pos+X]\n ret = (f&l[X]) == 0\n for i in range(X):\n ...
4
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
O(N) O(N) - Naïve solution :) - No bit manipulation
utf-8-validation
0
1
A very simple solution with both time and space complexity being `O(N)`.\n\n**Approach**\n* The basic idea is that we need a binary representation of the integers so create a list of binary representations.\n* Next, we iterate through this list and check if the current binary string represents a 1-byte or a 2-byte or a...
4
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
O(N) O(N) - Naïve solution :) - No bit manipulation
utf-8-validation
0
1
A very simple solution with both time and space complexity being `O(N)`.\n\n**Approach**\n* The basic idea is that we need a binary representation of the integers so create a list of binary representations.\n* Next, we iterate through this list and check if the current binary string represents a 1-byte or a 2-byte or a...
4
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Python3 || 10 lines, binary pad, w/explanation || T/M: 95%/97%
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n \n count = 0 # Keep a tally of non-first bytes required\n \n for byte in data: # Pad out bytes to nine digits and ignore the 1st 1\n byte|= 256 ...
3
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Python3 || 10 lines, binary pad, w/explanation || T/M: 95%/97%
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n \n count = 0 # Keep a tally of non-first bytes required\n \n for byte in data: # Pad out bytes to nine digits and ignore the 1st 1\n byte|= 256 ...
3
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Simple Python Solution || O(n) Complexity
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n l = []\n \n for i in range(len(data)):\n l.append(bin(data[i])[2:])\n if(len(l[i]) < 8):\n l[i] = \'0\'*(8-len(l[i]))+l[i]\n curr = 0\n byte = 0\n flag = True\n ...
1
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Simple Python Solution || O(n) Complexity
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n l = []\n \n for i in range(len(data)):\n l.append(bin(data[i])[2:])\n if(len(l[i]) < 8):\n l[i] = \'0\'*(8-len(l[i]))+l[i]\n curr = 0\n byte = 0\n flag = True\n ...
1
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Easy python solution
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n unicode=[]\n for i in range(len(data)):\n x=bin(data[i]).replace("0b", "")\n if len(x)<8:\n x=\'0\'*(8-len(x))+x\n unicode.append(x)\n curr=None\n cont=0\n for i i...
1
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Easy python solution
utf-8-validation
0
1
```\nclass Solution:\n def validUtf8(self, data: List[int]) -> bool:\n unicode=[]\n for i in range(len(data)):\n x=bin(data[i]).replace("0b", "")\n if len(x)<8:\n x=\'0\'*(8-len(x))+x\n unicode.append(x)\n curr=None\n cont=0\n for i i...
1
Given an integer array `data` representing the data, return whether it is a valid **UTF-8** encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). A character in **UTF8** can be from **1 to 4 bytes** long, subjected to the following rules: 1. For a **1-byte** character, the first bit is a `0`...
All you have to do is follow the rules. For a given integer, obtain its binary representation in the string form and work with the rules given in the problem. An integer can either represent the start of a UTF-8 character, or a part of an existing UTF-8 character. There are two separate rules for these two scenarios in...
Python solution based on stack
decode-string
0
1
# Intuition\nThis problem is a pretty classic stack problem. You can see examples of postfix or prefix problems its just similar to that.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nstep 1: first loop over string and push until you get a closing square bracket.\nstep 2: then pop out and ap...
2
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
394. Decode String
decode-string
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
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
394: Solution with step by step explanation
decode-string
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize the stack and the current string:\n```\nstack = []\ncurr_str = ""\n```\nThe stack is used to keep track of the current number and string that are being processed. The curr_str variable will store the current st...
32
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
Beginner friendly solution using STACKS (most efficient)
decode-string
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIdea here is to use a stack.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe initialize an empty stack to store characters.\n\nWe iterate through each character in the given string.\n\nIf the current character is ...
14
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
single stack solution
decode-string
0
1
# Intuition\nsingle stack solution\n\n# Approach\ndetail in comment of code\n\n# Complexity\n- Time complexity:\nO(2N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution:\n def decodeString(self, s: str) -> str:\n stack=[]\n for char in s:\n if char != \']\':\n stack....
2
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
394. Decode String
decode-string
0
1
# Intuition\nThe algorithm iterates through `s`, keeping track of the current number and current substring using the data structure stack. It performs either a push or pop operation on the stack based on the character encountered.\n\n# Approach\n- When a digit is encountered, it is used to update the current number. \n...
1
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
CodeDominar Solution
decode-string
0
1
# Code\n```\nclass Solution:\n def decodeString(self, s: str) -> str:\n nums = "0123456789"\n stack = []\n for char in s:\n #print(stack)\n if char == \']\':\n temp_s = \'\'\n num=\'\'\n while stack[-1] != \'[\':\n ...
3
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
[ Python ] | Simple & Clean Code | Easy Sol. using Stack
decode-string
0
1
- One of the Best problems to learn how to use stack.\n# Code\n```\nclass Solution:\n def decodeString(self, s: str) -> str:\n st = []\n num = 0\n res = \'\'\n\n for ch in s:\n if ch.isnumeric():\n num = num * 10 + int(ch)\n elif ch == \'[\':\n ...
14
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
Python | Easy to Understand | Stack
decode-string
0
1
```\nclass Solution:\n def decodeString(self, s: str) -> str:\n \'\'\'\n 1.Use a stack and keep appending to the stack until you come across the first closing bracket(\']\')\n 2.When you come across the first closing bracket start popping until you encounter an opening bracket(\'[\')),basically ...
2
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
Explained Python Solution using single stack
decode-string
0
1
\nExplanation:\nSolved using stack\n1. Possible inputs are - \'[\' , \']\', alphabet(s) or numbers. Lets talk about each one by one.\n\n2. We will start for loop for traversing through each element of \'s\'. If we encounter a number, it will be handled by checking isdigit() condition. curNum10+int(c) helps in storing t...
68
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
🐍 98% faster || With and without Stack || Cleane & Concise 📌📌
decode-string
0
1
## IDEA :\n**Using stack** to store the previously stored string and the number which we have to use instantly after bracket(if any) gets closed.\n\'\'\'\n\n\tclass Solution:\n def decodeString(self, s: str) -> str:\n \n res,num = "",0\n st = []\n for c in s:\n if c.isdigit():\...
36
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
Parse with Stack | O(n) | Faster than 100% | Fluent
decode-string
0
1
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nThe problem can be seen as a stack problem because the ordering of the closed brackets and opening ones is analogous to the `LIFO` princile of stacks. \r\n\r\n![faster.PNG](https://assets.leetcode.com/users/images/48ee7572-1d6f-4ac9-a...
12
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
Using of Stacks Simple Solution..
decode-string
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasically the opening and closing brackets plays a crucial role in this problemm.. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirstly we should insert the possible elements inside of our stack till we encount...
3
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
Decode String
decode-string
0
1
```class Solution:\n def decodeString(self, s: str) -> str:\n stack = []\n curString = \'\'\n curNum = 0\n for c in s:\n if c == \'[\':\n stack.append(curString)\n stack.append(curNum)\n curString = \'\'\n curNum = 0\n...
3
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
[0ms][1LINER][100%][Fastest Solution Explained] O(n)time complexity O(n)space complexity
decode-string
1
1
\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\nThe best result for the code below is ***0ms / 3.27MB*** (beats 99.04% / 90.42%).\n\n```\n\nclass Solution {\n int i ...
2
Given an encoded string, return its decoded string. The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white...
null
DIvide and conquer method --easy to understand--
longest-substring-with-at-least-k-repeating-characters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSolve this problem by dividing the list at the point that has frequency less thn \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)$...
9
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python Short & Simple Recursive Solution
longest-substring-with-at-least-k-repeating-characters
0
1
```\nclass Solution:\n def longestSubstring(self, s: str, k: int) -> int:\n if len(s) == 0 or k > len(s):\n return 0\n c = Counter(s)\n sub1, sub2 = "", ""\n for i, letter in enumerate(s):\n if c[letter] < k:\n sub1 = self.longestSubstring(s[:i], k)\n ...
59
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
395: Solution with step by step explanation
longest-substring-with-at-least-k-repeating-characters
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. First, we check if the length of the string is less than k. If so, we can immediately return 0, since no substring of the string can have a frequency of each character greater than or equal to k.\n\n2. Next, we count the ...
12
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
python/sliding windows
longest-substring-with-at-least-k-repeating-characters
0
1
# Intuition\nits not the best solution i just went through all windows\n# Approach\nsliding window \n# Complexity\n- Time complexity:\no(n^2) \n- Space complexity:\n16mb\n# Code\n```\n# class Solution:\n# def longestSubstring(self, s: str, k: int) -> int:\n# longest=0\n# for l in range(len(s)):\n# ...
1
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python by dict and recursion [w/ Comment]
longest-substring-with-at-least-k-repeating-characters
0
1
Python sol by dictioanry and recursion\n\n---\n\n**Implementation**:\n\n```\nclass Solution:\n def longestSubstring(self, s: str, k: int) -> int:\n \n if k > len(s):\n \n # k is too large, larger than the length of s\n # Quick response for invalid k\n return ...
24
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python3 - Silly Recursion + String ops + Counter 91.92%
longest-substring-with-at-least-k-repeating-characters
0
1
# Intuition\nYou can\'t cross (read \'use\') letters appearing less than k times, so split on those letters and recurse.\n\n# Code\n```\nclass Solution:\n def longestSubstring(self, s: str, k: int) -> int:\n for ch, co in Counter(s).items():\n if co < k:\n s=s.replace(ch, \'|\')\n ...
1
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python, short and clean
longest-substring-with-at-least-k-repeating-characters
0
1
The idea is that any characters in the string that do not satisfy the requirement break the string in multiple parts that do not contain these characters, and for each part we should check the requirement again. There are similar solutions (not many), though most use string methods like split or count, which keep some ...
27
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python O(n) Sliding window Solution based on template
longest-substring-with-at-least-k-repeating-characters
0
1
\nSolved using template from https://leetcode.com/problems/minimum-window-substring/discuss/26808/here-is-a-10-line-template-that-can-solve-most-substring-problemsa\n\nTime Complexity: O(26n)\nSpace Complexity: O(26) - Store character frequency array\n\nRuntime: 280 ms\nMemory Usage: 13.6 MB - less than 99%\n\n``` pyth...
19
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
easy sliding window approach
longest-substring-with-at-least-k-repeating-characters
0
1
\tclass Solution:\n\t\tdef longestSubstring(self, s: str, k: int) -> int:\n\n\t\t\t# number of unique characters available\n\t\t\tmax_chars = len(set(s))\n\t\t\tn = len(s)\n\t\t\tans = 0\n\n\t\t\t# for all char from 1 to max_chars \n\t\t\tfor available_char in range(1,max_chars+1):\n\n\t\t\t\th = {}\n\t\t\t\ti = j = 0\...
4
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python3 + divide and conquer
longest-substring-with-at-least-k-repeating-characters
0
1
\n```\nfrom collections import Counter\n\nclass Solution:\n def longestSubstring(self, s: str, k: int) -> int:\n\t\n if len(s) < k: return 0\n\t\t\n\t\tc = Counter(s)\n st = 0\n for p, v in enumerate(s):\n \n if c[v] < k:\n \n return max(self.l...
12
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python Recursive divide and conquer beats 80%
longest-substring-with-at-least-k-repeating-characters
0
1
\n```\n\n def rec(self,s,k):\n #s += \'0\' will lead to infinite loop\n # eg "aaa0" will always be checked, s[0:3]->s[0:3] \n #so on\n hmap = defaultdict(int);\n for c in s : hmap[c]+= 1\n p ,res = -1,0\n for i in range(0,len(s)):\n if( hmap[s[i]] < k ):\n...
3
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Faster than 97.6%. Recursion
longest-substring-with-at-least-k-repeating-characters
0
1
![image](https://assets.leetcode.com/users/images/64490eea-cdda-4cce-b7ee-f48f26106e82_1643197763.2125227.png)\n\n```\nclass Solution:\n def rec(self, s, k):\n c = Counter(s)\n\n if pattern := "|".join(filter(lambda x: c[x] < k, c)):\n if arr := list(filter(lambda x: len(x) >= k, re.split(pa...
3
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python 4-lines - 44ms (51%) - Counter + Split
longest-substring-with-at-least-k-repeating-characters
0
1
How about the following solution. You can make it without splits by passing the ranges... But it looks simple and passes the test cases...\n\n```\nfrom collections import Counter\n\nclass Solution:\n def longestSubstring(self, s: str, k: int) -> int:\n for char, count in Counter(s).items():\n if co...
8
Given a string `s` and an integer `k`, return _the length of the longest substring of_ `s` _such that the frequency of each character in this substring is greater than or equal to_ `k`. **Example 1:** **Input:** s = "aaabb ", k = 3 **Output:** 3 **Explanation:** The longest substring is "aaa ", as 'a' is repeated 3...
null
Python solution || Beats - 96%
rotate-function
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
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
easy-solution | well-explained | dp | Python3 | clean-code✅
rotate-function
0
1
***Please give an upvote if you like the solution**\n\n#6Companies30days #ReviseWithArsh Challenge 2023\nDay1\nQ4. You are given an integer array nums of length n. Return maximum length of Rotation Function.*\n\n# Approach\nDynamic Programming\n\n# Complexity\n- Time complexity: O(n)\n\n# Code\n**Python3:**\n```\nclass...
2
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
✅ Mathematics || Easy to understand || Javascript || Java || Python3 || Go
rotate-function
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt\'s a math problem. I draw a graph to show the process. The code is concise. Hope it\'s helpful.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n![image.png](https://assets.leetcode.com/users/images/92ccecf6-763f...
1
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
396: Solution with step by step explanation
rotate-function
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Calculate the sum of all elements in the array, and store it in a variable "total_sum".\n2. Calculate the initial value of F(0) using a loop that iterates through the array and multiplies each element by its index, then a...
5
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
Derive formula O(n) DP python
rotate-function
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n * If we rotate by 1 elements, then it increases F[i-1] by sum of all values in the array. For example\n * F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6)\n * when we rotate by 1, what changes?\n * a...
2
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
One line solution
rotate-function
0
1
```\nclass Solution:\n def maxRotateFunction(self, nums: List[int]) -> int:\n return (s_nums := sum(nums), max(accumulate(reversed(nums), lambda s, n: s+s_nums-len(nums)*n, initial = sum(i*n for i, n in enumerate(nums)))))[1]\n```\n> More readable\n```\nclass Solution:\n def maxRotateFunction(self, nums: L...
0
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
Python3 O(N) (T < 99%)
rotate-function
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEvery rotation, each element\'s k is increased by 1, and with the highest one being reduced to 0. So we can obtain the sum for the next rotation by adding the sum of the original list (which effectively increases every k by 1), then remov...
0
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
Simple 6 line solution beats 99%, O(n), O(1)
rotate-function
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nEvery time it rotates, last value becomes a[j] * (n-1) to a[j] * 0.\nwhich makes sum -= a[j] * (n-1)\nand every other sum increases a[i]. whick makes sum += Sum(nums) - a[j]\n\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\...
0
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
PYTHON
rotate-function
0
1
```\nclass Solution:\n def maxRotateFunction(self, nums: List[int]) -> int:\n f=[]\n k=0\n for i in range(len(nums)):\n k+=i*nums[i]\n f.append(k)\n nums=nums[::-1]\n s=sum(nums)\n for i in range(len(nums)):\n nums_pop=nums[i]\n p=f[-1...
0
You are given an integer array `nums` of length `n`. Assume `arrk` to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow: * `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].` Return _the maximum value of_ `F(0), F(1), ...,...
null
397: Solution with step by step explanation
integer-replacement
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe solution uses dynamic programming and memoization to avoid repeating subproblems.\n\nThe main function, integerReplacement, initializes a memoization dictionary and calls the recursive helper function with the input n an...
6
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Easy understand python solution with explanation || Beats 91 % of the python solution.
integer-replacement
0
1
# Intuition\nAs the problem statement states that we have only two operations that we could make, In one operation where it is an even number we are allowed to reduce it n//2, But the real con=mplexity of the problem lies in the odd number where we have to **choose the minimum of the eitheir options.** So it spretty in...
2
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
SIMPLE PYTHON SOLUTION
integer-replacement
0
1
```\nclass Solution:\n def dp(self,n):\n if n<1:\n return float("infinity")\n if n==1:\n return 0\n if n%2==0:\n return self.dp(n//2)+1\n else:\n x=self.dp(n+1)+1\n y=self.dp(n-1)+1\n return min(x,y)\n def integerReplace...
2
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Simple python3 solution | 30 ms - faster than 99% solutions
integer-replacement
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## Approach\nPicture of the need for memoization\n\n![123.jpg](https://assets.leetcode.com/users/images/571c6424-1b09-463f-ad60-21f7068addd8_169798...
1
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
easy peasy python [comments] bit manipulation
integer-replacement
0
1
\tdef integerReplacement(self, n):\n # Basically, There are four cases in the trailing digits:\n # 00 01 10 11 which represents n % 4 == 0, 1, 2, 3\n # For any odd number, it has remaining of 1 or 3 after mod with 4. If it\'s 1, decrease it, if it\'s 3 increase it.\n # if last two digits are...
25
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
beats 62%
integer-replacement
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. -->\nif the next bit is 1 choose +1, else choose -1\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\...
0
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Python solution
integer-replacement
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 positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Easy Python Solution
integer-replacement
0
1
# Code\n```\nclass Solution:\n def integerReplacement(self, n: int) -> int:\n memo = {}\n def getAnswer(number, steps):\n if number == 1: return steps\n if (number, steps) in memo: return memo[(number, steps)]\n else:\n if number % 2 == 1:\n ...
0
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
iterative bit manipulation method, no recursion or DP
integer-replacement
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nlooking at binary represnation of n, it\'s pretty clear what is the shortest way to reduce it to 1, and that depend only on the last 2 elast segnificant bits\n\n01 -> reduce 1\n00 -> devide by 2\n10 -> devide by 2\n11 -> unless n is 3, is...
0
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Python 1 liner
integer-replacement
0
1
# Intuition\n There is no need of intuition , very easy problem.\n\n# Code\n```\nclass Solution:\n def integerReplacement(self, n: int) -> int:\n c = 0\n while(n != 1):\n if(n % 2 == 0):\n n //= 2\n c+=1\n else:\n if(n==3):\n ...
0
Given a positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Easy Python Solution
integer-replacement
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 positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Python Math
integer-replacement
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\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 positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
using recursion
integer-replacement
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 positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Easy solution using Recursion || python3
integer-replacement
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 positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
Using Recursion
integer-replacement
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 positive integer `n`, you can apply one of the following operations: 1. If `n` is even, replace `n` with `n / 2`. 2. If `n` is odd, replace `n` with either `n + 1` or `n - 1`. Return _the minimum number of operations needed for_ `n` _to become_ `1`. **Example 1:** **Input:** n = 8 **Output:** 3 **Explanat...
null
✅97.87%🔥python3/C/C#/C++/Java🔥Randomized Index Selection for Target Values in an Array🔥
random-pick-index
1
1
![Screenshot 2023-08-20 065922.png](https://assets.leetcode.com/users/images/2a849569-493f-4d48-bfe2-9b8fc2be5d95_1694668475.0602646.png)\n\n\n```Python3 []\nclass Solution:\n\n def __init__(self, nums: List[int]):\n self.indices = defaultdict(list)\n for i, num in enumerate(nums):\n self.in...
26
Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the array `nums`. * `int pick(int target)` Pic...
null
Beat 100%, O(1) Solution
random-pick-index
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTake the advantage of hashmap and randint O(1) attribute\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse hashmap with list to store candidate numbers, and random pick target from the target list.\n\n# Complexit...
1
Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the array `nums`. * `int pick(int target)` Pic...
null
Python 3 Reservoir Sampling, O(n) Time & O(1) Space
random-pick-index
0
1
Reservoir sampling is particularly helpful when the size of the array `nums` is unknown (e.g. when we have a **stream** of numbers).\nThe [solution](https://leetcode.com/problems/linked-list-random-node/solution/) to LC 382 (Linked List Random Node) gives a more detailed & clear explanation (and the proof) on the mathe...
6
Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the array `nums`. * `int pick(int target)` Pic...
null
Python Easy Solution Ever !!! 🔥🔥🔥 Reservoir Sampling
random-pick-index
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
Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the array `nums`. * `int pick(int target)` Pic...
null
easy peasy python reservoir sampling
random-pick-index
0
1
\tdef __init__(self, nums: List[int]):\n self.nums = nums\n\n def pick(self, target: int) -> int:\n cnt = idx = 0\n for i, num in enumerate(self.nums):\n if num != target:\n continue\n if cnt == 0:\n idx = i\n cnt = 1\n ...
13
Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the array `nums`. * `int pick(int target)` Pic...
null