Skip to content

Commit 484e7df

Browse files
adde leetcode code
0 parents  commit 484e7df

File tree

221 files changed

+6507
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

221 files changed

+6507
-0
lines changed

Linked_List/__init__.py

Whitespace-only changes.

Linked_List/lc_141.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
""" write a function that determines if a singly linked list is circular and if it is, we are going to return the node where the cycle begins.
2+
This solution is based off of Robert Floyd’s algorithm, first discovered in the 1970’s. """
3+
4+
""" Floyd’s cycle finding algorithm or Hare-Tortoise algorithm is a pointer algorithm that uses only two pointers, moving through the sequence
5+
at different speeds. This algorithm is used to find a loop in a linked list. It uses two pointers one moving twice as fast as the other one.
6+
The faster one is called the faster pointer and the other one is called the slow pointer. """
7+
8+
""" How Does Floyd’s Cycle Finding Algorithm Works? """
9+
10+
""" While traversing the linked list one of these things will occur """
11+
12+
"""
13+
1) The Fast pointer may reach the end (NULL) this shows that there is no loop n the linked list.
14+
2) The Fast pointer again catches the slow pointer at some time therefore a loop exists in the linked list.
15+
"""
16+
17+
from typing import Optional
18+
19+
20+
class ListNode:
21+
def __init__(self, data=None, next=None):
22+
self.data = data
23+
self.next = next
24+
# self.next = None
25+
26+
27+
class Solution:
28+
29+
def hasCycle(self, head: Optional[ListNode]) -> bool:
30+
31+
slow, fast = head, head
32+
33+
while fast and fast.next:
34+
35+
slow = head.next
36+
fast = head.next.next
37+
print("klasjld")
38+
if slow == fast:
39+
print("kfdjsl")
40+
return True
41+
return False
42+
43+
44+
firstNode = ListNode(1)
45+
linkedList = Solution()
46+
47+
head = None
48+
for i in reversed(range(5)):
49+
head = ListNode(i + 1, head)
50+
51+
# insert cycle
52+
# head.next.next.next.next.next = head.next.next
53+
54+
if Solution().hasCycle(head):
55+
print('Cycle Found')
56+
else:
57+
print('No Cycle Found')
58+
59+
# listNode=ListNode(1)
60+
# listNode.val=2
61+
# listNode.val=3
62+
# listNode.val=4
63+
# listNode.val=5

Linked_List/lc_206.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# vedio : https://www.youtube.com/watch?v=RhCGA4jlPmQ&list=PLzjoZGHG3J8vdUH75YPqmO7lbQl_M-xXo&index=2
2+
class Node:
3+
def __init__(self, data):
4+
self.data = data
5+
self.next = None
6+
7+
8+
class LinkedList:
9+
def __init__(self):
10+
self.head = None
11+
12+
def insert(self, newNode):
13+
if self.head is None:
14+
self.head = newNode
15+
16+
else:
17+
lastNode = self.head
18+
19+
while True:
20+
if lastNode.next is None:
21+
break
22+
lastNode = lastNode.next
23+
lastNode.next = newNode
24+
25+
def printList(self):
26+
27+
currentNode = self.head
28+
while True:
29+
if currentNode is None:
30+
break
31+
print(currentNode.data, end=' ')
32+
currentNode = currentNode.next
33+
34+
def reverseList(self, head) -> Node:
35+
36+
prev = None
37+
38+
while head:
39+
temp = head
40+
head = head.next
41+
temp.next = prev
42+
prev = temp
43+
44+
return prev
45+
46+
47+
from typing import Optional
48+
49+
# class Solution:
50+
51+
52+
firstNode = Node(1)
53+
linkedList = LinkedList()
54+
linkedList.insert(firstNode)
55+
secondNode = Node(2)
56+
linkedList.insert(secondNode)
57+
thirdNode = Node(3)
58+
linkedList.insert(thirdNode)
59+
fourthNode = Node(4)
60+
linkedList.insert(fourthNode)
61+
fifthNode = Node(5)
62+
linkedList.insert(fifthNode)
63+
linkedList.printList()
64+
65+
rev = linkedList.reverseList(firstNode)
66+
67+
print("\n")
68+
while rev:
69+
print(rev.data, end=' ')
70+
rev = rev.next

Linked_List/ll_implementation.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Node:
2+
3+
def __init__(self, data):
4+
self.data = data
5+
self.next = None
6+
7+
8+
class LinkedList:
9+
10+
def __init__(self):
11+
self.start = None
12+
13+
def deleteFirst(self):
14+
if self.start is None:
15+
print("Linked list is empty")
16+
else:
17+
temp = self.start
18+
self.start = self.start.next
19+
20+
def insertLast(self, value):
21+
newNode = Node(value)
22+
if self.start is None:
23+
self.start = newNode
24+
else:
25+
temp = self.start
26+
while temp.next is not None:
27+
temp = temp.next
28+
temp.next = newNode
29+
30+
def viewList(self):
31+
32+
if self.start is None:
33+
print("List is empty")
34+
else:
35+
temp = self.start
36+
while temp is not None:
37+
print(temp.data, end=' ')
38+
temp = temp.next
39+
40+
def reverseList(self):
41+
42+
prev = None
43+
44+
while self.start:
45+
46+
temp = self.start
47+
self.start = self.start.next
48+
temp.next = prev
49+
prev = temp
50+
return prev
51+
52+
53+
mylist = LinkedList()
54+
mylist.insertLast(10)
55+
mylist.insertLast(20)
56+
mylist.insertLast(30)
57+
mylist.insertLast(40)
58+
mylist.viewList()
59+
print("\n")
60+
reversList = mylist.reverseList()
61+
62+
# print(reversList.data)
63+
64+
while reversList:
65+
print(reversList.data,end=" ")
66+
reversList = reversList.next
67+
68+
# mylist.deleteFirst()
69+
70+
# mylist.viewList()
71+
# input()

Matrix/__init__.py

Whitespace-only changes.

Matrix/caa_mtr_73.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
from typing import List
3+
4+
# class Solution():
5+
#
6+
# def setZeroes(self,matrix:List[List[int]]):
7+
#
8+
# ROWS,COLS = len(matrix),len(matrix[0])
9+
# rowZero = False
10+
#
11+
# for r in range(ROWS):
12+
# for c in range(COLS):
13+
# if matrix[r][c] == 0:
14+
# matrix[0][c] = 0
15+
#
16+
# if r > 0:
17+
# matrix[r][0] = 0
18+
# else:
19+
# rowZero = True
20+
#
21+
# for i in range(1,ROWS):
22+
# for c in range(1,COLS):
23+
# if matrix[0][r] == 0 or matrix[r][0] == 0:
24+
# matrix[r][c]
25+
# if matrix[0][0] == 0:
26+
#
27+
# for r in range(ROWS):
28+
# matrix[r][0]=0
29+
#
30+
# if rowZero:
31+
# for c in range(COLS):
32+
# matrix[0][c]=0
33+
34+
class Solution:
35+
36+
def setZeroes(self, matrix: List[List[int]]): #-> None:
37+
"""
38+
Do not return anything, modify matrix in-place instead.
39+
"""
40+
rLen,cLen=len(matrix),len(matrix[0])
41+
42+
col=[1]*cLen
43+
44+
for r in range(rLen):
45+
flag = 0 # flag=1 INDICATES CURRENT ROW CONTAINS 0
46+
for c in range(cLen):
47+
48+
if matrix[r][c] == 0:
49+
flag = 1
50+
col[c] = 0
51+
52+
if flag: # flag=1 INDICATES CURRENT ROW CONTAINS 0, SET ENTIRE ROW TO 0
53+
matrix[r] = [0] * cLen
54+
55+
'''
56+
col INDICATES WHICH COLUMN HAS 0,
57+
BELOW LOOP SET THE CORRESPONDING COLUMN 0
58+
59+
'''
60+
for r in range(rLen):
61+
for c in range(cLen):
62+
if col[c] == 0:
63+
matrix[r][c] = 0
64+
65+
return matrix
66+
67+
matrix = [[1,1,1],[1,0,1],[1,1,1]]
68+
69+
print(Solution().setZeroes(matrix))

String/__init__.py

Whitespace-only changes.

String/gfg_find_rotations.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
Minimum rotations required to get the same string
3+
Difficulty Level : Medium
4+
Last Updated : 28 Dec, 2021
5+
Given a string, we need to find the minimum number of rotations required to get the same string.
6+
Examples:
7+
8+
9+
Input : s = "geeks"
10+
Output : 5
11+
12+
Input : s = "aaaa"
13+
Output : 1
14+
'''
15+
16+
def findRotations(string):
17+
18+
tmp = string + string
19+
20+
n = len(string)
21+
22+
for i in range (1,n+1):
23+
24+
#substring from i index of original string size
25+
substring = tmp[i:i+n]
26+
27+
# if substring matches with original string then we will come out of the loop
28+
29+
if substring == string:
30+
return i
31+
32+
return n
33+
34+
string="abc"
35+
print(findRotations(string))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Generate all rotations of a given string
3+
Difficulty Level : Easy
4+
Last Updated : 02 Nov, 2021
5+
Given a string S. The task is to print all the possible rotated strings of the given string.
6+
7+
Examples:
8+
9+
Input : S = "geeks"
10+
Output : geeks
11+
eeksg
12+
eksge
13+
ksgee
14+
sgeek
15+
16+
Input : S = "abc"
17+
Output : abc
18+
bca
19+
cab
20+
'''
21+
22+
#O(n^2)
23+
def printRotatedString(string):
24+
25+
n=len(string)
26+
27+
temp=string+string # abcabc
28+
29+
for i in range(n): # 0,1,2
30+
31+
for j in range(n): # 0,1,2 | 0
32+
33+
print( temp[i+j],end="") # a,b,c | b,c,a | c,a,b
34+
print()
35+
36+
if __name__ == "__main__":
37+
string = "abc"
38+
printRotatedString(string)

String/gfg_reversing_eq.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Reversing an Equation
3+
Difficulty Level : Basic
4+
Last Updated : 04 Jan, 2021
5+
Given a mathematical equation using numbers/variables and +, -, *, /. Print the equation in reverse.
6+
7+
Examples:
8+
9+
Input : 20 - 3 + 5 * 2
10+
Output : 2 * 5 + 3 - 20
11+
12+
Input : 25 + 3 - 2 * 11
13+
Output : 11 * 2 - 3 + 25
14+
15+
Input : a + b * c - d / e
16+
Output : e / d - c * b + a
17+
'''
18+
19+
20+
def reverseEquation(s):
21+
# Reverse String
22+
result = ""
23+
for i in range(len(s)):
24+
25+
# A space marks the end of the word
26+
if (s[i] == '+' or s[i] == '-' or s[i] == '/' or s[i] == '*'):
27+
28+
# insert the word at the beginning
29+
# of the result String
30+
result = s[i] + result
31+
32+
# insert the symbol
33+
else:
34+
# insert the word at the beginning
35+
result = s[i] + result
36+
return result
37+
38+
39+
if __name__ == '__main__':
40+
# Driver Code
41+
s = "a+b*c-d/e"
42+
print(reverseEquation(s))

0 commit comments

Comments
 (0)