Skip to content
Paras Gupta

Jan 7 - Daily Programming Diary

Diary, Programming1 min read

15:00

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.

naive-approach-longest-substring-without-repeating-characters.cpp
1int lengthOfLongestSubstring(string s) {
2 int maxLen = 0, curr = 0;
3
4 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}
17:00

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.

longest-substring-without-repeating-characters.cpp
1int lengthOfLongestSubstring(string s) {
2 int maxLen = 0;
3
4 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 -

  1. Go doesn't have inbuilt Max function for integers which is a shocker.
  2. Maps in Go are far easy to check for existence of a key. _ , check = map[key] will return the value of check as false if the value is not present.
  3. Go doesn't have char data type, so byte is used to store a char.
longest-substring-without-repeating-characters.go
1// Had to implement the Max function myself
2func Max(x, y int) int {
3 if x < y {
4 return y
5 }
6 return x
7}
8
9func lengthOfLongestSubstring(s string) int {
10 var maxLen, i int = 0, 0
11 u_map := make(map[byte]int)
12 for j := 0; j < len(s); j++ {
13 // THIS
14 _, 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 + 1
24 }
25
26 return maxLen
27}
21:00

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.

EOD Checklist

  • Go
  • Leetcode January Challenge
  • Codechef Long Challenge
  • Raahee
  • This blog
© 2022 by Paras Gupta. All rights reserved.
Theme by LekoArts