Posts

Showing posts from January, 2023

2193. Minimum Number of Moves to Make Palindrome

https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/description/ You are given a string  s  consisting only of lowercase English letters. In one  move , you can select any two  adjacent  characters of  s  and swap them. Return  the  minimum number of moves  needed to make   s   a palindrome . Note  that the input will be generated such that  s  can always be converted to a palindrome.   Example 1: Input: s = "aabb" Output: 2 Explanation: We can obtain two palindromes from s, "abba" and "baab". - We can obtain "abba" from s in 2 moves: "a ab b" -> "ab ab " -> "abba". - We can obtain "baab" from s in 2 moves: "a ab b" -> " ab ab" -> "baab". Thus, the minimum number of moves needed to make s a palindrome is 2. Example 2: Input: s = "letelt" Output: 2 Explanation: One of the palindromes we can obtain from s in 2 moves is "lette...

583. Delete Operation for Two Strings

https://leetcode.com/problems/delete-operation-for-two-strings/description/ Given two strings  word1  and  word2 , return  the minimum number of  steps  required to make   word1   and   word2   the same . In one  step , you can delete exactly one character in either string.   Example 1: Input: word1 = "sea", word2 = "eat" Output: 2 Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea". Example 2: Input: word1 = "leetcode", word2 = "etco" Output: 4   Constraints: 1 <= word1.length, word2.length <= 500 word1  and  word2  consist of only lowercase English letters. --- Time - O(M * N) Space - O(M * N) --- ---- ----

916. Word Subsets

 https://leetcode.com/problems/word-subsets/description/ You are given two string arrays  words1  and  words2 . A string  b  is a  subset  of string  a  if every letter in  b  occurs in  a  including multiplicity. For example,  "wrr"  is a subset of  "warrior"  but is not a subset of  "world" . A string  a  from  words1  is  universal  if for every string  b  in  words2 ,  b  is a subset of  a . Return an array of all the  universal  strings in  words1 . You may return the answer in  any order .   Example 1: Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"] Output: ["facebook","google","leetcode"] Example 2: Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"] Output: ["apple","google","leetco...

474. Ones and Zeroes

 https://leetcode.com/problems/ones-and-zeroes/description/ You are given an array of binary strings  strs  and two integers  m  and  n . Return  the size of the largest subset of  strs  such that there are  at most   m   0 's and  n   1 's in the subset . A set  x  is a  subset  of a set  y  if all elements of  x  are also elements of  y .   Example 1: Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3 Output: 4 Explanation: The largest subset with at most 5 0's and 3 1's is {"10", "0001", "1", "0"}, so the answer is 4. Other valid but smaller subsets include {"0001", "1"} and {"10", "1", "0"}. {"111001"} is an invalid subset because it contains 4 1's, greater than the maximum of 3. Example 2: Input: strs = ["10","0","1"], m = 1, n = 1 Output: 2 Explanation: The largest subs...