Posts

1544. Make The String Great

  Difficulty: Easy Given a string  s  of lower and upper case English letters. A good string is a string which doesn't have  two adjacent characters   s[i]  and  s[i + 1]  where: 0 <= i <= s.length - 2 s[i]  is a lower-case letter and  s[i + 1]  is the same letter but in upper-case or  vice-versa . To make the string good, you can choose  two adjacent  characters that make the string bad and remove them. You can keep doing this until the string becomes good. Return  the string  after making it good. The answer is guaranteed to be unique under the given constraints. Notice  that an empty string is also good.   Example 1: Input: s = "leEeetcode" Output: "leetcode" Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode". Example 2: Input: s = "abBAcC" Output: "" Explanation: We have many possible scenarios, and all lead to the same answer. For exa

136. Single Number

  Difficulty: Easy Given a  non-empty  array of integers  nums , every element appears  twice  except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Example 1: Input: nums = [2,2,1] Output: 1 Example 2: Input: nums = [4,1,2,1,2] Output: 4 Example 3: Input: nums = [1] Output: 1 Python Solution: class Solution(object):     def singleNumber(self, nums):         nums.sort()         i=0         while 1:             try:                 if nums[i]==nums[i+1]:                     del nums[i]                     del nums[i]                 else:                     i+=1             except:                 break         return nums[0]

1323. Maximum 69 Number

Difficulty: Easy You are given a positive integer  num  consisting only of digits  6  and  9 . Return  the maximum number you can get by changing  at most  one digit ( 6  becomes  9 , and  9  becomes  6 ) . Example 1: Input: num = 9669 Output: 9969 Explanation: Changing the first digit results in 6669. Changing the second digit results in 9969. Changing the third digit results in 9699. Changing the fourth digit results in 9666. The maximum number is 9969. Example 2: Input: num = 9996 Output: 9999 Explanation: Changing the last digit 6 to 9 results in the maximum number. Example 3: Input: num = 9999 Output: 9999  Explanation: It is better not to apply any change.   Python Solution: class Solution:     def maximum69Number (self, num: int) -> int:         num=str(num)         for i in range(len(num)):             if num[i]=='6':                 num=num[:i]+'9'+num[i+1:]                 break         return int(num)

35. Search Insert Position

Difficulty: Easy   Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with  O(log n)  runtime complexity.   Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2: Input: nums = [1,3,5,6], target = 2 Output: 1 Example 3: Input: nums = [1,3,5,6], target = 7 Output: 4 Python Solution: class Solution: def searchInsert(self, nums: List[int], target: int) -> int: if target in nums: return nums.index(target) else: nums.append(target) nums.sort() return nums.index(target)

13. Roman to Integer

Difficulty: Easy Roman numerals are represented by seven different symbols:  I ,  V ,  X ,  L ,  C ,  D  and  M . Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example,  2  is written as  II  in Roman numeral, just two ones added together.  12  is written as  XII , which is simply  X + II . The number  27  is written as  XXVII , which is  XX + V + II . Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not  IIII . Instead, the number four is written as  IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as  IX . There are six instances where subtraction is used: I  can be placed before  V  (5) and  X  (10) to make 4 and 9.  X  can be placed before  L  (50) and  C  (100) to make 40 and 90.  C  can be placed before  D  (500) and  M  (1000) to make 400 an

394. Decode String

Difficulty: Medium  Given an encoded string, return its decoded string. The encoding rule is:  k[encoded_string] , where the  encoded_string  inside the square brackets is being repeated exactly  k  times. Note that  k  is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers,  k . For example, there will not be input like  3a  or  2[4] . The test cases are generated so that the length of the output will never exceed  10 5 .   Example 1: Input: s = "3[a]2[bc]" Output: "aaabcbc" Example 2: Input: s = "3[a2[c]]" Output: "accaccacc" Example 3: Input: s = "2[abc]3[cd]ef" Output: "abcabccdcdcdef" Python Solution: class Solution: def decodeString(self, s: str) -> str: c=0

11. Container With Most Water

Image
Difficulty : Medium You are given an integer array  height  of length  n . There are  n  vertical lines drawn such that the two endpoints of the  i th  line are  (i, 0)  and  (i, height[i]) . Find two lines that together with the x-axis form a container, such that the container contains the most water. Return  the maximum amount of water a container can store . Notice  that you may not slant the container.   Example 1: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example 2: Input: height = [1,1] Output: 1 Python Solution : class Solution: def maxArea(self, height: List[int]) -> int: i=0 j=len(height)-1 ans=0 while i<j: if height[i]<=height[j]: res=height[i]*(j-i) i=i+1 else: res=height[j]*(j-i)