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
Easiest python solution, faster than 95%
ransom-note
0
1
```\nclass Solution:\n def canConstruct(self, ransomNote, magazine):\n for i in set(ransomNote):\n if magazine.count(i) < ransomNote.count(i):\n return False\n return True\n
50
Given two strings `ransomNote` and `magazine`, return `true` _if_ `ransomNote` _can be constructed by using the letters from_ `magazine` _and_ `false` _otherwise_. Each letter in `magazine` can only be used once in `ransomNote`. **Example 1:** **Input:** ransomNote = "a", magazine = "b" **Output:** false **Example ...
null
easy and clean Python3 code
ransom-note
0
1
# Code\n```\nclass Solution:\n def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n for charr in set(ransomNote):\n if ransomNote.count(charr)>magazine.count(charr):return False\n return True\n```
3
Given two strings `ransomNote` and `magazine`, return `true` _if_ `ransomNote` _can be constructed by using the letters from_ `magazine` _and_ `false` _otherwise_. Each letter in `magazine` can only be used once in `ransomNote`. **Example 1:** **Input:** ransomNote = "a", magazine = "b" **Output:** false **Example ...
null
384: Solution with step by step explanation
shuffle-an-array
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- In the __init__ method, we initialize two instance variables: self.nums and self.original. self.nums is the input array, and self.original is a copy of it. We create the copy so that we can reset the array later.\n\n- In t...
5
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
Python code | Reset without storing the original array
shuffle-an-array
0
1
\n```\nRead a lot a solution, but everywhere the original array\nis stored seperately and returned when reset is called.\n\nI tried to reset the array we suffled,\nhowever many times we shuffle it, below is the \naccepted solution, it is slow, beats only 10% but it \nis the full solution in my view.\n```\n```\nclass So...
2
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
Beats 72.88% of Python3 with Explanation
shuffle-an-array
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n- In the `__init__` method we will create two instances one is `self.nums` which is input Array given to us and second is `self.original` which is copy of original input array.\n\n- In the reset method we will just return the `self.original` which is...
1
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
{ Python } | Best Optimal Approach ✔
shuffle-an-array
0
1
\tclass Solution:\n\t\tdef __init__(self, nums: List[int]):\n\t\t\tself.arr = nums[:] # Deep Copy, Can also use Shallow Copy concept!\n\t\t\t# self.arr = nums # Shallow Copy would be something like this!\n\n\t\tdef reset(self) -> List[int]:\n\t\t\treturn self.arr\n\n\t\tdef shuffle(self) -> List[int]:\n\t\t\tans = se...
9
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
Python
shuffle-an-array
0
1
# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(1)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(n)\n# Code\n```\nclass Solution:\n def __init__(self, nums: List[int]):\n self.original = nums\n self.nums = nums.copy()\...
0
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
Fisher-Yates shuffle algorithm; Runtime 134 ms - TC beats 72.98%
shuffle-an-array
1
1
# Code\n```python\nimport random\nclass Solution:\n\n def __init__(self, nums: List[int]):\n self.nums=nums\n self.original=nums.copy()\n self.n=len(nums)\n\n def shuffle_list(self,lst):\n \n #Fisher-Yates shuffle algorithm\n for i in range(self.n - 1, 0, -1):\n ...
0
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
Easy solution using random.shuffle()
shuffle-an-array
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 integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
python3 random
shuffle-an-array
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 integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
Simple solution without using random (99.92%)
shuffle-an-array
0
1
# Code\n```\n# by ilotoki0804\n\nfrom itertools import permutations\n\nclass Solution:\n def __init__(self, nums: List[int]):\n self.numbers = nums\n self.permutated_iter = permutations(nums)\n\n def reset(self) -> List[int]:\n return self.numbers\n\n def shuffle(self) -> List[int]:\n ...
0
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
Simplest sol using random for shuflling
shuffle-an-array
0
1
# Complexity\n- Time complexity: O(n)\n\n# Code\n```\nclass Solution:\n\n def __init__(self, nums: List[int]):\n self.nums = nums\n self.original = list(nums)\n \n\n def reset(self) -> List[int]:\n self.nums = list(self.original)\n return self.nums\n \n\n def shuffle(s...
0
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling. Implement the `Solution` class: * `Solution(int[] nums)` Initializes the object with the integer array `nums`. * `int[] reset()` Resets the arr...
The solution expects that we always use the original array to shuffle() else some of the test cases fail. (Credits; @snehasingh31)
385: Time 92.59% and Space 97.53%, Solution with step by step explanation
mini-parser
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We start by checking if the input string s starts with a [. If it doesn\'t, then it must be a single integer, so we create a NestedInteger object with that value and return it.\n\n```\nif s[0] != \'[\':\n return NestedIn...
9
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
Python Solution just 6 lines || Easy to understand approach || Using eval
mini-parser
0
1
```\nclass Solution:\n def deserialize(self, s: str) -> NestedInteger:\n\t\t# eval("[1,2,3]") => [1,2,3] it\'ll convert string of list to list\n return self.find(eval(s))\n \n def find(self,s):\n\t\t# if our elment s is type of int we\'ll return new NestedInteger of that value\n if type(s) == typ...
4
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
used stack, beats 62.45%, 65%
mini-parser
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)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n...
0
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
Python || Clean Recursive Solution
mini-parser
0
1
```\nclass Solution:\n def deserialize(self, s: str) -> NestedInteger:\n s = \'[\' + s + \']\'\n\n n = len(s)\n\n def parse(i: int) -> tuple[int, NestedInteger]:\n out = NestedInteger()\n num = \'\'\n\n while i < n and s[i] != \']\':\n if s[i].isdi...
0
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
Beats 100.00% of users with Python3
mini-parser
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nWhen we encounter an opening bracket [, we create a new NestedInteger and push it onto a stack.\n\nWhen we encounter a closing bracket ], we finish constructing the current NestedInteger and pop it from the stack.\n\nWhile processing numbers and comma...
0
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
Python code along with Algorithm
mini-parser
0
1
# Approach\nUsing Stack and DFS(Depth-first search)\n\nSince the input is a string, which is not much helpful for our stack approach\nFirst we will convert the string into an array with the following condition:\n- The $$]$$, $$[$$, and the integers (represented as string) should be added as a sepearte element to the ar...
0
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
Solution
mini-parser
1
1
```C++ []\nclass Solution {\nprivate:\n NestedInteger dfs(string& s, int& i) {\n NestedInteger res;\n int n(s.size());\n for ( ;i < n; ++i) {\n if (s[i] == \',\') continue;\n if (s[i] == \'[\') res.add(dfs(s,++i));\n else if (s[i]==\']\') break;\n else...
0
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
O(n) SOlution
mini-parser
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea for this problem is to use a recursive approach to deserialize the string representation of a nested integer. \n# Approach\n<!-- Describe your approach to solving the problem. -->\n- If the input string is empty, return an empty ...
0
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
python solution with index-memoization
mini-parser
0
1
## What\'s special:\n\n- Manipulate index is cheaper (regarding time&space complexity) than substring\n- Recur between `[]` but creating a `ci` (closing index) to quickly find where the `[]` ends and jump to that index \n\n## Code\n\n```python\nclass Solution:\n s: str\n ci: dict[int, int] # V is ind of close pa...
0
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
[Python3] a concise recursive solution
mini-parser
0
1
\n```\nclass Solution:\n def deserialize(self, s: str) -> NestedInteger:\n if not s: return NestedInteger()\n if not s.startswith("["): return NestedInteger(int(s)) # integer \n ans = NestedInteger()\n s = s[1:-1] # strip outer "[" and "]"\n if s: \n ii = op = 0 \n ...
2
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
Elegant Python One Pass Solution (using stack)
mini-parser
0
1
```\nclass Solution:\n def deserialize(self, s: str) -> NestedInteger:\n stack = []\n integerStr = \'\'\n \n for c in s:\n if c == \'[\':\n stack.append(NestedInteger())\n elif c == \']\':\n if len(integerStr)>0:\n sta...
1
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`. Each element is either an integer or a list whose elements may also be integers or other lists. **Example 1:** **Input:** s = "324 " **Output:** 324 **Explanation:** Yo...
null
Elegent, intuitive recursive O(n) solution
lexicographical-numbers
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nI use pre-order traversal and treat this like a DFS problem, where we first add 1, then it\'s *10, and if N < value * 10, then we move on to adding + 1.\n\n# Complexity\n- Time complexity:\n O(N), goes through each number...
1
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
386: Time 99.87% and Space 93.99%, Solution with step by step explanation
lexicographical-numbers
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize an empty list res to store the result and a variable cur to start from 1.\n```\nres = []\ncur = 1\n```\n2. Loop through the range n times, where n is the upper limit of the numbers to generate.\n```\nfor i in r...
12
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Easy | Python3 Solution
lexicographical-numbers
0
1
\n```\nclass Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n lst=[str(i) for i in range(1,n+1)]\n lst.sort()\n return [int(i) for i in lst]\n \n```
3
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Python 3 - Simple DFS with Explanation
lexicographical-numbers
0
1
# Intuition\nWhenever any lexicographical solution is asked, the first thing that should come to your mind should be DFS on Trees.\n\n(A more [flashy one liner](https://leetcode.com/problems/lexicographical-numbers/solutions/3773772/python-3-disgusting-1-liner-use-at-end-of-interview/))\n# Approach\nWe start from the d...
1
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Easy Python Code
lexicographical-numbers
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ncreating a list of string(i) for integers i in range(1,n)\nsorting them in lexicographical order.\nconverting the list of strings into list of integers \nreturning the list\n\n# Approach\n<!-- Describe your approach to solving the proble...
1
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Easy Solution O(N) Time O(1) Space || Beats 90% || Examples and Thought Process
lexicographical-numbers
0
1
To solve this problem, it\u2019s less about data structure knowledge and applying them, it\u2019s more about understanding what is \u201Clexicographical order?\u201D If you do understand what that means the problem becomes easy.\n\n## What is lexicographical order?\n\nPeople online say it\u2019s like alphabetical order...
3
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Python oneliner
lexicographical-numbers
0
1
```\nclass Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n return sorted([x for x in range(1,n+1)],key=lambda x: str(x))\n```
2
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
DFS
lexicographical-numbers
0
1
# Intuition\nWe use DFS to traverse the tree of all possible numbers. We start with 1, and then we try to append 0-9 to the end of it. If the number is less than or equal to N, we append it to the result and then recursively call the function on it. We do this for all numbers from 1 to 9.\n\n# Approach\n - Initializ...
0
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
merged sorted lists
lexicographical-numbers
0
1
Not the fastest solution (actually it\'s like 7% percentile or something) but I think it\'s an easy solution to think about. \n\nWe can think of the final result as a merged list of all the lists of numbers with the same number of digits:\n```\none = [1, 2, 3, 4, 5, 6, 7, 8, 9]\ntwo = [100, 101, 102, 103, ...]\nthree =...
0
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Python, runtime O(n), memory O(1)
lexicographical-numbers
0
1
```\nclass Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n ans = [1]\n stack = [1]\n cur = 1\n while stack:\n if cur*10 <= n:\n stack.append(0)\n cur = cur*10\n elif cur+1 <= n and stack[-1] < 9:\n stack.append(...
0
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Python Solution
lexicographical-numbers
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 integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
one liner python code
lexicographical-numbers
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 integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
1-liner python
lexicographical-numbers
0
1
# Intuition\nsorted will use lexicographical sort order for string\n\n# Code\n```\nclass Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n return sorted([a for a in range(1,n+1)], key=lambda x: str(x))\n```
0
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Python DFS
lexicographical-numbers
0
1
# Approach\nDFS\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1) without counting return array\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n res...
0
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Python| Very simple solutions!!
lexicographical-numbers
0
1
# Intuition\nThis can be easly done by follwing Basic list concept(s)\n\n# Approach\nFollow Fundamentals for Array/List\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n ranging = [list(str(x)) for x in range(1...
0
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Most Easiest Solution in Python>>>Two line code......(^_^)
lexicographical-numbers
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(n)\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 integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space. **Example 1:** **Input:** n = 13 **Output:** \[1,10,11,12,13,2,3,4,5,6,7,8,9\] **Example 2:** **Input:** n = 2 **Output:** \[1,2\] *...
null
Simple solution with using Queue and HashTable DS
first-unique-character-in-a-string
0
1
# Intuition\nThe description of task requires to find **the first unique character** in string, that has **no repetitions**.\n\n---\n\n1. **Naive brut force**:\n\nIterate over all chars in string => check the **frequency** of a current count, and compare like `freq = cache[s[i]]; freq == 1`\n\nThis will lead us to **O(...
1
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
simple solution for beginners
first-unique-character-in-a-string
0
1
\n\n# Code\n```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n for i in range(len(s)):\n if s.count(s[i])==1:\n return i\n return -1\n\n```
2
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
5 Lines of Code in Two Methods
first-unique-character-in-a-string
0
1
\n\n# 1. Count the first occurence one and break loop\n```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n for i,c in enumerate(s):\n if s.count(c)==1:\n return i\n break\n return -1\n #please upvote me it would encourage me alot\n\n```\n# 2. Usi...
43
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Very Easy || 100% || Fully Explained || Java, C++, Python, JavaScript, Python3
first-unique-character-in-a-string
1
1
# **Java Solution:**\n```\nclass Solution {\n public int firstUniqChar(String s) {\n // Base case...\n if(s.length() == 0) return -1;\n // To keep track of the count of each character, we initialize an int[]store with size 26...\n int[] store = new int[26];\n // Traverse string to...
73
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Python || 3 diff way || 99% beats
first-unique-character-in-a-string
0
1
**If you got help from this,... Plz Upvote .. it encourage me**\n# Code\n> # HashMap\n```\n# HASHMAP\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n d = {}\n for char in s:\n d[char] = d.get(char,0) + 1\n \n for i , char in enumerate(s):\n if d[char] == ...
9
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Python Easy Solution || Hashmap
first-unique-character-in-a-string
0
1
# Code\n```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n dic=dict()\n for i in s:\n if i in dic:\n dic[i]+=1\n else:\n dic[i]=1\n for i in dic:\n if dic[i]==1:\n return s.index(i)\n return -1\n ...
1
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Python3 ✅✅✅|| Faster than 93.83% ⏭ || Memory beats 83.08% 🧠
first-unique-character-in-a-string
0
1
![image.png](https://assets.leetcode.com/users/images/64e1d520-a174-4115-b439-7f19741a1fe6_1693420934.7659862.png)\n\n\n![image.png](https://assets.leetcode.com/users/images/d05dd9a1-ee81-4cf7-98c6-d7b13b322fe7_1693420925.0015662.png)\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. get freq...
1
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
[ Python ] ✅✅ Simple Python Solution With Two Approach 🥳✌👍
first-unique-character-in-a-string
0
1
# If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Approach 1: - Iterative \n\tclass Solution:\n\t\tdef firstUniqChar(self, s: str) -> int:\n\n\t\t\tfor i in range(len(s)):\n\n\t\t\t\tif s[i] not in s[:i] and s[i] not in s[i+1:]:\n\n\t\t\t\t\treturn i\n\n\t\t\treturn -1\n...
48
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Python, using Counter
first-unique-character-in-a-string
0
1
```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n counter = Counter(s)\n for i, c in enumerate(s):\n if counter[c] == 1:\n return i\n \n return -1\n```
4
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Simple Python Solution with hashmap
first-unique-character-in-a-string
0
1
```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n hashmap = {}\n for c in s:\n hashmap[c] = hashmap.get(c, 0) + 1\n \n for i, c in enumerate(s):\n if hashmap[c] == 1:\n return i\n \n return -1\n\nTime: O(n)\nSpace: O(n)\n``...
3
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Easy Python solution
first-unique-character-in-a-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 a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Beats : 54.37% [36/145 Top Interview Question]
first-unique-character-in-a-string
0
1
# Intuition\n*my intuition would be to iterate through the string and for each character, check if it appears only once in the string. If so, return its index. If no such character is found, return -1. This would involve two nested loops, resulting in a time complexity of O(n^2), where n is the length of the input stri...
2
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
3 Line code Python3 easy and neat solution
first-unique-character-in-a-string
0
1
# Code\n```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n for i,j in(Counter(s).items()):\n if j == 1: return s.index(i)\n return -1\n```
3
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Easy and simple || First Unique Character in a String
first-unique-character-in-a-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)$$ --...
3
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Best PYTHON Solution beats 99%!!!!
first-unique-character-in-a-string
0
1
The best easy solution beats 99%.\nPlease upvote if you like it.\n\n# Code\n```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n if 1 not in Counter(s).values():\n return -1\n for i,j in Counter(s).items():\n if j==1:\n return s.index(i)\n\n```
4
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Beats 90% | CodeDominar Solution
first-unique-character-in-a-string
0
1
# Code\n```\nclass Solution:\n def firstUniqChar(self, s: str) -> int:\n d=dict()\n for c in s:\n d[c] = d.get(c,0)+1\n for i,c in enumerate(s):\n if d[c] == 1:\n return i\n return -1\n```
4
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`. **Example 1:** **Input:** s = "leetcode" **Output:** 0 **Example 2:** **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** **Input:** s = "aabb" **Output:** -1 **Constraints:** * `...
null
Python easy stack
longest-absolute-file-path
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nkeep track longest range\nassume level = 1,2,3,1,2,3,5,1,1\nex. 1 => 1,2 => 1,2,3 => 1 => 1,2 => 1,2,3 => 1,2,3,5 => 1 => 1\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!--...
1
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
388: Time 100%, Solution with step by step explanation
longest-absolute-file-path
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. First, we split the input string s into individual paths using the newline character \\n as the delimiter. We store these paths in the paths list.\n\n2. Next, we initialize a stack with a single value of 0. We use this to...
9
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Python 3 | Stack | Explanation
longest-absolute-file-path
0
1
### Intuition\n- Directories has a hierarchy type of relation, so we can use stack to simulate the process.\n### Explanations\n- For each dir or file, we store 3 things in stack\n\t- current total length (including parent and \'/\'), depth (how many \'\\t\' to reach this subdir)\n- If `stack` is empty, we add new tuple...
27
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Python | Stack | 9 Lines | Simple
longest-absolute-file-path
0
1
```\nclass Solution:\n def lengthLongestPath(self, input: str) -> int:\n input = input.split(\'\\n\')\n path, ans, total = [], 0, 0\n for line in input:\n tabs = line.count(\'\\t\')\n while len(path) > tabs: total -= path.pop()\n path.append(len(line) - tabs)\n ...
2
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Easiest solution with full explanation : )
longest-absolute-file-path
1
1
\n### Problem Explanation:\n\nImagine a file system that stores both files and directories, represented in a textual format. Directories may contain subdirectories and files. The goal is to find the length of the longest absolute path to a file in the file system.\n\n### File System Representation:\n\nThe file system i...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Simple Solution Using Stack
longest-absolute-file-path
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
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Intuitive Python Solution: Stack-based Approach
longest-absolute-file-path
0
1
# Approach\nUse a stack to store the current path. Length of stack represents the current level of nesting.\n\nPop off the top of the stack while the level of nesting is greater than the current component\'s level of nesting (indicated by the number of tabs).\n\n# Complexity\nTime complexity: O(n)\nSpace complexity: O(...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Python3 Solution
longest-absolute-file-path
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt uses a stack t keep track of the current layer depth and the size of the path.\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)$$ ...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Python Iterative DFS Solution | 91.52% Runtime
longest-absolute-file-path
0
1
```\nclass Solution:\n def lengthLongestPath(self, input: str) -> int:\n \n new_input = input.split("\\n")\n input_levels = []\n \n # Get level of each file/dir\n for d in new_input:\n input_levels.append(len(d.split("\\t")) - 1)\n\n max_len = 0\n sta...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
(Python3) 30ms Beats 96.49 - Readable code - Monotonic stack
longest-absolute-file-path
0
1
# Code\n```\nEntry = namedtuple("Entry", ["line", "depth", "length"])\n\n\nclass Solution:\n def lengthLongestPath(self, input: str) -> int:\n result = 0\n stack = []\n\n def consume(depth):\n if not stack or depth > stack[-1].depth:\n return\n\n name, _, len...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Python with Tuple Stack | O(N)
longest-absolute-file-path
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initialization**\n - Initialize `maxLen` to 0 to store the length of the longest path to a file.\n - Initialize `stack` with a dummy root tuple `(0, 0)` to facilitate length calculations.\n\n2. **Split the Input**\n - Split the input str...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Python3 approach (beats 88%)
longest-absolute-file-path
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Split the input into a list of paths based on the line breaks.\n2. Set three variables, path, slashes, and max_len. Path will initially be set to the first file/directory in the list, and will store the current path. Slashes will be empty at first,...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Monotonic Stack | With the detailed understanding | Python Easiest Solution
longest-absolute-file-path
0
1
\n# Approach\nI have solved it through monotonic stack. \n\nFirstly, seeking for the new line character to check if our extension file is in the further subfolder or not. For this, I have written a check \n```\nif input[i] == "\\n":\n if "\\t" * len(self.stack) == input[i + 1 : i + 1 + len(self.stack)]:\n i +...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
[Python3] [Stack] Simple but fast with Stack
longest-absolute-file-path
0
1
# Code\n```\nclass Solution:\n def lengthLongestPath(self, input: str) -> int:\n lines = input.split(\'\\n\')\n s = []\n total_names_length = 0\n max_length = 0\n current_level = 0\n for line_index in range(len(lines)):\n current_line = lines[line_index]\n ...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Solution
longest-absolute-file-path
1
1
```C++ []\nclass Solution {\npublic:\n int lengthLongestPath(string str) {\n str.push_back(\'\\n\');\n vector<int> dp = {0};\n int res = 0;\n for (int i = 0; i < str.size(); ++i) {\n int t = 0;\n while (str[i] == \'\\t\') {\n ++t;\n ++i;...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
[Python] Count the number of '\t'; Explained
longest-absolute-file-path
0
1
We can count the number of \'\\t\' in each file/folder string.\nThe number of \'\\t\' can represent the level or depth of a folder or file\n\nFor example,\n`\'dir\'`, has 0 \'\\t\' ,and it is on level 0\n`\'\\tsubdir1\'`, has 1 \'\\t\', and it is on level 1\n\nBased on the folder level (i.e., the count of \'\\t\') we c...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
O(n) Solution
longest-absolute-file-path
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea for this problem is to use a dictionary to store the length of the current path and a variable to store the maximum length of a file path.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo do this, we can u...
0
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`....
null
Python 3 || Sorting
find-the-difference
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
[Py , Java , C++ , C# , JS] ✅ MAP |🔥100%
find-the-difference
1
1
```python []\nclass Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n char_count = {}\n \n # Count characters in string t\n for char in t:\n if char in char_count:\n char_count[char] += 1\n else:\n char_count[char] = 1\n ...
1
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
✅94.54%🔥Efficient Algorithms for Finding the Added Letter in a Shuffled String🔥
find-the-difference
1
1
# Problem\n\n#### The problem you\'re describing is known as "Find the Difference." You are given two strings, s and t, with the following characteristics:\n##### 1. String t is generated by randomly shuffling string s.\n##### 2.An additional letter is added to t at a random position.\n#### You need to find and return ...
25
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Simple Python Solution O(n^2)
find-the-difference
0
1
\n\n# Code\n```\nclass Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n l1=list(s)\n l2=list(t)\n for i in l2:\n if i in l1:\n l1.remove(i)\n else:\n return i \n```
0
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
【Video】How we think about a solution - Python, JavaScript, Java, C++
find-the-difference
1
1
Welcome to my article! This artcle starts with "How we 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\nCo...
30
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Python | easy solution
find-the-difference
0
1
\n\n# Code\n```\nclass Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n ht={}\n hs={}\n for char in s:\n hs[char]=hs.get(char,0)+1\n for char in t:\n ht[char]=ht.get(char,0)+1\n for char in ht:\n if char not in hs or ht[char] > hs[c...
1
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Python shortest 1-liner. Functional programming.
find-the-difference
0
1
# Approach\nSimilar to [136. Single Number](https://leetcode.com/problems/single-number/description/) and it\'s [solution](https://leetcode.com/problems/single-number/solutions/3215764/python-shortest-1-liner-functional-programming/).\n\n# Complexity\n- Time complexity: $$O(m + n)$$\n\n- Space complexity: $$O(1)$$\n\n#...
1
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
✅96.54%🔥Efficient Algorithms for Finding the Added Letter in a Shuffled String🔥
find-the-difference
1
1
# Problem\n\n#### The problem you\'re describing is known as "Find the Difference." You are given two strings, s and t, with the following characteristics:\n##### 1. String t is generated by randomly shuffling string s.\n##### 2.An additional letter is added to t at a random position.\n#### You need to find and return ...
5
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Python Easy Bitwise Solution - 25/09/2023
find-the-difference
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
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Python3 with hash map
find-the-difference
0
1
I used Counter from collections method to make the hash map.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n**O**(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**O**(n+m)\nWhere, n = length of string **t**\nm = length of string **s**\n\n#...
0
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Easy to understand using single for loop (PYTHON 3)
find-the-difference
0
1
\n\n# Code\n```\nclass Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n for x in t:\n if x not in s:\n return x \n s=s.replace(x,"",1)\n```
1
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
✔Best Bit Manipulation 📈|| Easy to understand ✨|| #Beginner😇😎
find-the-difference
1
1
# Intuition\n- The code aims to find the extra character (the difference) that is present in string `t `but not in string `s`. It does this by XORing all the characters in both strings, and the result will be the extra character.\n\n# Approach\n1. Initialize a variable `r` to `0`. This variable will be used to store th...
14
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
✔Bit Manipulation 📈|| Easy to understand ✨|| #Beginner😇😎
find-the-difference
1
1
# Intuition\n- The code aims to find the extra character (the difference) that is present in string `t `but not in string `s`. It does this by XORing all the characters in both strings, and the result will be the extra character.\n\n# Approach\n1. Initialize a variable `r` to `0`. This variable will be used to store th...
5
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
C++/Python frequencies vs XOR vs Sum||0ms Beats 100%||Math explains
find-the-difference
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCount the frequencies of alphabets in s & t. find the different alphbet.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n[Please turn English subtitles if neccessary]\n[https://youtu.be/79AP8uMpO5E?si=Ty4FAbc05I00yYE...
7
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Beats 99.15% of Solution- Find the Difference
find-the-difference
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
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Easy Python Solution
find-the-difference
0
1
```\ndef findTheDifference(self, s: str, t: str) -> str:\n for i in t:\n if s.count(i) != t.count(i):\n return i\n```
72
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
NOOB CODE :
find-the-difference
0
1
# Intuition\nCounting the no. of each char\n\n# Approach\nFirst, the function checks if the length of string s is 0. If it is, it returns the entire string t. This is because, in this case, there are no characters in s to compare with t, so the extra character in t is the difference.\n\nTwo empty lists ls and lt are in...
3
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Using Counter,Sorting,Count Approaches
find-the-difference
0
1
# 1. Using Sort Algorithm\n```\nclass Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n \ts, t = sorted(s), sorted(t)\n \tfor i in range(len(t))\n \t\tif t[i] not in s[i]:\n\t\t\t\treturn t[i]\n```\n\n# 2. Using Count\n```\nclass Solution:\n def findTheDifference(self, s: str, t: str) ->...
11
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Easy For Python Beginners
find-the-difference
0
1
\n\n# Approach\n1.iterating Over t \n2.if count of the character in t not equal to it\'s count in s\n "THEN WE CAN RETURN THAT character"\n\n\n# Code\n```\nclass Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n if len(s)==0:\n return t\n\n for i in t:\n if s.cou...
2
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
Friendly approach for beginners : time complexity O(n)
find-the-difference
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(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. $...
2
You are given two strings `s` and `t`. String `t` is generated by random shuffling string `s` and then add one more letter at a random position. Return the letter that was added to `t`. **Example 1:** **Input:** s = "abcd ", t = "abcde " **Output:** "e " **Explanation:** 'e' is the letter that was added. **Exam...
null
390: Time 92.4%, Solution with step by step explanation
elimination-game
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize variables:\n\n - left as True to indicate that we start with left-to-right elimination\n - remaining as n to keep track of the remaining numbers\n - step as 1 to keep track of the step size\n - head ...
7
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
Python O(log(n)) solution
elimination-game
0
1
# Intuition\nAt each step we remove half of the numbers so in total we will do `log(n)` step.\n\n# Approach\nIf at level `i` we have the numbers of the form: `2 ^ i * k + m` then at the level `i + 1` we will have numbers of the form: `2 ^ (i + 1) * k + m` or `2 ^ (i + 1) * k + 2 ^ i + m` depending on the removed number...
2
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
Python > 95% Time, O(logN) and Constant Memory, Commented and Explained
elimination-game
0
1
```\nclass Solution:\n def lastRemaining(self, n: int) -> int:\n N = n # number of remaining numbers\n fwd = True # flag for forward/backward elimination\n m = 2 # elimination step/interval\n s = 0 # elimination base\n\n while N > 1:\n if fwd or N % 2 == 1: \n ...
11
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
Easiest solution with full explanation
elimination-game
0
1
\n### Problem Explanation:\n\nYou have a list `arr` of all integers in the range [1, n] sorted in strictly increasing order. The task is to apply a specific algorithm on `arr` that involves removing numbers alternately from left to right and right to left until only one number remains. The goal is to find and return th...
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
Intuition: Just observe starting element 😉
elimination-game
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n Head changes when we go from (left to right) or (right to left when we have odd number of digits remaining) otherwise it remains same.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n Just track starting ele...
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
2 pointers. beats 88%, 55%
elimination-game
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nUsed 2 pointers keep track of the starting and the ending point. and moves one pointer according to the gap between start and end.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(logn)\n\n- Space complexit...
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
Easy Way to Think (Python)
elimination-game
0
1
\nKeep tracking the first element.\nIf ```from_left``` is ```True``` or the number of the list is odd. Then we should update the first element.\n```gap``` is the number we should add to make the first element to the second element in the current list.\n```n``` is the number of element in the list.\n\n# Code\n```python\...
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
Easy explanation | Arithmetic progression | Math | Python | Recursion
elimination-game
0
1
# Approach\nAt each step we will use formula of `nth` term of AP. `n` will be half of what prevous `n` was. This is because for each iteration we are eliminating the 1st number. So after emimination process, half the elements will remain. \nFor example:\n```\n1st iteration: [1,2,3,4]\n2nd iteration: [2,4]\n```\nThis is...
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