𝗗𝗮𝘆 𝟱𝟵/𝟭𝟬𝟬 — 𝗢𝗻𝗲 𝗗𝗮𝘆 𝗧𝗼 𝟲𝟬 Day 59. One day away from another milestone. But today? Simple string manipulation. Two pointers. Classic reversal. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟮𝟬𝟬𝟬: Reverse Prefix of Word (Easy) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a word and a character, reverse the prefix up to the first occurrence of that character. Example: word = "abcdefd", ch = 'd' Result = "dcbaefd" (Reverse "abcd" → "dcba", keep "efd") 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Find the index. Two-pointer swap. Done. 👉 Find first occurrence of character 👉 If not found, return original word 👉 Two pointers: left = 0, right = index 👉 Swap characters while moving inward 👉 Return modified string Time: O(n), Space: O(n) for char array 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: Two-pointer reversal. Day 31 used it for linked lists. Day 59 uses it for strings. Same pattern. Different data structure. That's the power of understanding fundamentals—they transfer. 𝗖𝗼𝗱𝗲: https://lnkd.in/g7UXBf_u 59 down. 41 to go. Tomorrow = 60. 𝗗𝗮𝘆 𝟱𝟵/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Strings #TwoPointer #Algorithms #ProblemSolving #CodingInterview #Programming #Java #Fundamentals #Day60Tomorrow
More Relevant Posts
-
🔥 Day 349 – Daily DSA Challenge! 🔥 Problem: 🧱 Pyramid Transition Matrix You are stacking blocks to form a pyramid. Each block is represented by a letter. Given a bottom row and a list of allowed triples "ABC" meaning: A B → C Return true if you can build the pyramid to the top. 💡 Key Insight — Bitmask + DFS Instead of storing allowed transitions as lists, we encode them using bitmasks. For each pair (A, B) we store possible top blocks using a bitmask. Conceptually: Example: ABC ABD mask[A][B] = {C, D} stored as bits. 🧠 Recursive Construction We build the pyramid row by row: 1️⃣ Current row → cur 2️⃣ Generate next row → next 3️⃣ For each adjacent pair (cur[i], cur[i+1]) check all possible blocks above 4️⃣ Recursively continue until: length = 1 → pyramid complete ⚡ Optimization Trick To extract possible blocks from bitmask: bit = m & -m This isolates the lowest set bit, letting us iterate through candidates efficiently. ⚙️ Complexity Let n be bottom length. ✅ Time Complexity: ~ O(7ⁿ) worst-case (pruned heavily) ✅ Space Complexity: O(n) recursion stack But pruning via allowed transitions keeps it practical. 💬 Challenge for you 1️⃣ Why does using bitmasks make transitions faster than lists? 2️⃣ How would you add memoization to avoid recomputing rows? 3️⃣ Can you solve this using DP with states instead of DFS? #DSA #Day349 #LeetCode #DFS #Bitmask #Backtracking #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🔥 Day 350 – Daily DSA Challenge! 🔥 Problem: 🔤 Sort Characters By Frequency Given a string s, sort it in descending order based on character frequency. 💡 Key Insight — Frequency + Sorting We solve this in two steps: 1️⃣ Count frequency of each character 2️⃣ Sort characters based on frequency (descending) 🧠 Frequency Concept Each character contributes: Then we arrange characters in decreasing order of this frequency. ⚡ Algorithm Steps ✅ Count frequencies using HashMap ✅ Store unique characters in a list ✅ Sort list based on frequency (descending) ✅ Build result string by repeating characters ⚙️ Complexity ✅ Time Complexity: O(n log k) (k = unique characters) ✅ Space Complexity: O(n) 🚀 Optimization Idea Instead of sorting, we can use bucket sort (since max frequency ≤ n) → O(n) 💬 Challenge for you 1️⃣ Can you solve this using bucket sort in O(n)? 2️⃣ What if input contains Unicode characters? 3️⃣ How would you return characters sorted by increasing frequency? #DSA #Day350 #LeetCode #HashMap #Sorting #Strings #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 45 of #100DaysOfCode Solved 209. Minimum Size Subarray Sum on LeetCode 📊 🧠 Key Insight: We need to find the smallest length subarray whose sum is ≥ target. A brute-force approach would be O(n²), but this can be optimized using the Sliding Window (Two Pointers) technique. ⚙️ Approach: 1️⃣ Initialize two pointers: left = 0 and iterate right 2️⃣ Keep adding elements to curr_sum 3️⃣ Once curr_sum ≥ target, try to shrink the window: 🔹Update minimum length 🔹Remove nums[left] and move left forward 4️⃣ Repeat until the entire array is processed This ensures we always maintain the smallest valid window. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #SlidingWindow #Arrays #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
#100DaysOfCode 🚀 👉Solved: LeetCode 75 – Sort Colors The goal was to sort an array containing only three values: 0 (Red), 1 (White), and 2 (Blue) At first glance this looks like a simple sorting problem. But the twist: ❌ No built-in sort ✔ One pass ✔ Constant space The solution uses the famous **Dutch National Flag Algorithm**. Instead of sorting normally, we maintain three pointers: • low → for 0s • mid → current element • high → for 2s Rules: • If nums[mid] == 0 → swap with low • If nums[mid] == 1 → move mid forward • If nums[mid] == 2 → swap with high This allows sorting the array in a single pass. 📌 Key Points: ✔ In-place sorting ✔ One pass algorithm ✔ Constant space usage ✔ Classic three-pointer technique ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Problems like this show how powerful pointer techniques can be. Day 15✅ #DSA #Java #LeetCode #Algorithms #ProblemSolving #100DaysOfCode #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 42 of #100DaysOfCode Solved 1508. Range Sum of Sorted Subarray Sums on LeetCode 📊 🧠 Problem Insight: Given an array, we must compute the sum of all subarray sums, sort them, and return the sum of elements between indices left and right. ⚙️ Approach Used: 1️⃣ Generate all possible subarray sums using nested loops 2️⃣ Store them in an array of size n*(n+1)/2 3️⃣ Sort the subarray sums 4️⃣ Compute the sum of elements from index left-1 to right-1 5️⃣ Apply mod = 1e9 + 7 to avoid overflow This approach works well because the total number of subarrays is manageable for the given constraints. ⏱️ Time Complexity: O(n² log n) O(n²) to generate subarray sums O(n² log n) to sort them 📦 Space Complexity: O(n²) #100DaysOfCode #LeetCode #DSA #Arrays #Algorithms #Java #CodingPractice #InterviewPrep
To view or add a comment, sign in
-
-
Day 82/100 – LeetCode Challenge ✅ Problem: #43 Multiply Strings Difficulty: Medium Language: Java Approach: Manual Multiplication with Result Array Time Complexity: O(n × m) Space Complexity: O(n + m) Key Insight: Multiply digits from right to left (least significant first). Store intermediate results in array where index i + j + 1 holds current digit. Handle carry by adding to previous index. Solution Brief: Edge case: if either number is "0", return "0". Created result array of size n1 + n2 (max possible digits). Nested loops multiply each digit of num1 with each digit of num2. Accumulated results with proper carry handling. Built final string skipping leading zeros. #LeetCode #Day82 #100DaysOfCode #Math #String #Java #Algorithm #CodingChallenge #ProblemSolving #MultiplyStrings #MediumProblem #Multiplication #Array #DSA
To view or add a comment, sign in
-
-
Day 38 of Daily DSA 🚀 Solved LeetCode 1897: Redistribute Characters to Make All Strings Equal ✅ Problem: Given an array of strings, determine if you can make every string equal by moving any character from one string to any position in another string. Rules: * You can pick two distinct indices i and j and move any character from words[i] to any position in words[j] * Any number of operations are allowed * Return true if all strings can be made equal, false otherwise Approach: Used a HashMap to count the total frequency of every character across all strings. If every character's count is divisible by the number of strings, equal redistribution is possible. Steps: 1. Concatenate all strings into one using StringBuilder 2. Count the frequency of each character using a HashMap 3. For every character count, check if it is divisible by the number of words 4. If any count is not divisible → return false 5. Otherwise → return true ⏱ Complexity: • Time: O(n * m) — n = number of words, m = average word length • Space: O(1) — at most 26 characters in the map 📊 LeetCode Stats: • Runtime: 12 ms (Beats 17.93%) • Memory: 46.63 MB A brilliant insight — redistribution is just a divisibility check! No complex simulation needed. #DSA #LeetCode #Java #HashMap #Strings #CodingJourney #ProblemSolving
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
-
-
🔥 Day 352 – Daily DSA Challenge! 🔥 Problem: 📦 Subarray Product Less Than K Given an array nums and an integer k, return the number of contiguous subarrays where the product of all elements is strictly less than k. 💡 Key Insight — Sliding Window (Two Pointers) Brute force would check all subarrays → O(n²) ❌ Instead, we use a sliding window to maintain a valid product. Core idea: 🧠 How It Works 🔹 Expand the window by moving right 🔹 Multiply current element into prod 🔹 If product ≥ k → shrink window from left 🔹 Once valid, all subarrays ending at right are valid 👉 Count added: right - left + 1 ⚡ Optimized Plan ✅ Initialize: left = 0, prod = 1, count = 0 ✅ For each right: Multiply nums[right] While product ≥ k: divide by nums[left] move left Add (right - left + 1) to count ⚙️ Complexity ✅ Time Complexity: O(n) (each element enters and exits window once) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why does this approach fail if negative numbers are allowed? 2️⃣ How would you modify this for sum < k instead of product? 3️⃣ Can you solve this using log transformation + prefix sum? #DSA #Day352 #LeetCode #SlidingWindow #TwoPointers #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
Day 33 of Daily DSA 🚀 Solved LeetCode 58: Length of Last Word ✅ Problem: Given a string s, return the length of the last word (a sequence of non-space characters). Approach: First, trim() the string to remove trailing spaces Traverse the string from the end Count characters until a space is encountered 💡 Key Insight: Instead of splitting the string (which uses extra space), we can simply iterate backwards for an efficient solution. ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.17 MB A simple yet important problem to strengthen string traversal techniques. #DSA #LeetCode #Java #Strings #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
Explore related topics
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