#CodeEveryday — My DSA Journey | Day 30 🧩 Problem Solved: Longest Repeating Character Replacement (LeetCode #424) 💭 What I Learned: Used the Sliding Window technique with a HashMap to find the longest substring that can be transformed into repeating characters after at most k replacements. Key idea: 👉 A window is valid if: (window size − frequency of most common character) ≤ k Approach: ✔️ Expanded the window using the right pointer ✔️ Tracked character frequencies using HashMap ✔️ Maintained the count of the most frequent character ✔️ Shrunk the window whenever replacements required exceeded k This helped me understand how frequency tracking can optimize substring problems efficiently. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) (only uppercase English letters) ⚡ Key Takeaways: ✔️ Sliding Window becomes powerful when combined with frequency analysis ✔️ Maintaining the maximum frequency avoids unnecessary recomputation ✔️ Many substring optimization problems follow a “valid window” pattern 💻 Language Used: Java ☕ 📘 Concepts: Sliding Window · HashMap · Frequency Counting · Strings · Two Pointers #CodeEveryday #DSA #LeetCode #Java #SlidingWindow #Strings #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
Longest Repeating Character Replacement LeetCode Solution
More Relevant Posts
-
#CodeEveryday — My DSA Journey | Day 32 🧩 Problem Solved: Count Number of Nice Subarrays (LeetCode #1248) 💭 What I Learned: Used the Sliding Window technique with the “at most k” approach to count subarrays containing exactly k odd numbers. Key idea: 👉 Subarrays with exactly k odd numbers = atMost(k) − atMost(k−1) Approach: ✔️ Counted subarrays having at most k odd numbers ✔️ Repeated the same for k−1 ✔️ Subtracted both counts to get the final answer Maintained a dynamic window by tracking the count of odd numbers inside the current subarray. This strengthened my understanding of transforming exact-count problems into at-most-count problems. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ “Exactly k” subarray problems can often be solved using: exactly(k) = atMost(k) − atMost(k−1) ✔️ Sliding Window efficiently handles dynamic subarray constraints ✔️ Reusing the same logic reduces complexity and improves clarity 💻 Language Used: Java ☕ 📘 Concepts: Sliding Window · Two Pointers · Subarray Counting · Prefix Logic #CodeEveryday #DSA #LeetCode #Java #SlidingWindow #Subarrays #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 33 🧩 Problem Solved: Maximum Points You Can Obtain from Cards (LeetCode #1423) 💭 What I Learned: Used the Sliding Window technique to maximize points obtained by picking exactly k cards from either end of the array. Approach: ✔️ Initially took the first k cards from the left ✔️ Gradually shifted the window by removing one card from the left side and adding one from the right side ✔️ Tracked the maximum score during each transition Instead of checking all combinations, efficiently explored valid possibilities in linear time. This strengthened my understanding of window shifting between two ends of an array. ⏱ Time Complexity: O(k) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ Sliding Window can be applied even when elements are chosen from both ends ✔️ Maintaining running sums avoids repeated calculations ✔️ Incremental updates lead to efficient optimization 💻 Language Used: Java ☕ 📘 Concepts: Sliding Window · Arrays · Prefix/Suffix Logic · Optimization #CodeEveryday #DSA #LeetCode #Java #SlidingWindow #Arrays #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 40 🧩 Problem Solved: Remove Nth Node From End of List (LeetCode #19) 💭 What I Learned: Used linked list traversal to remove the n-th node from the end of the list. Approach: ✔️ First traversed the list to calculate its length ✔️ Determined the position of the node to remove from the beginning ✔️ Traversed again to reach that node ✔️ Updated pointers to skip the target node Handled important edge cases such as removing the head node. This strengthened my understanding of linked list traversal and pointer manipulation. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ Linked list problems often require careful pointer updates ✔️ Edge cases (like deleting the head) are crucial ✔️ Multiple traversals can still achieve optimal linear complexity 💻 Language Used: Java ☕ 📘 Concepts: Linked List · Pointer Manipulation · Traversal · Node Deletion #CodeEveryday #DSA #LeetCode #Java #LinkedList #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
Day 134: Keeping It Simple ✅📊 Problem 2784: Check if Array is Good After yesterday's intense difference array logic, today was a welcome return to basic validation and set mechanics. The goal was to check if an array is a base permutation of length n with one extra copy of n. The Strategy: • Bound Check: The maximum valid number in the array must equal nums.length−1. Anything larger is an immediate false. • Set Filtering: I used a HashSet to keep track of unique numbers. • Duplicate Logic: The only allowed duplicate is the maximum element n. If any other number repeats, or if n repeats more than once, the array is invalid. I went with Java today for a quick, clean O(N) hash set approach. Day 134—consistency doesn't always require a complex algorithm, just solid logic. 🚀 #LeetCode #Java #DataStructures #Algorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 35 🧩 Problem Solved: Middle of the Linked List (LeetCode #876) 💭 What I Learned: Used the Slow and Fast Pointer technique to efficiently find the middle node of a linked list. Approach: ✔️ Slow pointer moves one step at a time ✔️ Fast pointer moves two steps at a time ✔️ When fast pointer reaches the end, slow pointer reaches the middle Handled both odd and even length linked lists correctly. This strengthened my understanding of two-pointer traversal techniques in linked lists. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ Slow & Fast pointers are very useful in linked list problems ✔️ Two-pointer traversal avoids extra space usage ✔️ Pointer movement patterns help optimize traversal problems efficiently 💻 Language Used: Java ☕ 📘 Concepts: Linked List · Two Pointers · Slow & Fast Pointer Technique #CodeEveryday #DSA #LeetCode #Java #LinkedList #TwoPointers #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
🧠 Day 224— Rotate String 🔁📚 Today Revised & solved a simple yet clever string problem: checking if one string can become another through rotation. 📌 Problem Goal Given two strings s and goal, determine if s can be rotated to become goal. ✔️ Rotation means shifting characters cyclically ✔️ Lengths must be equal 🔹 Core Idea If a string is rotated, it will always appear as a substring of itself doubled. 🔹 Approach Thinking 1️⃣ Length Check → If lengths differ → impossible 2️⃣ Key Trick → Concatenate string with itself → Now all possible rotations exist inside it 3️⃣ Substring Check → If goal exists in (s + s) → valid rotation 🧠 Key Learning ✔️ Many string problems hide simple patterns ✔️ Doubling trick is powerful for rotation problems ✔️ Substring search can simplify complex logic 💡 Today’s Realization This problem connects with: String matching Rotation-based problems Pattern searching Same idea → transform → search → validate. 🚀 Momentum Status: Strong clarity in string patterns. On to Day 225. #DSA #Strings #PatternMatching #LeetCode #ProblemSolving #Java #CodingJourney #ConsistencyWins
To view or add a comment, sign in
-
-
Today I tackled the Sequence Equation problem, which involves working with permutations and index mapping. 🔍 Problem Insight: Given a permutation p, find values of y such that: 👉 p[p[y]] = x for all x from 1 to n 💡 Key Idea: Instead of brute force, I used: Position mapping for fast lookup Double indexing trick (pos[pos[x]]) 🚀 Why this works: It reduces complexity from O(n²) to O(n), making it highly efficient. 🧠 What I learned: Importance of inverse mapping Optimizing lookup operations Writing clean and efficient Java code 📌 Example: Input: [5, 2, 1, 3, 4] Output: [4, 2, 5, 1, 3] 🔖 Tags #Java #DataStructures #Algorithms #CodingChallenge #ProblemSolving #100DaysOfCode #DeveloperJourney #SoftwareDeveloper #Hackerrank #CodingPractice #TechLearning
To view or add a comment, sign in
-
-
🚀 Day68 🚀 DSA Problem Solved: Search a Word in a 2D Grid Today I solved an interesting pattern-searching problem in Java: 🔍 Problem Statement: Given a 2D grid of characters and a target word, find all starting positions where the word exists. The word can be searched in 8 directions: ➡ Left ⬅ Right ⬆ Up ⬇ Down ↖ Diagonal Up-Left ↗ Diagonal Up-Right ↙ Diagonal Down-Left ↘ Diagonal Down-Right 💡 Approach I used: ✔ Traverse each cell as a possible starting point ✔ Check if the first character matches ✔ Explore all 8 directions one by one ✔ Continue matching until the full word is found 🧠 Key Learning: This problem strengthens concepts of: • Matrix Traversal • Direction Arrays • Boundary Checking • Pattern Matching 📊 Complexity Analysis: ⏱ Time Complexity: O(N × M × L × 8) 📦 Space Complexity: O(1) For practice, I used the word "AKASH" as the search pattern in the grid. Problems like this improve logical thinking and help build strong foundations in grid-based algorithms. #Java #DSA #Programming #CodingInterview #ProblemSolving #100DaysOfCode #DataStructures #Algorithms
To view or add a comment, sign in
-
Day 91/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Word Break A classic Dynamic Programming problem that strengthens string partitioning intuition. Problem idea: Check if a string can be segmented into valid words from a given dictionary. Key idea: Use DP to track valid prefixes. Why? • Brute force tries all partitions → exponential • DP helps store results of subproblems • If a prefix is valid, we build on it How it works: • Create a DP array dp[i] → can substring [0..i-1] be formed? • Initialize dp[0] = true (empty string is valid) • For each index i, check all possible splits j < i • If dp[j] == true and substring (j → i) exists in dictionary → mark dp[i] = true Optimization used: • HashSet for O(1) lookup • Limit substring length using max word length Time Complexity: O(n²) Space Complexity: O(n) Big takeaway: Whenever you see string segmentation / partition problems, think DP on prefixes. 🔥 This pattern is widely used in problems like sentence formation, decoding, etc. Day 91 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #DynamicProgramming #Strings #DP #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 27 🧩 Problem Solved: Construct Binary Search Tree from Preorder Traversal (LeetCode #1008) 💭 What I Learned: Constructed a Binary Search Tree (BST) using preorder traversal by leveraging the relationship between: 👉 Preorder Traversal = Root → Left → Right 👉 Inorder Traversal of BST = Sorted Order Approach: ✔️ Created inorder traversal by sorting the preorder array ✔️ Used a HashMap to store inorder indices for fast lookup ✔️ Recursively built left and right subtrees using preorder + inorder ranges This strengthened my understanding of tree construction using traversal techniques. ⏱ Time Complexity: O(n log n) (due to sorting) 🧠 Space Complexity: O(n) ⚡ Key Takeaways: ✔️ BST inorder traversal is always sorted ✔️ Tree construction problems rely heavily on traversal properties ✔️ HashMap helps optimize inorder index lookup from O(n) → O(1) 💻 Language Used: Java ☕ 📘 Concepts: BST · Tree Construction · Recursion · Preorder Traversal · Inorder Traversal #CodeEveryday #DSA #LeetCode #Java #BST #Trees #Recursion #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-