Skip to content

Commit f9a19d9

Browse files
author
Shashank Singh
committed
May 2020 leetcode challenge
1 parent 7d99532 commit f9a19d9

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3320/
3+
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
4+
5+
Examples:
6+
7+
s = "leetcode"
8+
return 0.
9+
10+
s = "loveleetcode",
11+
return 2.
12+
13+
Note: You may assume the string contain only lowercase letters.
14+
15+
"""
16+
from collections import OrderedDict
17+
18+
19+
class Solution(object):
20+
def firstUniqChar(self, s: str) -> int:
21+
"""
22+
:type s: str
23+
:rtype: int
24+
"""
25+
26+
character_frequency = OrderedDict()
27+
for index in range(len(s)):
28+
c = s[index]
29+
character_frequency[c] = ((character_frequency.get(c, (0, -1)))[0] + 1, index)
30+
for k, v in character_frequency.items():
31+
if v[0] == 1:
32+
return v[1]
33+
return -1
34+
35+
36+
if __name__ == '__main__':
37+
print(Solution().firstUniqChar('loveleetcode'))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3322/
3+
In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.
4+
5+
Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
6+
7+
We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
8+
9+
Return true if and only if the nodes corresponding to the values x and y are cousins.
10+
"""
11+
12+
13+
# Definition for a binary tree node.
14+
class TreeNode:
15+
def __init__(self, val=0, left=None, right=None):
16+
self.val = val
17+
self.left = left
18+
self.right = right
19+
20+
21+
class Solution:
22+
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
23+
24+
global_var = {}
25+
26+
def traversal(node, depth, parent_val):
27+
if node is None:
28+
return
29+
if node.val in [x, y]:
30+
global_var[node.val] = {
31+
'parent': parent_val,
32+
'depth': depth
33+
}
34+
traversal(node.left, depth + 1, node.val)
35+
traversal(node.right, depth + 1, node.val)
36+
37+
traversal(root, 0, None)
38+
if global_var[x]['depth'] == global_var[y]['depth']:
39+
if global_var[x]['parent'] != global_var[y]['parent']:
40+
return True
41+
42+
return False

Leetcode/MAY2020challenge/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)