👋 Empowering Algorithms : Google, Amazon Interview 👋

-
Introduction:
- Explore the intricacies of the Two Pointer Technique, a formidable strategy in algorithm design.
- Harness the power of dual pointers to navigate through arrays, strings, and beyond.
-
Target Sum Challenge (Asked in Google Interviews):
-
Task: Identify the minimum number of elements required to add up to a specific target sum.
-
Utilize Two Pointer Technique to minimize complexity and maximize efficiency.
-
Code Example:
class Solution { public: int minElements(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; long long currentSum = 0; for (int num : nums) { currentSum += num; } long long diff = abs(target - currentSum); int count = 0; while (diff > 0) { if (diff >= nums[right]) { diff -= nums[right]; right--; } else { diff = 0; } count++; } return count; } };
-
-
Longest Substring Without Repeating Characters (Asked in Amazon Interviews):
-
Task: Find the longest substring without repeating characters.
-
Employ Two Pointer Technique to traverse through the string and identify substrings.
-
Code Example:
class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map<char, int> charIndex; int left = 0, maxLength = 0; for (int right = 0; right < s.length(); ++right) { if (charIndex.find(s[right]) != charIndex.end() && charIndex[s[right]] >= left) { left = charIndex[s[right]] + 1; } charIndex[s[right]] = right; maxLength = max(maxLength, right - left + 1); } return maxLength; } };Got it! Here's a challenging approach to tackle the Longest Substring with At Most K Distinct Characters problem:
-
Approach: Sliding Window with Frequency Map
-
Initialize Variables:
- Initialize
leftandrightpointers to traverse the string. - Initialize an empty map (
charFreq) to track the frequency of characters within the window. - Initialize a variable
maxLengthto store the length of the longest substring.
- Initialize
-
Sliding Window Algorithm:
- Move the
rightpointer to the right until the window contains more thankdistinct characters. - While traversing, update the character frequencies in the
charFreqmap.
- Move the
-
Track Maximum Length:
- While the window contains at most
kdistinct characters, update themaxLengthwith the length of the current window.
- While the window contains at most
-
Move Left Pointer:
- Move the
leftpointer to the right until the window contains less thankdistinct characters. - While moving, decrement the frequency of characters in the
charFreqmap and remove characters with frequency 0.
- Move the
-
Repeat:
- Repeat steps 2-4 until the
rightpointer reaches the end of the string.
- Repeat steps 2-4 until the
-
Return Result:
- Return the
maxLengthas the length of the longest substring with at mostkdistinct characters.
- Return the
Complexity Analysis:
- Time Complexity: O(N), where N is the length of the input string
s. - Space Complexity: O(K), where K is the number of distinct characters in the string
s.
This approach efficiently help you to solves the problem using the sliding window technique combined with a frequency map, providing an optimal solution for LeetCode challenges and coding interviews.

