Skip to content

Commit 56a2974

Browse files
committed
New Sol
1 parent da3d19c commit 56a2974

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

Arrays/neigh.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#Solution not completed
2+
from collections import Counter
3+
def neigh(st):
4+
toCheck = "neigh"
5+
lis = []
6+
for i in toCheck:
7+
lis.append(st.count(i))
8+
min_ =min(lis)
9+
if len(set(lis)) == 1:
10+
return min_
11+
else:
12+
return min_
13+
14+
st = 'n'
15+
print neigh(st)

String/Count-and-Say.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
The count-and-say sequence is the sequence of integers beginning as follows:
3+
1, 11, 21, 1211, 111221, ...
4+
1 is read off as one 1 or 11.
5+
11 is read off as two 1s or 21.
6+
7+
21 is read off as one 2, then one 1 or 1211.
8+
9+
Given an integer n, generate the nth sequence.
10+
11+
Note: The sequence of integers will be represented as a string.
12+
13+
Example:
14+
15+
if n = 2,
16+
the sequence is 11.
17+
18+
'''
19+
def countAndSay(n):
20+
s = '1'
21+
for i in range(n-1):
22+
23+
let ,temp ,count = s[0], '', 0
24+
for l in s:
25+
if let == l:
26+
count += 1
27+
else:
28+
temp += str(count) + let
29+
let = l
30+
count = 1
31+
temp += str(count) + let
32+
s = temp
33+
return s
34+
35+
print countAndSay(4)

String/Longest-Common-Prefix.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Write a function to find the longest common prefix string amongst an array of strings.
3+
4+
Longest common prefix for a pair of strings S1 and S2 is the longest string S which is the prefix of both S1 and S2.
5+
6+
As an example, longest common prefix of "abcdefgh" and "abcefgh" is "abc".
7+
8+
Given the array of strings, you need to find the longest S which is the prefix of ALL the strings in the array.
9+
'''
10+
def checkPrefix(prefix,st):
11+
j = 0
12+
len_ = 0
13+
for i in range(len(prefix)):
14+
if st[i] != prefix[j]:
15+
break
16+
j += 1
17+
len_ +=1
18+
return prefix[0:len_]
19+
20+
def LCP(arr):
21+
min_ = len(arr[0])
22+
index = 0
23+
for i in range(1,len(arr)):
24+
m = len(arr[i])
25+
if m < min_:
26+
min_ = m
27+
index = i
28+
prefix = arr[index]
29+
for i in arr:
30+
prefix = checkPrefix(prefix,i)
31+
32+
return prefix
33+
34+
arr = [ "aaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ]
35+
print LCP(arr)
36+
37+
38+
39+
40+
41+
42+
#Divide and Conquer Approach
43+
def commonPrefix(st1,st2):
44+
j = 0
45+
len = 0
46+
for i in range(min(len(st1),lem(st2))):
47+
if st1[i] != st2[j]:
48+
break
49+
else:
50+
j += 1
51+
len += 1
52+
return st1[0:len]
53+
54+
55+
def LongestCommonPrefix(arr):
56+
low = 0
57+
high = len(arr)
58+
if low == high:
59+
return arr[low]
60+
if low < high:
61+
mid = (high - low)/2
62+
st1 = LongestCommonPrefix(arr,low,mid)
63+
st2 = LongestCommonPrefix(arr,mid+1,high)
64+
65+
return commonPrefix(st1,st2)

String/Reverse-Strings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'''Given an input string, reverse the string word by word.'''
2+
class Solution:
3+
# @param A : string
4+
# @return string
5+
def reverseWords(self, a):
6+
7+
s = a.split()
8+
s.reverse()
9+
res = " ".join(s)
10+
return res

0 commit comments

Comments
 (0)