Day 23/100 – DSA Challenge 🚀 📌 Problem: Longest Repeating Character Replacement 📌 Pattern: Advanced Sliding Window 📌 Language: Java Today I worked on strengthening my sliding window understanding. The goal was to find the longest substring that can be converted into a single repeating character after at most k replacements. Key idea: Maintain a window using two pointers Track character frequency inside the window Keep track of the highest frequency character (maxFreq) If (windowSize - maxFreq) > k, shrink the window This problem taught me: How to control window validity mathematically Why we don’t decrease maxFreq when shrinking How small mistakes (like double increment or wrong indexing) can break logic ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #DSA #Java #LeetCode #SlidingWindow #CodingJourney
Java Sliding Window Challenge: Longest Repeating Character Replacement
More Relevant Posts
-
🚀 DSA Consistency – Day 48 Today’s problem: Concatenation of Consecutive Binary Numbers 🔹 Problem Idea For a given n, concatenate the binary representations of numbers from 1 → n and return the decimal value modulo (10^9 + 7). Example: 1 → 1 2 → 10 3 → 11 Concatenation → 11011 💡 Key Intuition Instead of converting numbers to strings, we can use bit manipulation: • When a number becomes a power of 2, its binary length increases by 1 • Left shift the current result by the number of bits needed • Append the current number using addition Formula used: res = ((res << bits) + i) % mod ⚡ Why this works Every time we encounter a power of 2, the binary representation grows by one bit. We track this using: (i & (i - 1)) == 0 which efficiently checks if i is a power of two. ⏱ Complexity • Time: O(n) • Space: O(1) Consistency compounds. 48 days of showing up and solving. 💪 #DSA #LeetCode #Java #BitManipulation #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 52 of DSA Problem Solving Today’s challenge: Prime Number of Set Bits in Binary Representation 🔢 💡 What I practiced: Used Integer.bitCount() to count set bits efficiently Applied a prime check on the bit count Strengthened concepts of bit manipulation + number theory ⚡ Realization: Built-in methods like bitCount() are not just shortcuts — they are highly optimized and interview-relevant. 🧠 Approach: 1️⃣ Iterate from left → right 2️⃣ Count set bits using Integer.bitCount(n) 3️⃣ Check if the count is prime 4️⃣ Increment answer Clean, efficient, and readable ✅ 📈 Progress update: Bit manipulation getting stronger Writing more optimized Java code Staying consistent every single day 52 days.💪 #Java #LeetCode #DSA #BitManipulation #CodingJourney
To view or add a comment, sign in
-
-
Day 44 of DSA 🚀 Solved Minimum Number of Flips to Make the Binary String Alternating. 💡 Key Insight: An alternating binary string can only follow two patterns: 010101... 101010... To handle rotations efficiently: Double the string (s + s) Use a sliding window of size n Count mismatches with both patterns Track the minimum flips required. ⏱ Time: O(n) 💾 Space: O(n) Good practice for sliding window + pattern matching. #DSA #Java #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
Day 42 of DSA 🚀 | Alternating Binary String Today’s problem: Minimum Changes To Make Alternating Binary String 🔹 Task: Convert a binary string into an alternating string (0101... or 1010...) using the minimum number of flips. 💡 Key Insight: Only two valid patterns exist: 010101... 101010... Count mismatches with one pattern and compute the other using: min(count, n - count) 📌 Example s = "0100" → Output = 1 ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) Small problem, but a great reminder that recognizing patterns can simplify the solution drastically. #DSA #Java #LeetCode #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 48: Checking the time with Bit Manipulation! ⌚ Problem 401: Binary Watch Today’s problem involved a watch that represents hours and minutes using binary LEDs. The task was to find all possible times given a specific number of "on" LEDs. Instead of overcomplicating the combinations, I went with a clean brute-force approach across all possible hours (0-11) and minutes (0-59). The MVP of today’s code was Integer.bitCount(), which effortlessly counts the number of set bits (1s) in an integer. By checking if the combined bit count of the hour and minute matched the target, I could format and add the valid times to my result list. It’s O(1) effectively, given the fixed constraints of a clock, and way more readable than manual bit shifting. #LeetCode #Java #BitManipulation #Algorithms #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
Day 32 of DSA 🚀 | Check If a String Contains All Binary Codes of Size K Today’s problem looked straightforward but tested how well I understand constraints and coverage. 🔹 Task: Check whether a binary string contains every possible binary code of length k as a substring. 🔹 Key realization: This is not about generating binary codes. It’s about verifying coverage using a sliding window. 💡 What I learned: Total binary codes of size k = 2^k Sliding window helps examine substrings efficiently HashSet tracks unique patterns automatically Correct boundary (s.length() - k) avoids runtime errors ⏱ Time Complexity: O(n × k) 📦 Space Complexity: O(2^k) 📌 Example: "00110110", k = 2 → all patterns {00, 01, 10, 11} exist → ✅ true This problem reinforced how small details in problem statements can completely change the approach. #DSA #SlidingWindow #Strings #Java #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 43 of DSA |Check if Binary String Has at Most One Segment of Ones. Given a binary string without leading zeros, we need to check whether it contains at most one contiguous segment of '1's. Example Input: "1001" → Output: false Input: "110" → Output: true 💡 Key Insight If a '0' appears after the first segment of '1's and we encounter another '1', it means a new segment started. 🧠 Approach • Traverse the string • Once '0' appears after '1', mark it • If another '1' appears afterwards → return false ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem highlights how pattern observation can simplify string traversal problems. #DSA #Java #LeetCode #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 88 of 100 Days of LeetCode Problem: Min Cost Climbing Stairs Difficulty: Easy Today’s problem was another classic Dynamic Programming pattern. Each step has a cost, and you can climb either 1 or 2 steps at a time. The goal is to reach the top with the minimum total cost. You can start from index 0 or index 1. The key idea: To reach step i, you must come from either i-1 or i-2. So the recurrence becomes: dp[i] = cost[i] + min(dp[i-1], dp[i-2]) Finally, since you can reach the top from the last or second-last step: answer = min(dp[n-1], dp[n-2]) This problem reinforces the “take minimum of previous two states” DP pattern — very similar to Climbing Stairs but focused on cost minimization instead of counting ways. Time Complexity: O(n) Space Complexity: O(n) → can be optimized to O(1) Another solid DP variation completed. On to Day 89 💪 #LeetCode #100DaysOfCode #DSA #DynamicProgramming #Java #Algorithms #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 62/365 — DSA Challenge Today's problem was simple in statement... but powerful in concept. Solved: 242. Valid Anagram The task: Given two strings s and t, return true if t is an anagram of s. An anagram means: Same characters. Same frequency. Different order allowed. 💡 My Approach Instead of sorting both strings, I used a frequency counter array of size 26. Steps: • If lengths differ -> return false • Increment count for each character in s • Decrement count for each character in t • If all values return to zero -> it's an anagram ⚡ Why this approach? Sorting -> O(n log n) Frequency count -> O(n) Time Complexity: O(n) Space Complexity: O(1) (fixed 26 letters) What I learned today Sometimes the optimal solution is just about counting correctly. Not every string problem needs sorting. Clean. Efficient. Intentional. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #LearningInPublic #Consistency #ProblemSolving
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development