diff --git a/1) 2 sum (easy) b/1) 2 sum (easy) deleted file mode 100644 index 8b13789..0000000 --- a/1) 2 sum (easy) +++ /dev/null @@ -1 +0,0 @@ - diff --git a/2) Add two numbers (medium) b/2) Add two numbers (medium) deleted file mode 100644 index 8b13789..0000000 --- a/2) Add two numbers (medium) +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Daily Challenge/2390. Removing Stars From a String/README.txt b/Daily Challenge/2390. Removing Stars From a String/README.txt new file mode 100644 index 0000000..490abe2 --- /dev/null +++ b/Daily Challenge/2390. Removing Stars From a String/README.txt @@ -0,0 +1,37 @@ +2390. Removing Stars From a String(Medium) + +You are given a string s, which contains stars *. + +In one operation, you can: + +Choose a star in s. +Remove the closest non-star character to its left, as well as remove the star itself. +Return the string after all stars have been removed. + +Note: + +The input will be generated such that the operation is always possible. +It can be shown that the resulting string will always be unique. + + +Example 1: + +Input: s = "leet**cod*e" +Output: "lecoe" +Explanation: Performing the removals from left to right: +- The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e". +- The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e". +- The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe". +There are no more stars, so we return "lecoe". +Example 2: + +Input: s = "erase*****" +Output: "" +Explanation: The entire string is removed, so we return an empty string. + + +Constraints: + +1 <= s.length <= 105 +s consists of lowercase English letters and stars *. +The operation above can be performed on s. diff --git a/Daily Challenge/2390. Removing Stars From a String/Sol.py b/Daily Challenge/2390. Removing Stars From a String/Sol.py new file mode 100644 index 0000000..6cbf6d5 --- /dev/null +++ b/Daily Challenge/2390. Removing Stars From a String/Sol.py @@ -0,0 +1,11 @@ +class Solution: + def removeStars(self, s: str) -> str: + st = [] + for i in s: + if i == '*' and len(st)>0: + st.pop() + else: + st.append(i) + + ans = ''.join(st) + return ans \ No newline at end of file diff --git a/Daily Challenge/516. Longest Palindromic Subsequence/README.txt b/Daily Challenge/516. Longest Palindromic Subsequence/README.txt new file mode 100644 index 0000000..a58a966 --- /dev/null +++ b/Daily Challenge/516. Longest Palindromic Subsequence/README.txt @@ -0,0 +1,24 @@ +516. Longest Palindromic Subsequence(Medium) + +Given a string s, find the longest palindromic subsequence's length in s. + +A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. + + + +Example 1: + +Input: s = "bbbab" +Output: 4 +Explanation: One possible longest palindromic subsequence is "bbbb". +Example 2: + +Input: s = "cbbd" +Output: 2 +Explanation: One possible longest palindromic subsequence is "bb". + + +Constraints: + +1 <= s.length <= 1000 +s consists only of lowercase English letters. diff --git a/Daily Challenge/516. Longest Palindromic Subsequence/sol.py b/Daily Challenge/516. Longest Palindromic Subsequence/sol.py new file mode 100644 index 0000000..c10fd0b --- /dev/null +++ b/Daily Challenge/516. Longest Palindromic Subsequence/sol.py @@ -0,0 +1,37 @@ +# 1) Top down + +class Solution: + def longestPalindromeSubseq(self, s: str) -> int: + # dp(i, j) := LPS's length in s[i..j] + @functools.lru_cache(None) + def dp(i: int, j: int) -> int: + if i > j: + return 0 + if i == j: + return 1 + if s[i] == s[j]: + return 2 + dp(i + 1, j - 1) + return max(dp(i + 1, j), dp(i, j - 1)) + + return dp(0, len(s) - 1) + +# 2) Bottom up + +class Solution: + def longestPalindromeSubseq(self, s: str) -> int: + n = len(s) + # dp[i][j] := LPS's length in s[i..j] + dp = [[0] * n for _ in range(n)] + + for i in range(n): + dp[i][i] = 1 + + for d in range(1, n): + for i in range(n - d): + j = i + d + if s[i] == s[j]: + dp[i][j] = 2 + dp[i + 1][j - 1] + else: + dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) + + return dp[0][n - 1] \ No newline at end of file diff --git a/Daily Challenge/71. Simplify Path/README.txt b/Daily Challenge/71. Simplify Path/README.txt new file mode 100644 index 0000000..bbd3fa9 --- /dev/null +++ b/Daily Challenge/71. Simplify Path/README.txt @@ -0,0 +1,38 @@ +71. Simplify Path(Medium) + +Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path. + +In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names. + +The canonical path should have the following format: + +The path starts with a single slash '/'. +Any two directories are separated by a single slash '/'. +The path does not end with a trailing '/'. +The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..') +Return the simplified canonical path. + + + +Example 1: + +Input: path = "/home/" +Output: "/home" +Explanation: Note that there is no trailing slash after the last directory name. +Example 2: + +Input: path = "/../" +Output: "/" +Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go. +Example 3: + +Input: path = "/home//foo/" +Output: "/home/foo" +Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one. + + +Constraints: + +1 <= path.length <= 3000 +path consists of English letters, digits, period '.', slash '/' or '_'. +path is a valid absolute Unix path. diff --git a/Daily Challenge/71. Simplify Path/sol.py b/Daily Challenge/71. Simplify Path/sol.py new file mode 100644 index 0000000..2a73f81 --- /dev/null +++ b/Daily Challenge/71. Simplify Path/sol.py @@ -0,0 +1,19 @@ +class Solution: + def simplifyPath(self, path: str) -> str: + path = path.split('/') + print(path) + stack = [] + for c in path: + if c: + if c == '.': + continue + elif c == '..': + if stack: + stack.pop() + else: + continue + else: + stack.append(c) + + + return '/'+'/'.join(stack) diff --git a/Daily Challenge/946. Validate Stack Sequences/README.txt b/Daily Challenge/946. Validate Stack Sequences/README.txt new file mode 100644 index 0000000..c591f6c --- /dev/null +++ b/Daily Challenge/946. Validate Stack Sequences/README.txt @@ -0,0 +1,30 @@ +946. Validate Stack Sequences(Medium) + +Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of +push and pop operations on an initially empty stack, or false otherwise. + + + +Example 1: + +Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] +Output: true +Explanation: We might do the following sequence: +push(1), push(2), push(3), push(4), +pop() -> 4, +push(5), +pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 +Example 2: + +Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2] +Output: false +Explanation: 1 cannot be popped before 2. + + +Constraints: + +1 <= pushed.length <= 1000 +0 <= pushed[i] <= 1000 +All the elements of pushed are unique. +popped.length == pushed.length +popped is a permutation of pushed. diff --git a/Daily Challenge/946. Validate Stack Sequences/sol.py b/Daily Challenge/946. Validate Stack Sequences/sol.py new file mode 100644 index 0000000..1e076d9 --- /dev/null +++ b/Daily Challenge/946. Validate Stack Sequences/sol.py @@ -0,0 +1,12 @@ +class Solution: + def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: + stack = [] + i = 0 # popped's index + + for x in pushed: + stack.append(x) + while stack and stack[-1] == popped[i]: + stack.pop() + i += 1 + + return not stack diff --git a/Problem Solution/1) 2 sum (easy)/2 sum cpp.cpp b/Problem Solution/1) 2 sum (easy)/2 sum cpp.cpp new file mode 100644 index 0000000..294a1f4 --- /dev/null +++ b/Problem Solution/1) 2 sum (easy)/2 sum cpp.cpp @@ -0,0 +1,44 @@ +1) Brute Force [O(n^2)] +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector ret; + int size = nums.size(); + for(int i=0 ; i < size - 1 ;i++) + { + for(int j = i+1 ; j twoSum(vector& nums, int target) { + vector ret; + int size = nums.size(); + int diff; + unordered_mapm; + for (int i=0 ; i second != i ) + { + ret.push_back(i); + ret.push_back(m.find(diff) -> second); + return ret; + } + m[nums[i]] = i; + } + return ret; + } +}; diff --git a/Problem Solution/1) 2 sum (easy)/2 sum python.py b/Problem Solution/1) 2 sum (easy)/2 sum python.py new file mode 100644 index 0000000..a90c63d --- /dev/null +++ b/Problem Solution/1) 2 sum (easy)/2 sum python.py @@ -0,0 +1,10 @@ +1) Brute force +class Solution(object): + def twoSum(self, nums, target): + #Using Two pointers P1 and P2 to represent every possible pairs of the array. + for p1 in range(len(nums)): + for p2 in range(p1+1, len(nums)): + #If one of those pairs add together equal to the target return the answer else return None. + if nums[p1] + nums[p2] == target: + return [p1, p2] + return 'None' diff --git a/Problem Solution/1) 2 sum (easy)/README.txt b/Problem Solution/1) 2 sum (easy)/README.txt new file mode 100644 index 0000000..3b876fc --- /dev/null +++ b/Problem Solution/1) 2 sum (easy)/README.txt @@ -0,0 +1,31 @@ +1. Two Sum (Easy) + +Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + +You may assume that each input would have exactly one solution, and you may not use the same element twice. + +You can return the answer in any order. + + + +Example 1: + +Input: nums = [2,7,11,15], target = 9 +Output: [0,1] +Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. +Example 2: + +Input: nums = [3,2,4], target = 6 +Output: [1,2] +Example 3: + +Input: nums = [3,3], target = 6 +Output: [0,1] + + +Constraints: + +2 <= nums.length <= 104 +-109 <= nums[i] <= 109 +-109 <= target <= 109 +Only one valid answer exists. diff --git a/Problem Solution/2) Add two number (medium)/README.txt b/Problem Solution/2) Add two number (medium)/README.txt new file mode 100644 index 0000000..377bcb2 --- /dev/null +++ b/Problem Solution/2) Add two number (medium)/README.txt @@ -0,0 +1,29 @@ +2. Add Two Numbers (Medium) + +You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. + +You may assume the two numbers do not contain any leading zero, except the number 0 itself. + + + +Example 1: + + +Input: l1 = [2,4,3], l2 = [5,6,4] +Output: [7,0,8] +Explanation: 342 + 465 = 807. +Example 2: + +Input: l1 = [0], l2 = [0] +Output: [0] +Example 3: + +Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] +Output: [8,9,9,9,0,0,0,1] + + +Constraints: + +The number of nodes in each linked list is in the range [1, 100]. +0 <= Node.val <= 9 +It is guaranteed that the list represents a number that does not have leading zeros. diff --git a/Problem Solution/2) Add two number (medium)/sol.py b/Problem Solution/2) Add two number (medium)/sol.py new file mode 100644 index 0000000..1e7de7f --- /dev/null +++ b/Problem Solution/2) Add two number (medium)/sol.py @@ -0,0 +1,18 @@ +class Solution: + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: + dummy = ListNode(0) + curr = dummy + carry = 0 + + while carry or l1 or l2: + if l1: + carry += l1.val + l1 = l1.next + if l2: + carry += l2.val + l2 = l2.next + curr.next = ListNode(carry % 10) + carry //= 10 + curr = curr.next + + return dummy.next \ No newline at end of file diff --git a/Problem Solution/3) Longest Substring Without Repeating Characters/README.txt b/Problem Solution/3) Longest Substring Without Repeating Characters/README.txt new file mode 100644 index 0000000..31663aa --- /dev/null +++ b/Problem Solution/3) Longest Substring Without Repeating Characters/README.txt @@ -0,0 +1,30 @@ +3. Longest Substring Without Repeating Characters(Medium) + +Given a string s, find the length of the longest +substring + without repeating characters. + + + +Example 1: + +Input: s = "abcabcbb" +Output: 3 +Explanation: The answer is "abc", with the length of 3. +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 <= 5 * 104 +s consists of English letters, digits, symbols and spaces. diff --git a/Problem Solution/3) Longest Substring Without Repeating Characters/sol.py b/Problem Solution/3) Longest Substring Without Repeating Characters/sol.py new file mode 100644 index 0000000..3a0c87e --- /dev/null +++ b/Problem Solution/3) Longest Substring Without Repeating Characters/sol.py @@ -0,0 +1,14 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + ans = 0 + count = collections.Counter() + + l = 0 + for r, c in enumerate(s): + count[c] += 1 + while count[c] > 1: + count[s[l]] -= 1 + l += 1 + ans = max(ans, r - l + 1) + + return ans \ No newline at end of file