컴퓨터공학/LeetCode 1000
[LeetCode] 557. Reverse Words in a String III
557. Reverse Words in a String III Easy Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Example 2: Input: s = "God Ding" Output: "doG gniD" Constraints: 1 str: result = split(' ', s) result = ' '.join([word[::-1] for ..
[LeetCode]1338. Reduce Array Size to The Half
1338. Reduce Array Size to The Half Medium You are given an integer array arr. You can choose a set of integers and remove all the occurrences of these integers in the array. Return the minimum size of the set so that at least half of the integers of the array are removed. Example 1: Input: arr = [3,3,3,3,5,5,5,2,2,7] Output: 2 Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] whic..
[LeetCode] 1570. Dot Product of Two Sparse Vectors
1570. Dot Product of Two Sparse Vectors Medium Given two sparse vectors, compute their dot product. Implement class SparseVector: SparseVector(nums) Initializes the object with the vector nums dotProduct(vec) Compute the dot product between the instance of SparseVector and vec A sparse vector is a vector that has mostly zero values, you should store the sparse vector efficiently and compute the ..
[LeetCode] 28. Implement strStr()
28. Implement strStr() Easy Implement strStr(). Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when needle is an empty string. Th..
[LeetCode] 49. Group Anagrams
49. Group Anagrams Medium Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1: Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] Examp..
[LeetCode] 804. Unique Morse Code Words
804. Unique Morse Code Words Easy International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: 'a' maps to ".-", 'b' maps to "-...", 'c' maps to "-.-.", and so on. For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--"..
[LeetCode] 5. Longest Palindromic Substring
5. Longest Palindromic Substring Medium Given a string s, return the longest palindromic substring in s. Example 1: Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2: Input: s = "cbbd" Output: "bb" Constraints: 1 = 0 and r self.res_cnt: self.res_cnt = ans self.res = s[l + 1 : r] for i in range(le..
[LeetCode] 387. First Unique Character in a String
387. First Unique Character in a String Easy 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: 1