Skip to content

Commit b16ef28

Browse files
authored
Merge pull request #3 from adhocore/code
2 parents aa26203 + a13803f commit b16ef28

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
Just some (n|l)eet code solutions in Python.
44

55
- [Container with max water](./containermaxwater.py)
6+
- [Eval reverse Polish notation](./evalrpn.py)
67
- [Group anagrams together](./groupanagrams.py)
78
- [Is Anagram](./isanagram.py)
89
- [Longest consecutive seq](./longestconsecutiveseq.py)
10+
- [Longest non repeat substr len](./longestsubstrlen.py)
911
- [Product except self](./productexceptself.py)
1012
- [Stock max profit - single txn](./stockmaxprofit.py)
13+
- [String permutations as substring](./substrpermute.py)
1114
- [Three sum to zero](./threesumzero.py)
1215
- [Top K frequent items](./topkfrequent.py)
1316
- [Trap rain water](./traprainwater.py)
1417
- [Two sum to target](./twosum.py)
18+
- [Valid parentheses](./validparens.py)
1519

1620
## Running
1721

evalrpn.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def evalRPN(self, tokens):
3+
stak = []
4+
for i in range(len(tokens)):
5+
if not tokens[i] in "+-*/":
6+
stak.append(int(tokens[i]))
7+
continue
8+
a, b = stak.pop(), stak.pop()
9+
if tokens[i] == "+":
10+
stak.append(b + a)
11+
elif tokens[i] == "-":
12+
stak.append(b - a)
13+
elif tokens[i] == "*":
14+
stak.append(b * a)
15+
elif tokens[i] == "/":
16+
stak.append(int(b / a))
17+
return stak.pop()
18+
19+
20+
if __name__ == "__main__":
21+
s = Solution()
22+
print(s.evalRPN(["2", "1", "+", "3", "*"]))
23+
# => 9 => (2+1)*3
24+
print(
25+
s.evalRPN(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"])
26+
)
27+
# => 22
28+
print(s.evalRPN(["4", "-2", "/", "2", "-3", "-", "-"]))
29+
# => -7

longestsubstrlen.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s):
3+
left, right, maxl, win = 0, 0, 0, {}
4+
while right < len(s):
5+
if not win.get(s[right]):
6+
win[s[right]] = (True,)
7+
maxl = max(right - left + 1, maxl)
8+
else:
9+
while s[left] != s[right]:
10+
win[s[left]], left = False, left + 1
11+
left += 1
12+
right += 1
13+
return maxl
14+
15+
16+
if __name__ == "__main__":
17+
s = Solution()
18+
print(s.lengthOfLongestSubstring("abcdabecbb"))
19+
# => 5 => cdabe

substrpermute.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def checkStrPermuteSub(self, s1, s2):
3+
s1c, s2c = {}, {}
4+
for c in s1:
5+
s1c[c] = s1c.get(c, 0) + 1
6+
for c in s2:
7+
if c in s1c:
8+
s2c[c] = s2c.get(c, 0) + 1
9+
if s1c == s2c:
10+
return True
11+
else:
12+
s2c = {}
13+
return False
14+
15+
16+
if __name__ == "__main__":
17+
s = Solution()
18+
print(s.checkStrPermuteSub("abcd", "zdcbax"))
19+
# => True

validparens.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def isValidParens(self, s):
3+
stak = []
4+
pair = {")": "(", "}": "{", "]": "["}
5+
for c in s:
6+
if c == "(" or c == "{" or c == "[":
7+
stak.append(c)
8+
elif stak.pop() != pair[c]:
9+
return False
10+
return len(stak) == 0
11+
12+
13+
if __name__ == "__main__":
14+
s = Solution()
15+
print(s.isValidParens("([]{})"))
16+
# => True

0 commit comments

Comments
 (0)