Skip to content

Commit 0c8ba75

Browse files
committed
New Strings InterviewBit Solution
1 parent 56a2974 commit 0c8ba75

8 files changed

+183
-2
lines changed

LINKEDLIST/Nth-Node-From-Last.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Node:
2+
def __init__(self,data):
3+
self.data = data
4+
self.next = None
5+
6+
class LinkedList:
7+
def __init__(self):
8+
self.head = None
9+
10+
def push(self, new_data):
11+
new_node = Node(new_data)
12+
new_node.next = self.head
13+
self.head = new_node
14+
15+
def printNthFromEnd(self,n):
16+
main_ptr = self.head
17+
ref_ptr = self.head
18+
19+
count = 0
20+
if (self.head is not None):
21+
while(count < n):
22+
ref_ptr = ref_ptr.next
23+
count += 1
24+
while (ref_ptr is not None):
25+
main_ptr = main_ptr.next
26+
ref_ptr = ref_ptr.next
27+
return main_ptr.data
28+
29+
ll = LinkedList()
30+
ll.push(20)
31+
ll.push(4)
32+
ll.push(15)
33+
ll.push(35)
34+
print ll.printNthFromEnd(4)

Math/Sort-An-array-of-0_1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def sort012(arr):
2+
n = len(arr)
3+
hi = n - 1
4+
low = 0
5+
high = n - 1
6+
mid = 0
7+
while mid <= high:
8+
if arr[mid] == 0:
9+
arr[low], arr[mid] = arr[mid], arr[low]
10+
low += 1
11+
mid += 1
12+
elif arr[mid] == 1:
13+
arr[mid], arr[high] = arr[high], arr[mid]
14+
high -= 1
15+
return arr
16+
17+
arr = [0,1,1,0,1,1,0,0,0,1]
18+
print sort012(arr)

String/Add-binary-strings.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
3+
Given two binary strings, return their sum (also a binary string).
4+
'''
5+
class Solution:
6+
# @param A : string
7+
# @param B : string
8+
# @return a strings
9+
def addBinary(self, A, B):
10+
a = int(A,2)
11+
b = int(B,2)
12+
c = bin(a+b)[2:]
13+
return c

String/Implement-StrStr.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution:
2+
# @param haystack : string
3+
# @param needle : string
4+
# @return an integer
5+
def strStr(self, txt, pat):
6+
def computeLPSArray(pat,m,lps):
7+
j = 0 #length of previous lps
8+
lps[0] = 0
9+
i = 1
10+
while i < m:
11+
if pat[i] == pat[j]:
12+
j += 1
13+
lps[i] = j
14+
i += 1
15+
else:
16+
if j != 0:
17+
j = lps[j-1]
18+
else:
19+
lps[i] = 0
20+
i += 1
21+
m = len(pat)
22+
n = len(txt)
23+
#Now create a lps array that will hold the longest prefix suffix value
24+
lps = [0]*m
25+
j = 0 #index for pat
26+
computeLPSArray(pat,m,lps)
27+
28+
i = 0 #index For txt[]
29+
30+
while i < n:
31+
if pat[j] == txt[i]:
32+
i += 1
33+
j += 1
34+
if j == m:
35+
return (i-j)
36+
elif i < n and pat[j] != txt[i]:
37+
if j != 0:
38+
j = lps[j-1]
39+
else:
40+
i += 1
41+
return -1

String/Integer-To-Roman.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def integerToRoman(num):
2+
m = ["", "M" ,"MM", "MMM"]
3+
c = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
4+
x = ["", "X", "XX", "XXX", "XL", "L" ,"LX", "LXX", "LXXX", "XC"]
5+
i = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
6+
thousands = m[num/1000]
7+
hundreds = c[(num%1000)/100]
8+
tens = x[(num%100)/10]
9+
ones = i[num%10]
10+
ans = thousands + hundreds + tens + ones
11+
return ans
12+
13+
print integerToRoman(3549)

String/KMP-Algo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ def computeLPSArray(pat,m,lps):
3939
i += 1
4040

4141
#Now time to test
42-
txt = "ABABDABACDABABCABAB"
43-
pat = "ABABCABAB"
42+
txt = "abdefgh"
43+
pat = "defg"
4444
KMPSearch(pat,txt)

String/Power-of-2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Find if Given number is power of 2 or not.
3+
More specifically, find if given number can be expressed as 2^k where k >= 1.
4+
'''
5+
class Solution:
6+
# @param A : string
7+
# @return an integer
8+
def power(self, num):
9+
num = int(num)
10+
if num == 1:
11+
return 0
12+
while num != 1:
13+
if num%2 != 0:
14+
return 0
15+
num = num/2
16+
return 1

String/addBinaryString.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Given two binary strings, return their sum (also a binary string).
3+
'''
4+
def binarySum(a,b):
5+
n1 = len(a)
6+
n2 = len(b)
7+
if n1 > n2:
8+
b = "0"*(n1-n2) + b
9+
elif n2 > n1:
10+
a = "0"*(n2-n1) + a
11+
carry = 0
12+
a = list(a)
13+
b = list(b)
14+
for i in range(n2-1,-1,-1):
15+
if (a[i] == b[i]) and a[i] == '0':
16+
a[i] = str(0+carry)
17+
carry = 0
18+
elif (a[i] == b[i]) and a[i] == '1':
19+
a[i] = str(0 + carry)
20+
carry = 1
21+
else:
22+
if carry == 1:
23+
a[i] = str(0)
24+
carry = 1
25+
else:
26+
a[i] = str(1)
27+
carry = 0
28+
if carry == 1:
29+
a = ["1"] + a
30+
31+
return ''.join(a)
32+
33+
a = "1110000000010110111010100100111"
34+
b = "101001"
35+
36+
print binarySum(a,b)
37+
38+
39+
'''
40+
Another Approach
41+
'''
42+
def add(a,b):
43+
a = int(a,2)
44+
b = int(b,2)
45+
c = bin(a+b)[2:]
46+
return c

0 commit comments

Comments
 (0)