-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added solutions of leetcode problems
- Loading branch information
1 parent
03ad1a1
commit 2cb2b6c
Showing
10 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SuGo, 23 August 2018 | ||
# https://leetcode.com/problems/find-anagram-mappings/description/ | ||
# Example :: | ||
# A = [12, 28, 46, 32, 50] | ||
# B = [50, 12, 32, 46, 28] | ||
#result: [1, 4, 3, 2, 0] | ||
# time and space complexity: O(n) | ||
import collections | ||
class Solution: | ||
def anagramMapping(self, A, B): | ||
lookup = collections.defaultdict(collections.deque) | ||
for index, item in enumerate(B): | ||
lookup[item].append(index) | ||
result = [] | ||
for item in A: | ||
result.append(lookup[item].popleft()) | ||
return result | ||
|
||
A = [12, 28, 46, 32, 50] | ||
B = [50, 12, 32, 46, 28] | ||
C = [40, 40] | ||
D = [40, 40] | ||
sol = Solution() | ||
print(sol.anagramMapping(A, B)) | ||
print(sol.anagramMapping(C, D)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# SuGo, 2 Sep 2018 | ||
# Implementation of Counter datatypes inbuilt in python | ||
# | ||
import collections | ||
# Initialization | ||
print(collections.Counter(["a", "b", "b", "c", "a"])) | ||
print(collections.Counter({"a":2, "b":2, "c":1})) | ||
print(collections.Counter(a = 2, b = 2, c =1)) | ||
|
||
# update method in Counter | ||
c = collections.Counter() | ||
print("1st c ", c) | ||
c.update('abcdaab') | ||
print("2nd c ", c) | ||
c.update({"d":3, "e":1}) | ||
print("3rd c", c) | ||
|
||
|
||
# Counter keep the count of the elements | ||
c = collections.Counter("abcdefdef") | ||
for letter in "abcde": | ||
print("letter ", letter, " comes ", c[letter], " any times!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class Solution(object): | ||
# O(n) | ||
def longestPalindrome(self, s): | ||
#check beforehand if the passed string is already a palindrome in itself | ||
#or if length of string is less than 3, then return s itself | ||
if (s==s[::-1] or len(s) < 3): | ||
return s | ||
res = "" | ||
for i in range(len(s)): | ||
# odd case, like "aba" | ||
tmp = self.helper(s, i, i) | ||
if len(tmp) > len(res): | ||
res = tmp | ||
# even case, like "abba" | ||
tmp = self.helper(s, i, i+1) | ||
if len(tmp) > len(res): | ||
res = tmp | ||
#check if "i" has crossed middle element and if length of largest | ||
#palindrome till found, is greater than half of total length of string | ||
#then break, answer found no need to iterate more | ||
if (i>(len(s)//2) and len(res)>(len(s)//2)): | ||
break | ||
return res | ||
# get the longest palindrome, l, r are the middle indexes | ||
# from inner to outer | ||
def helper(self, s, l, r): | ||
while l >= 0 and r < len(s) and s[l] == s[r]: | ||
l -= 1; r += 1 | ||
return s[l+1:r] | ||
def longestPalindrome1(self, s): | ||
""" | ||
:type s: str | ||
:rtype: str | ||
""" | ||
n=len(s) | ||
if n<2: return s | ||
start,maxlen,i=0,1,0 | ||
while i<n: | ||
print("entering WHILE loop ") | ||
if n-i<=maxlen/2: | ||
break | ||
j,k=i,i | ||
while k<n-1 and s[k]==s[k+1]: # ship the same character | ||
k = k+1 | ||
i = k+1 | ||
while k<n-1 and j>0 and s[k+1]==s[j-1]: #expand | ||
print("in while 2") | ||
k,j=k+1,j-1 | ||
if maxlen<=k-j+1: | ||
print("in IF cond") | ||
start=j | ||
maxlen=k-j+1 | ||
return s[start:start+maxlen] | ||
s = Solution() | ||
a = "babad" | ||
print(s.longestPalindrome1(a)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# SuGo, 25 August 2018 | ||
# https://leetcode.com/problems/palindrome-number/description/ | ||
# Example:Input: 121 | ||
# Output: true | ||
# Example: Input: -121 | ||
# Output: false | ||
|
||
class Solution: | ||
def palindrome(self, InputStr): | ||
""" | ||
Checks for the palindrome by converting the input into a string | ||
""" | ||
InputStr = str(InputStr) | ||
if InputStr[0:] == InputStr[::-1]: | ||
return True | ||
else: | ||
return False | ||
|
||
def palindromeInt(self, inputInt): | ||
""" | ||
finds a number palindrome without converting it into a string. | ||
""" | ||
if inputInt < 0: | ||
return | ||
copy, reverse = inputInt, 0 | ||
while copy: | ||
reverse = reverse*10 | ||
reverse = reverse + copy%10 | ||
copy = copy//10 | ||
return (inputInt==reverse) | ||
inputStr = 12121 | ||
sol = Solution() | ||
print(sol.palindrome(inputStr)) | ||
print(sol.palindromeInt(inputStr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# SuGo, 25 August 2018 | ||
#https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ | ||
|
||
class Solution: | ||
def removeDuplicates(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
nextInd = 0 | ||
if len(nums)==0: | ||
return 0 | ||
for i in range(0, len(nums)): | ||
if nums[i]!= nums[nextInd]: | ||
nextInd = nextInd+1 | ||
if i!=nextInd: | ||
nums[nextInd] = nums[i] | ||
return nextInd+1 | ||
|
||
nums = [1, 1, 2] | ||
s = Solution() | ||
print(s.removeDuplicates(nums)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# SuGo, 28 August 2018 | ||
class Solution(): | ||
def reverse(self, x): | ||
reverse = 0 | ||
y = abs(x) | ||
while y: | ||
a = y%10 # remainder | ||
y = y//10 # quotient | ||
reverse = reverse * 10 + a | ||
|
||
|
||
return (-1*reverse) if x<0 else reverse | ||
|
||
x = -231 | ||
s = Solution() | ||
print(s.reverse(x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# SuGo, 3 Sep. 2018 | ||
class Solution(object): | ||
# imolementation 1 | ||
def matrixZeros(self, matrix): | ||
rowFlag = False | ||
colFlag = False | ||
rows = len(matrix)-1 | ||
cols = len(matrix[0])-1 | ||
print("rows and cols ", rows,cols) | ||
|
||
for i in range(0, rows): | ||
for j in range(0, cols): | ||
if i == 0 and matrix[i][j] == 0: | ||
print("i ", i) | ||
rowFlag = True | ||
if j == 0 and matrix[i][j] == 0: | ||
print("j ", j) | ||
colFlag = True | ||
if matrix[i][j] == 0: | ||
print("i and j", i, j) | ||
matrix[0][j] = 0 | ||
matrix[i][0] = 0 | ||
|
||
for i in range(0, rows): | ||
for j in range(0, cols): | ||
if matrix[0][j] == 0 or matrix[i][0] == 0: | ||
print("i and j 2 -- ", i, j) | ||
matrix[i][j] = 0 | ||
|
||
if rowFlag == True: | ||
for i in range(0, cols): | ||
print("cols ", i) | ||
matrix[0][cols] = 0 | ||
|
||
if colFlag == True: | ||
for i in range(0, rows): | ||
print("rows ", i) | ||
matrix[rows][0] = 0 | ||
return matrix | ||
# using set() | ||
# O(m*n) time complexity | ||
def matrixZeros_(self, matrix): | ||
rowID = set() | ||
colID = set() | ||
rows = len(matrix) | ||
cols = len(matrix[0]) | ||
for i in range(0, rows): | ||
for j in range(0, cols): | ||
if matrix[i][j] == 0: | ||
rowID.add(i) | ||
colID.add(j) | ||
|
||
for i in range(0, rows): | ||
for j in range(0, cols): | ||
if i in rowID or j in colID: | ||
matrix[i][j] =0 | ||
return matrix | ||
|
||
def setZeroes(self, matrix): | ||
rows = set() | ||
columns = set() | ||
#First pass to check which rows and columns will need to be changed | ||
for y in range(len(matrix)): | ||
for x in range(len(matrix[0])): | ||
if matrix[y][x] == 0: | ||
rows.add(y) | ||
columns.add(x) | ||
#Second pass to change rows and columns to zero values | ||
for y in range(len(matrix)): | ||
for x in range(len(matrix[0])): | ||
if y in rows or x in columns: | ||
matrix[y][x] = 0 | ||
return matrix | ||
####### | ||
matrix = [ | ||
[1,1,1], | ||
[1,0,1], | ||
[1,1,1] | ||
] | ||
matrix_ = [ | ||
[0,1,2,0], | ||
[3,4,5,2], | ||
[1,3,1,5], | ||
[1, 2, 0, 0] | ||
] | ||
|
||
s = Solution() | ||
print(s.setZeroes(matrix)) | ||
print(s.matrixZeros(matrix)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# SuGo, 27 August2018 | ||
# Demo of 2D array implementation | ||
|
||
def twoD(): | ||
a = [] | ||
for i in range(0, 9): | ||
for j in range(0, 9): | ||
a[i][j] = 1 | ||
print(a) | ||
|
||
twoD() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# SuGo, 24 August 2018 | ||
# https://leetcode.com/problems/two-sum/description/ | ||
|
||
class Solution: | ||
def twoSum1(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
time complexity : O(n^2) | ||
""" | ||
result = [] | ||
for i in range(len(nums)): | ||
for j in range((i+1), len(nums)): | ||
if (nums[i] + nums[j] == target): | ||
result.append(i) | ||
result.append(j) | ||
return result | ||
# **** Implementation 2 **** | ||
def twoSum2(self, nums, target): | ||
""" | ||
Time complexity: O(n) | ||
Space : O(n) | ||
""" | ||
|
||
|
||
|
||
|
||
nums = [2, 7, 11, 15] | ||
target = 9 | ||
solve = Solution() | ||
print(solve.twoSum(nums, target)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# SuGo, 25 August 2018 | ||
# https://leetcode.com/problems/valid-parentheses/description/ | ||
# | ||
class Solution: | ||
def validParenthesis(self, parenthese): | ||
openBrackets = ["(", "[", "{"] | ||
closeBrackets = [")", "]", "}"] | ||
stack = [] | ||
for brackets in parenthese: | ||
if brackets in openBrackets: | ||
brack_id = openBrackets.index(brackets) | ||
stack.append(closeBrackets[brack_id]) | ||
elif brackets in closeBrackets: | ||
item = stack.pop() | ||
if item != brackets: | ||
return False | ||
return True | ||
def isValid(self, parenthese): | ||
dict_ = {"(":")", "[":"]", "{":"}"} | ||
stack = [] | ||
for bracket in parenthese: | ||
if bracket in dict_: | ||
stack.append(bracket) | ||
elif bracket != dict_[stack.pop()]: | ||
return False | ||
return len(stack) == 0 | ||
s = "[}" | ||
sol = Solution() | ||
print(sol.validParenthesis(s)) | ||
print(sol.isValid(s)) |