You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Kangli/README.md
+30-1Lines changed: 30 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,41 @@
1
1
Keeping a record of thoughts behind solving certain problems
2
2
3
+
1/4/18 Making some catch-up edits.
4
+
5
+
**387: First Unique Character in a String-**
6
+
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
7
+
Examples:
8
+
s = "leetcode"
9
+
return 0.
10
+
s = "loveleetcode",
11
+
return 2.
12
+
Note: You may assume the string contain only lowercase letters.
13
+
14
+
Thoughts: Straightforward use of a hash to store the counts of each letter in the string. Then iterate the string again from the beginning and return the first letter with count of 1.
**443: String Compression-** Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying the input array in-place, return the new length of the array.
18
+
19
+
Example:
20
+
Input:
21
+
["a","a","b","b","c","c","c"]
22
+
Output:
23
+
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
24
+
Explanation:
25
+
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
26
+
27
+
Thoughts: Easy if not doing it in place. I made an extra array, added the character and added its count if count > 1.
**219: Contains Duplicate II-** Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
5
32
Thoughts:
6
33
Use a dictionary to store each integer and its index in nums as key, value pairs.
7
34
If a value is already in the dictionary, check whether index difference <= k.
8
35
9
36
Implementation: did not pass on first 2 tries, failed for [1, 0, 1, 1, ], 1 (true got false). Need to update
10
-
index if value already in dictionary but i - d[n] > k.
37
+
index if value already in dictionary but i - d[n] > k.
**49: Group Anagrams-** Given an array of strings, group anagrams together.
13
41
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
@@ -18,6 +46,7 @@ Return:
18
46
["bat"]
19
47
]
20
48
Thoughts: Using a dictionary (hash), insert each sorted string into the dictionary as keys, using the original string as the value. Need the values to be an array, so initialize the value of each key as [] and add the original string directly to [] each time. Could also use defaultdict (python specific).
0 commit comments