Jyotirmay Welcomes You!

#### palindrome-number: PROBLEM STATEMENT

```

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true
Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Coud you solve it without converting the integer to a string?

```

#### MY SOLUTION

```                                    ```

class Solution:
def isPalindrome(self, x: int) -> bool:
x_arr = []
x_orig = x
while (x > 0):
x_arr.append(x % 10)
x = x // 10

x_len = len(x_arr)
p_num = 0
for idx, x_r in enumerate(x_arr):
p_num += x_r * 10 ** (x_len - idx - 1)

return True if x_orig == p_num else False

```
```

#### BEST RUN-TIME SOLUTION

```                                    ```

class Solution:
def isPalindrome(self, x: int) -> bool:
x_str = str(x)
return x >= 0 and x_str == x_str[::-1]

```
```

#### BEST SPACE-TIME SOLUTION

```                                    ```

class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False
x = str(x)
n = len(x)
for i in range(n//2):
if x[i] != x[-(i+1)]:
return False
return True

```
```

#### RANK ACHIEVED #### two-sum: PROBLEM STATEMENT

```

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums + nums == 9, we return [0, 1].
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:

2 <= nums.length <= 105
-109 <= nums[i] <= 109
-109 <= target <= 109

```

#### MY SOLUTION

```                                    ```

class Solution:
def twoSum(self, nums, target):
valList = []
for i, val in enumerate(nums):
if val in valList:
return [valList.index(val), i]
else:
more = target - val
valList.append(more)

```
```

#### BEST RUN-TIME SOLUTION

```                                    ```

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
a = {}
for i in range(len(nums)):
n = target - nums[i]
if n not in a.keys():
a[nums[i]] = i
else:
return [a[n], i]

```
```

#### BEST SPACE-TIME SOLUTION

```                                    ```

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i,j]

```
```

#### RANK ACHIEVED #### house-robber: PROBLEM STATEMENT

```

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:

Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.

Constraints:

0 <= nums.length <= 100
0 <= nums[i] <= 400

```

#### MY SOLUTION

```                                    ```

# COPIED

class Solution:
def rob(self, nums):
dp1, dp2 = 0, 0
for num in nums:
dp1, dp2 = dp2, max(dp1 + num, dp2)
return dp2

```
```

#### BEST RUN-TIME SOLUTION

```                                    ```

class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return (0)

dp =  * (len(nums))
dp = nums

for i in range(1, len(nums)):
dp[i] = max(dp[i - 1], nums[i] + (dp[i - 2] if i > 1 else 0))
return (dp[-1])

```
```

#### BEST SPACE-TIME SOLUTION

```                                    ```

class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
if len(nums) == 1:
return nums
dp =  * len(nums)
dp = nums
dp = max(nums, nums)

for i in range(2, len(nums)):
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
return dp[-1]

```
```

#### RANK ACHIEVED #### valid-parentheses: PROBLEM STATEMENT

```

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.

Example 1:

Input: s = "()"
Output: true
Example 2:

Input: s = "()[]{}"
Output: true
Example 3:

Input: s = "(]"
Output: false
Example 4:

Input: s = "([)]"
Output: false
Example 5:

Input: s = "{[]}"
Output: true

Constraints:

1 <= s.length <= 104
s consists of parentheses only '()[]{}'.

```

#### MY SOLUTION

```                                    ```

# In Progress
class Solution:
def isValid(self, s: str) -> bool:
b1 = 0
b2 = 0
b3 = 0
for i, si in enumerate(s):

# b1 = si
# last = isValid(s[i:])
if si == '(':
b1 += 1
elif si == '{':
b2 += 1
elif si == '[':
b3 += 1
elif si == ')':
b1 -= 1
elif si == '}':
b2 -= 1
elif si == ']':
b3 -= 1
else:
pass

print(b1, b2, b3)
if (b1 != 0) or (b2 != 0) or (b3 != 0):
return False
else:
return True

```
```

#### BEST RUN-TIME SOLUTION

```                                    ```

```
```

#### BEST SPACE-TIME SOLUTION

```                                    ```

```
```

#### RANK ACHIEVED #### reverse-integer: PROBLEM STATEMENT

```

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321
Example 2:

Input: -123
Output: -321
Example 3:

Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

```

#### MY SOLUTION

```                                    ```

class Solution:
def reverse(self, x: int) -> int:
if x > (2 ** 31) - 1 or x < -(2 ** 31):
return 0

x_s = str(x)
if x_s == '-' or x_s == '+':
x_tr = x_s[1:]
signed = x_s
else:
x_tr = x_s
signed = ''

def print_reverse(xt):
if len(xt) == 0:
return xt

return print_reverse(xt[1:]) + xt

out = print_reverse(x_tr)

if len(out) > 1:
for idx, o in enumerate(out):
if o is '0':
continue
elif o is not '0':
out = out[idx:]
break
final_out = int(signed + out)
if final_out > (2 ** 31) - 1 or final_out < -(2 ** 31):
return 0

return final_out

```
```

#### BEST RUN-TIME SOLUTION

```                                    ```

class Solution:
def reverse(self, x: int) -> int:
x = str(x)
a =  int('-' + x[-1:0:-1]) if x == '-' else int(x[::-1])
if a >= -2147483648 and a<= 2147483647: return a
else: return 0

```
```

#### BEST SPACE-TIME SOLUTION

```                                    ```

class Solution:
def reverse(self, x: int) -> int:

reversed = 0
if x == 0:
return 0

num = x
x = abs(x)
while x != 0:
reversed = (reversed * 10) + (x % 10)
x //= 10
if (reversed > ((2 ** 31) - 1) or reversed < -1 * (2 ** 31)):
return 0

if num < 0:
return -reversed
else:
return reversed

```
```

#### RANK ACHIEVED #### roman-to-integer: PROBLEM STATEMENT

```

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, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3
Example 2:

Input: "IV"
Output: 4
Example 3:

Input: "IX"
Output: 9
Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

```

#### MY SOLUTION

```                                    ```

class Solution:
def romanToInt(self, s: str) -> int:
priority = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
num = 0
for idx, si in enumerate(s):
if idx == 0:
num += priority[si]
continue

temp_num = priority[si]
prev_num = priority[s[idx - 1]]
if temp_num <= prev_num:
num += priority[si]
else:
num += priority[si] - (2 * prev_num)

return num

```
```

#### BEST RUN-TIME SOLUTION

```                                    ```

class Solution:
def romanToInt(self, s: str) -> int:
table = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}

prev = 0
current = 0
total = 0
for i in range(len(s)):
current = table[s[i]]
if current > prev:
total = total + current - 2 * prev
else:
total = total + current
prev = current

```
```

#### BEST SPACE-TIME SOLUTION

```                                    ```

class Solution:
def romanToInt(self, s: str) -> int:
dictt={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
prev=0
ans=0
for i in range(len(s)-1,-1,-1):
if prev<=dictt[s[i]]:
prev=dictt[s[i]]
ans+=prev
else:
prev-=dictt[s[i]]
ans-=dictt[s[i]]
#print(ans)
return ans

```
```

#### RANK ACHIEVED #### longest-common-prefix: PROBLEM STATEMENT

```

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"
Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:

All given inputs are in lowercase letters a-z.

```

#### MY SOLUTION

```                                    ```

class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""

base = strs
for toMatch in strs[1:]:
base = self.lcp(base, toMatch)

return base

def lcp(self, w1, w2):
lcp = ''
for l1, l2 in zip(w1, w2):
if l1 == l2:
lcp += l1
else:
break
return lcp

```
```

#### BEST RUN-TIME SOLUTION

```                                    ```

class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
l = min(len(x) for x in strs)
lcp = strs[:l]

for i in range(1, len(strs)):
while l >= 0 and lcp != strs[i][:l]:
l = l - 1
lcp = strs[:l]

return lcp

```
```

#### BEST SPACE-TIME SOLUTION

```                                    ```

class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
prefix = ""
arranged = list(zip(*strs))
for chars in arranged:
print(chars)
if chars.count(chars) == len(chars):
prefix = "".join([prefix, chars])
else:
break

return prefix

```
```

#### RANK ACHIEVED #### July 4th, 2020 #### July 19th, 2020 #### July 16th, 2020 #### August 4th, 2020 #### September 02nd, 2020 #### July 13th, 2020 #### September 19th, 2020 #### September 04th, 2020 #### August 13th, 2020 #### August 21th, 2020 #### July 22nd, 2020 #### July 1st, 2020 #### October 16th, 2020 #### July 10th, 2020 #### August 1st, 2020 #### August 18th, 2020 #### September 07th, 2020 #### September 26th, 2020 #### September 16th, 2020 #### August 24th, 2020 #### August 10th, 2020 #### October 05th, 2020 #### August 27th, 2020 #### September 13th, 2020 #### October 24th, 2020 #### October 09th, 2020 #### September 10th, 2020 #### August 30th, 2020 #### August 04th, 2020 #### July 7th, 2020 #### September 22nd, 2020 #### August 15th, 2020 #### August 07th, 2020 #### 070920 ## Get connect with me from anywhere

World is small and round, even if we don't wanna meet, we will!