Posts

771. Jewels and Stones

You're given strings  J  representing the types of stones that are jewels, and  S  representing the stones you have.  Each character in  S is a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in  J  are guaranteed distinct, and all characters in  J  and  S  are letters. Letters are case sensitive, so  "a"  is considered a different type of stone from  "A" . Example 1: Input: J = "aA", S = "aAAbbbb" Output: 3 Example 2: Input: J = "z", S = "ZZ" Output: 0 Note: S  and  J  will consist of letters and have length at most 50. The characters in  J  are distinct. Code: class Solution:     def numJewelsInStones(self, J, S):         """         :type J: str         :type S: str         :rtype: int         """         count = 0         for i in S:             if i in J:                 count += 1  

561. Array Partition I

Given an array of  2n  integers, your task is to group these integers into  n  pairs of integer, say (a 1 , b 1 ), (a 2 , b 2 ), ..., (a n , b n ) which makes sum of min(a i , b i ) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4). Note: n  is a positive integer, which is in the range of [1, 10000]. All the integers in the array will be in the range of [-10000, 10000]. Python 3 Code: class Solution:     def arrayPairSum(self, nums):         """         :type nums: List[int]         :rtype: int         """         nums.sort()         x = 0         for i in range (0,len(nums),2):             x += nums[i]         return x

728. Self Dividing Numbers

A  self-dividing number  is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because  128 % 1 == 0 ,  128 % 2 == 0 , and  128 % 8 == 0 . Also, a self-dividing number is not allowed to contain the digit zero. Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible. Example 1: Input: left = 1, right = 22 Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22] Note: The boundaries of each input argument are  1 <= left <= right <= 10000 . Python 3 code ( Your runtime beats 87.86 % of python3 submissions. ) : class Solution:     def selfDividingNumbers(self, left, right):         """         :type left: int         :type right: int         :rtype: List[int]         """         A = []         for i in range (left, right+1):             if '0' in str(i):                 continue      

657. Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to  the original place . The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are  R  (Right),  L (Left),  U  (Up) and  D  (down). The output should be true or false representing whether the robot makes a circle. Example 1: Input: "UD" Output: true Example 2: Input: "LL" Output: false Python3 Code: class Solution:     def judgeCircle(self, moves):         """         :type moves: str         :rtype: bool         """         if moves.count("U") == moves.count("D") and moves.count("L") == moves.count("R"):             return True         else:             return False

461. Hamming Distance

The  Hamming distance  between two integers is the number of positions at which the corresponding bits are different. Given two integers  x  and  y , calculate the Hamming distance. Note: 0 ≤  x ,  y  < 2 31 . Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different. Python 3 Code: class Solution:     def hammingDistance(self, x, y):         """         :type x: int         :type y: int         :rtype: int         """         z = int (x ^ y)         ans = 0         while z > 0:             ans += z % 2             z = z >> 1                       return ans

535. Encode and Decode TinyURL

Note: This is a companion problem to the  System Design  problem:  Design TinyURL . TinyURL is a URL shortening service where you enter a URL such as  https://leetcode.com/problems/design-tinyurl  and it returns a short URL such as  http://tinyurl.com/4e9iAk . Design the  encode  and  decode  methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL. Python 3 code: class Codec:     def encode(self, longUrl):         """Encodes a URL to a shortened URL.                  :type longUrl: str         :rtype: str         """         return longUrl     def decode(self, shortUrl):         """Decodes a shortened URL to its original URL.                  :type shortUrl: str         :rtype: str         """         return shortUrl

760. Find Anagram Mappings

Given two lists  A and  B , and  B  is an anagram of  A .  B  is an anagram of  A  means  B  is made by randomizing the order of the elements in  A . We want to find an  index mapping   P , from  A  to  B . A mapping  P[i] = j  means the  i th element in  A  appears in  B  at index  j . These lists  A  and  B  may contain duplicates. If there are multiple answers, output any of them. For example, given A = [12, 28, 46, 32, 50] B = [50, 12, 32, 46, 28] We should return [1, 4, 3, 2, 0] as  P[0] = 1  because the  0 th element of  A  appears at  B[1] , and  P[1] = 4  because the  1 st element of  A  appears at  B[4] , and so on. Note: A, B  have equal lengths in range  [1, 100] . A[i], B[i]  are integers in range  [0, 10^5] . Python 3 solution: class Solution:     def anagramMappings(self, A, B):         """         :type A: List[int]         :type B: List[int]         :rtype: List[int]         """         return [B.