— Diary, Programming — 1 min read
Don't really have the motivation to work today, but I guess I'll at least try today's Leetcode challenge.
Naive approach: To check substring starting from every position and get the maximum length. The time complexity will be O(n^2)
, which is causing TLE. So I guess I'll have to optimize it.
1int lengthOfLongestSubstring(string s) {2 int maxLen = 0, curr = 0;34 for (int i = 0; i < s.length(); i++) {5 unordered_map<char, int> umap;6 curr = 0;7 for (int j = i; j < s.length(); j++) {8 if (umap[s[j]] == 1) {9 curr = 0;10 } else {11 umap[s[j]] = 1;12 curr++;13 maxLen = max(maxLen, curr);14 }15 }16 }17 return maxLen;18}
Took a break, and figured out a better approach in O(n)
. Check when the previous occurrence of that character occurred and get max length based on that.
1int lengthOfLongestSubstring(string s) {2 int maxLen = 0;34 int i = 0;5 unordered_map<char, int> umap;6 for (int j = 0; j < s.length(); j++) {7 if (umap.find(s[j]) != umap.end()) {8 i = max(umap[s[j]], i);9 }10 maxLen = max(maxLen, j - i + 1);11 umap[s[j]] = j + 1;12 }13 return maxLen;14}
Now I have no idea how to implement maps in Go, so today will be a first.
Okay, done with that.
Lessons -
_ , check = map[key]
will return the value of check
as false if the value is not present.char
data type, so byte
is used to store a char.1// Had to implement the Max function myself2func Max(x, y int) int {3 if x < y {4 return y5 }6 return x7}89func lengthOfLongestSubstring(s string) int {10 var maxLen, i int = 0, 011 u_map := make(map[byte]int)12 for j := 0; j < len(s); j++ {13 // THIS14 _, check = u_map[s[j]]15 if check {16 i = Max(u_map[s[j]], i)17 }18 /* OR 19 temp := u_map[s[j]]20 i = Max(temp, i)21 */22 maxLen = Max(maxLen, j-i+1)23 u_map[s[j]] = j + 124 }2526 return maxLen27}
I was thinking about writing a cron job for making my <HideContent />
Component more secure. The current implementation allows anyone to see the source code and check the password which is not what I want.
So, I can store an encrypted key in firebase that will change according to a set parameter everyday (using a cron job) and then my password will be checked against that encrypted key. Sounds secure!
The only issue with that approach is that I don't have a server right now to host the cron job on. I can think about using my PC, but then it's unpredictable if my PC will be turned on at the time. I'll probably go with Heroku and that hobby dyno, guess it'll be fine.