Posts

Showing posts with the label string

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...

6. Zigzag Conversion

 https://leetcode.com/problems/zigzag-conversion/ The string  "PAYPALISHIRING"  is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line:  "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows);   Example 1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR" Example 2: Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation: P I N A L S I G Y A H R P I Example 3: Input: s = "A", numRows = 1 Output: "A"   Constraints: 1 <= s.length <= 1000 s  consists of English letters (lower-case and upper-case),  ','  and  '.' . 1 <= numRows <= 1000 --- Intuition Concatenate characters sequentially ...

Number of stickers of source string to make target string

https://leetcode.com/discuss/interview-question/418351/uber-internship-no-offer Uber used to call Ubercab, and they have a lot of "ubercab" stickers and assuming you can cut them into individual characters. You are now given a word in string, and return how many stickers you need to make the word --- Intuition Generic solution given source and target words Pre process => source chars freq, and target  chars  freq in HashMap's Ignore spaces in both source, and target for each char in target     ans = Math.max(ans, (int)Math.ceil(target freq * 1.0 / source freq)) --- Time - O(m + n) Space - O(1) = Character set size --- ---

290. Word Pattern

https://leetcode.com/problems/word-pattern/ Given a  pattern  and a string  str , find if  str  follows the same pattern. Here  follow  means a full match, such that there is a bijection between a letter in  pattern  and a  non-empty  word in  str . Example 1: Input: pattern = "abba" , str = "dog cat cat dog" Output: true Example 2: Input: pattern = "abba" , str = "dog cat cat fish" Output: false Example 3: Input: pattern = "aaaa" , str = "dog cat cat dog" Output: false Example 4: Input: pattern = "abba" , str = "dog dog dog dog" Output: false Notes: You may assume  pattern  contains only lowercase letters, and  str  contains lowercase letters that may be separated by a single space. --- Related problems 205-isomorphic-strings 890-find-and-replace-pattern

97. Interleaving String

https://leetcode.com/problems/interleaving-string/ Given  s1 ,  s2 ,  s3 , find whether  s3  is formed by the interleaving of  s1  and  s2 . Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" Output: false

68. Text Justification

https://leetcode.com/problems/text-justification/ Given an array of words and a width  maxWidth , format the text such that each line has exactly  maxWidth  characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces  ' '  when necessary so that each line has exactly  maxWidth  characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left justified and no  extra  space is inserted between words. Note: A word is defined as a character sequence consisting of non-space characters only. Each word's length is guaranteed to be greater than 0 and not exceed  maxWidth . The input array  words  cont...