🚀 Day 38 / 100 | Merge Close Characters -Intuition: -The idea is that if the same character appears within distance k, the right character merges into the left. -So we only keep characters that cannot be merged with any previous same character within range k. -Approach: O(n²) -Create a StringBuilder to store the result. -Traverse each character in the string. -For each character, check in the result string if the same character exists within distance k from the end. -If found, it merges (ignore current character). -Otherwise, append it to the result. -Final StringBuilder will be the answer after all merges. -Complexity: Time Complexity: O(n²) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #String
Java String Merging Characters
More Relevant Posts
-
🚀 LeetCode Problem || The k-th Lexicographical String of All Happy Strings of Length n A happy string is a string that: • Contains only 'a', 'b', 'c' • No two adjacent characters are the same For example: Valid → abc, ac, b Invalid → aa, baa 💡 Approach I Used: I used Backtracking to generate all possible happy strings while ensuring that the current character is not the same as the previous one. Since we try characters in the order 'a' → 'b' → 'c', the generated strings are already in lexicographical order. After generating the strings, we simply return the k-th string if it exists. #Backtracking #Recursion #Strings #LeetCode #ProblemSolving #Java
To view or add a comment, sign in
-
-
Day 45 on LeetCode Largest Submatrix With Rearrangements :- This problem clicked once I stopped treating columns as fixed. Rearranging them turns each row into a controllable histogram. Approach: ->Build vertical heights of consecutive 1s ->Sort each row to simulate optimal column order ->Evaluate max area using height × width Time Complexity: O(m × n log n) The editorial on LeetCode made the difference here. It didn’t just give the solution, it clarified the intuition behind sorting heights, which is easy to miss on your own. #DSA #LeetCode #Java
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟮𝟳𝟲𝟮. 𝗖𝗼𝗻𝘁𝗶𝗻𝘂𝗼𝘂𝘀 𝗦𝘂𝗯𝗮𝗿𝗿𝗮𝘆𝘀 | 🟡 𝗠𝗲𝗱𝗶𝘂𝗺 | 𝗝𝗮𝘃𝗮 This one is a proper sliding window + monotonic deque problem — one of the most powerful combos in DSA. 𝗧𝗵𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻: A subarray is valid if max - min ≤ 2 for all pairs. Checking this naively for every subarray is O(n²). We need smarter. 𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝘄𝗶𝗻𝗱𝗼𝘄 + 𝘁𝘄𝗼 𝗱𝗲𝗾𝘂𝗲𝘀: ✅ maxDeque → monotonic decreasing (tracks window max) ✅ minDeque → monotonic increasing (tracks window min) ✅ Expand right every iteration ✅ Shrink left when max - min > 2 ✅ Every valid window ending at right contributes (right - left + 1) subarrays 𝗪𝗵𝘆 𝘁𝘄𝗼 𝗱𝗲𝗾𝘂𝗲𝘀? They give O(1) access to the window's current max and min at all times — no rescanning needed when the window shrinks. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n) — each element enters and exits each deque at most once 📦 Space: O(n) deque space The moment this pattern clicked for me — sliding window controlling the range, deques tracking the extremes — a whole class of hard problems became approachable. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/e_Mu8-Eh 16 more days. Grinding through! 💪 #LeetCode #Day84of100 #100DaysOfCode #Java #DSA #SlidingWindow #MonotonicDeque #CodingChallenge #Programming
To view or add a comment, sign in
-
🚀 Day 81/100 – 𝐋𝐨𝐧𝐠𝐞𝐬𝐭 𝐒𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠 𝐰𝐢𝐭𝐡 𝐀𝐭 𝐋𝐞𝐚𝐬𝐭 𝐊 𝐑𝐞𝐩𝐞𝐚𝐭𝐢𝐧𝐠 𝐂𝐡𝐚𝐫𝐚𝐜𝐭𝐞𝐫𝐬 Today’s problem was a bit tricky but super interesting — Longest Substring with At Least K Repeating Characters. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: This problem can be efficiently solved using a 𝐃𝐢𝐯𝐢𝐝𝐞 & 𝐂𝐨𝐧𝐪𝐮𝐞𝐫 𝐚𝐩𝐩𝐫𝐨𝐚𝐜𝐡 instead of brute force. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Count frequency of all characters If any character appears less than k, it cannot be part of the valid substring Split the string at that character and solve recursively ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Count 𝐟𝐫𝐞𝐪𝐮𝐞𝐧𝐜𝐲 𝐨𝐟 𝐜𝐡𝐚𝐫𝐚𝐜𝐭𝐞𝐫𝐬 Traverse string: If any char frequency < k → split Recursively solve 𝐥𝐞𝐟𝐭 & 𝐫𝐢𝐠𝐡𝐭 parts Return max of both ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ~O(n log n) (depends on splits) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(n) (recursion stack) #Day81 #100DaysOfCode #Java #DSA #LeetCode #DivideAndConquer #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 45 / 100 | Longest Substring Without Repeating Characters Intuition: The problem is to find the length of the longest substring without repeating characters. If a repeated character appears, the substring must be adjusted to remove duplicates. Using a sliding window approach, it maintain a window of unique characters while scanning the string. Approach: O(n) -Use a HashSet to store characters currently in the substring window. -Maintain two pointers: left and right . -Iterate through the string using the right pointer. -If the character already exists in the set, remove characters from the left until the duplicate is removed. -Add the current character to the set. -Update the maximum length of the substring using the window size. -Repeat this process until the end of the string. Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
To view or add a comment, sign in
-
-
🚀 Day 40 / 100 | Contains Duplicate II -Intuition: -The goal is to check whether there exist two equal elements within a distance k. -Instead of checking every pair, we only check for duplicates inside a window of size k. -If a number repeats within this window, the condition |i - j| ≤ k is satisfied, then return true. -Approach: O(n) -Use a HashSet to maintain a sliding window of size k. -Traverse through the array. -If the current element already exists in the set, return true. -Add the current element to the set. -If the window size exceeds k, remove the element that goes out of range (nums[i - k]). -If traversal completes without finding duplicates, return false. -Complexity: Time Complexity: O(n) Space Complexity: O(k) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
To view or add a comment, sign in
-
-
Day 22/30 – LeetCode streak Problem: Find Unique Binary String You are given 'n' distinct binary strings of length 'n' and must return any binary string of length 'n' that doesn’t appear in the array. Core idea (Cantor’s diagonal argument) * Think of 'nums' as an 'n × n' matrix of characters. * Look at the diagonal positions: character 'i' of string 'nums[i]'. * Build a new string where the 'i'-th character is the flip of 'nums[i].charAt(i)' ('0' → '1', '1' → '0'). * This constructed string differs from 'nums[0]' at index '0', from 'nums[1]' at index '1', …, from 'nums[i]' at index 'i', so it cannot be equal to any string in 'nums'. * Time complexity: 'O(n)', space: 'O(n)' for the output string. Day 22 takeaway: This problem is a neat, direct application of Cantor’s diagonalization: by flipping the diagonal bits, you guarantee a new string that is different from every given one in at least one position, without any searching or backtracking. #leetcode #dsa #java #strings #consistency
To view or add a comment, sign in
-
-
LeetCode Problem || Minimum Changes To Make Alternating Binary String (1758)🚀 Binary String. 🔹 Problem Summary: Given a binary string s containing only 0 and 1, we need to find the minimum number of changes required to make the string alternating (no two adjacent characters should be the same). Example of valid alternating strings: 0101, 1010 🔹 Key Idea: An alternating string can only have two possible patterns: 1️⃣ Starting with 0 → 010101... 2️⃣ Starting with 1 → 101010... So the approach is: Count mismatches if the string follows pattern 0101... Count mismatches if the string follows pattern 1010... The minimum of the two counts is the answer Time Complexity:O(n) Space Complexity:O(1) #LeetCode #DSA #Java #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
💡 Day 37 of LeetCode Problem Solved! 🔧 🌟75. Sort Colors🌟 Task : • Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. • We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. • You must solve this problem without using the library's sort function. Example 1: Input: nums = [2,0,2,1,1,0] Output: [0,0,1,1,2,2] Example 2: Input: nums = [2,0,1] Output: [0,1,2] #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
100 Days of Code Day-14 Solved a classic problem: Longest Common Prefix Given an array of strings, the task is to find the longest common prefix shared among them. If no common prefix exists, return an empty string. Approach: Used a simple horizontal scanning method: Start with the first string as the prefix Compare it with each string Gradually reduce the prefix until it matches all strings Example: "flower", "flow", "flight" → "fl" "dog", "racecar", "car" → "" A good exercise to strengthen string handling and logical thinking. #Java #DSA #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