`3. Longest Substring Without Repeating CharactersGiven a string s, find the length of the longest substring without repeating characters.Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.`

Maintain two pointers left and right. Iterate over array using right pointer. Keep inserting character at right into a map along with index.

**If we encounter a duplicate character, move left to current character's index + 1 (or right + 1).**

Calculate result as `result = Math.max(result, right-left+1);`

and keep track of it.

`class Solution { public int lengthOfLongestSubstring(String s) { int left=0, result=0; Map<Character, Integer> map = new HashMap<>(); for(int right=0; right<s.length(); right++) { char c = s.charAt(right); if(map.containsKey(c)) { left = Math.max(left, map.get(c) + 1); } map.put(c, right); result = Math.max(result, right-left+1); } return result; }}`

This problem expects us to know how to:

Iterate over array and use hash map

Use two pointers approach

Calculate substring length using formula (right - left + 1)

Handle corner case of

`2. Add Two NumbersYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.You may assume the two numbers do not contain any leading zero, except the number 0 itself.Example 1:2 -> 4 -> 35 -> 6 -> 4---7 -> 0 -> 8Input: l1 = [2,4,3], l2 = [5,6,4]Output: [7,0,8]Explanation: 342 + 465 = 807.Example 2:Input: l1 = [0], l2 = [0]Output: [0]Example 3:Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]Output: [8,9,9,9,0,0,0,1]`

The Intuition is to iterate through two linked lists representing non-negative integers in reverse order, starting from the least significant digit. It performs digit-wise addition along with a carry value and constructs a new linked list to represent the sum. The process continues until both input lists and the carry value are exhausted. The resulting linked list represents the sum of the input numbers in the correct order.

`class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0), curr = dummy; int carry = 0; while (l1 != null || l2 != null || carry != 0) { int digit1 = (l1 != null) ? l1.val : 0; int digit2 = (l2 != null) ? l2.val : 0; int sum = digit1 + digit2 + carry; int digit = sum % 10; carry = sum / 10; curr.next = new ListNode(digit); curr = curr.next; l1 = (l1 != null) ? l1.next : null; l2 = (l2 != null) ? l2.next : null; } return dummy.next; }}`

This problem expects us to know how to:

Iterate over multiple linked lists at the same time

Concept of addition on per digit basis

Use of dummy head node to avoid null checks when iterating over linked lists

`1. Two SumGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order.Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].Input: nums = [3,2,4], target = 6Output: [1,2]Input: nums = [3,3], target = 6Output: [0,1`

Maintain a Hash map to store the complement of each number in the array. For each number, check if the complement value is present in the map.

If present, return the index of current value and complement value. If not present, calculate the complement and store it in the hash map along with the index.

`class Solution { public int[] twoSum(int[] nums, int target) { var cache = new HashMap<Integer, Integer>(); for(var i=0; i<nums.length; i++) { if(cache.containsKey(nums[i])) { return new int[]{cache.get(nums[i]), i}; } cache.put(target - nums[i], i); } return new int[]{}; }}`

This problem expects us to know how to:

Iterate over an array

Use hashmap to store and retrieve values in constant time O(1)

I am a backend developer with 12 years of experience in Java, Python, Go and data engineering. I have worked on a wide variety of roles and in multiple companies ranging from 10 ppl startups to multi national companies.

In the next 2 months, I plan to complete 2 series:

Code repository for the solutions: link

]]>