mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-17 07:26:27 +00:00
refactor(work): restructure solution directories with bucket level and add .gitkeep placeholders
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
3. Longest Substring Without Repeating Characters
|
||||
Difficulty: Medium
|
||||
https://leetcode.com/problems/longest-substring-without-repeating-characters/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given a string s, find the length of the longest substring without
|
||||
duplicate characters.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: s = "abcabcbb"
|
||||
Output: 3
|
||||
Explanation: The answer is "abc", with the length of 3. Note that
|
||||
"bca" and "cab" are also correct answers.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: s = "bbbbb"
|
||||
Output: 1
|
||||
Explanation: The answer is "b", with the length of 1.
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: s = "pwwkew"
|
||||
Output: 3
|
||||
Explanation: The answer is "wke", with the length of 3.
|
||||
Notice that the answer must be a substring, "pwke" is a subsequence
|
||||
and not a substring.
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 0 <= s.length <= 10^5
|
||||
|
||||
• s consists of English letters, digits, symbols and spaces.
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def lengthOfLongestSubstring(self, s: str) -> int:
|
||||
left = 0
|
||||
ans = 0
|
||||
window = set()
|
||||
for right, c in enumerate(s):
|
||||
while c in window:
|
||||
window.remove(s[left])
|
||||
left += 1
|
||||
window.add(c)
|
||||
ans = max(ans, right - left + 1)
|
||||
|
||||
return ans
|
||||
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
424. Longest Repeating Character Replacement
|
||||
Difficulty: Medium
|
||||
https://leetcode.com/problems/longest-repeating-character-replacement/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
You are given a string s and an integer k. You can choose any
|
||||
character of the string and change it to any other uppercase English
|
||||
character. You can perform this operation at most k times.
|
||||
|
||||
Return the length of the longest substring containing the same letter
|
||||
you can get after performing the above operations.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: s = "ABAB", k = 2
|
||||
Output: 4
|
||||
Explanation: Replace the two 'A's with two 'B's or vice versa.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: s = "AABABBA", k = 1
|
||||
Output: 4
|
||||
Explanation: Replace the one 'A' in the middle with 'B' and form
|
||||
"AABBBBA".
|
||||
The substring "BBBB" has the longest repeating letters, which is 4.
|
||||
There may exists other ways to achieve this answer too.
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 1 <= s.length <= 10^5
|
||||
|
||||
• s consists of only uppercase English letters.
|
||||
|
||||
• 0 <= k <= s.length
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def characterReplacement(self, s: str, k: int) -> int:
|
||||
count = {}
|
||||
res = 0
|
||||
|
||||
l = 0
|
||||
maxF = 0
|
||||
for r, c in enumerate(s):
|
||||
count[c] = 1 + count.get(c, 0)
|
||||
maxF = max(maxF, count[c])
|
||||
|
||||
while (r - l + 1) - maxF > k:
|
||||
count[s[l]] -= 1
|
||||
l += 1
|
||||
|
||||
res = max(res, r - l + 1)
|
||||
return res
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 451. Sort Characters By Frequency
|
||||
* Difficulty: Medium
|
||||
* https://leetcode.com/problems/sort-characters-by-frequency/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given a string s, sort it in decreasing order based on the frequency
|
||||
* of the characters. The frequency of a character is the number of times
|
||||
* it appears in the string.
|
||||
*
|
||||
* Return the sorted string. If there are multiple answers, return any
|
||||
* of them.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: s = "tree"
|
||||
* Output: "eert"
|
||||
* Explanation: 'e' appears twice while 'r' and 't' both appear once.
|
||||
* So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also
|
||||
* a valid answer.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: s = "cccaaa"
|
||||
* Output: "aaaccc"
|
||||
* Explanation: Both 'c' and 'a' appear three times, so both "cccaaa"
|
||||
* and "aaaccc" are valid answers.
|
||||
* Note that "cacaca" is incorrect, as the same characters must be
|
||||
* together.
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: s = "Aabb"
|
||||
* Output: "bbAa"
|
||||
* Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
|
||||
* Note that 'A' and 'a' are treated as two different characters.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= s.length <= 5 * 10^5
|
||||
*
|
||||
* • s consists of uppercase and lowercase English letters and digits.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {string}
|
||||
*/
|
||||
var frequencySort = function (s) {
|
||||
// count the frequency of each character
|
||||
const freq = {};
|
||||
for (let c of s) freq[c] = (freq[c] || 0) + 1;
|
||||
|
||||
// sort the characters by frequency
|
||||
return s
|
||||
.split("")
|
||||
.sort((a, b) => freq[b] - freq[a] || a.localeCompare(b))
|
||||
.join("");
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
567. Permutation in String
|
||||
Difficulty: Medium
|
||||
https://leetcode.com/problems/permutation-in-string/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given two strings s1 and s2, return true if s2 contains a permutation
|
||||
of s1, or false otherwise.
|
||||
|
||||
In other words, return true if one of s1's permutations is the
|
||||
substring of s2.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: s1 = "ab", s2 = "eidbaooo"
|
||||
Output: true
|
||||
Explanation: s2 contains one permutation of s1 ("ba").
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: s1 = "ab", s2 = "eidboaoo"
|
||||
Output: false
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 1 <= s1.length, s2.length <= 10^4
|
||||
|
||||
• s1 and s2 consist of lowercase English letters.
|
||||
"""
|
||||
|
||||
from collections import Counter
|
||||
|
||||
|
||||
class Solution:
|
||||
def checkInclusion(self, s1: str, s2: str) -> bool:
|
||||
k = len(s1)
|
||||
if k > len(s2):
|
||||
return False
|
||||
|
||||
need = Counter(s1)
|
||||
window = Counter()
|
||||
|
||||
for right, c in enumerate(s2):
|
||||
window[c] += 1
|
||||
if right >= k:
|
||||
left_char = s2[right - k]
|
||||
window[left_char] -= 1
|
||||
if window[left_char] == 0:
|
||||
del window[left_char]
|
||||
if window == need:
|
||||
return True
|
||||
|
||||
return False
|
||||
Reference in New Issue
Block a user