Day 18 of my #30DayCodeChallenge: The Art of String Manipulation! The Problem: Count and Say. A classic string problem where each term is generated by describing the previous term using Run-Length Encoding (RLE). The Logic: The key here is to simulate the process of "reading out loud" what you see. Using a StringBuilder and a two-pointer approach, we can efficiently build the next string in the sequence: 1. Iterative Progression: Starting from the base case "1", we loop n - 1 times to reach the n" term. 2. The Two-Pointer Scan: For each string, I used an inner whi le loop to find groups of identical consecutive characters. 3. Count and Append: * Count: Calculate the length of the run (number of times a digit repeats). *Say: Append the count followed by the digit itself to our StringBuilder. 4. Update & Repeat: The newly built string becomes the input for the next iteration. This problem is a great exercise in managing indices and understanding how strings are constructed dynamically in Java. One step closer to mastery. Onward to Day 19! #Java #Algorithms #DataStructures #LeetCode #ProblemSolving #150DaysOfCode #SoftwareEngineering #CodingLife #Strings
Java String Manipulation Challenge: Count and Say Problem
More Relevant Posts
-
Problem Solved: Super Reduced String (Stack Approach) Today I solved an interesting string problem where we repeatedly remove adjacent matching characters until no more reductions are possible. 💡 Key Idea: Used a StringBuilder as a stack to efficiently remove adjacent duplicates in a single pass. 🔧 Tech Used: Java | String Manipulation | Stack Concept 📌 What I Learned: How stack-based thinking simplifies string problems Writing optimized O(n) solutions Clean handling of edge cases like empty string 📊 Example: Input: aaabccddd Output: abd This problem is a great example of how simple logic + the right data structure can lead to efficient solutions. #Java #DataStructures #CodingPractice #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
🚀 LeetCode — Problem 242 | Day 14 💡 Problem: Valid Anagram --- 🧠 Problem: Given two strings s and t, return true if t is an anagram of s, otherwise false. --- 🧠 Approach: - First check length: • If lengths differ → not an anagram - Use a frequency array of size 26 - Traverse both strings: • Increment count for s • Decrement count for t - Finally check: • If all values are 0 → valid anagram --- ⚙️ Core Logic: - freq[s.charAt(i) - 'a']++ - freq[t.charAt(i) - 'a']-- 👉 If all counts become zero → both strings have same characters --- ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) (fixed size array) --- ⚠️ Edge Cases: - Different lengths → false - Same characters, different order → true - Completely different strings → false --- 🔍 Insight: Instead of sorting, count character frequency --- 🔑 Key Learning: - Frequency counting is faster than sorting - Fixed-size array gives O(1) space - Simple and optimal approach for character problems --- "Compare frequency of characters using a fixed array instead of sorting." --- #LeetCode #DSA #Java #Strings #CodingJourney
To view or add a comment, sign in
-
-
The Two-Pointer streak continues! Today’s LeetCode Problem: Is Subsequence. After using multiple pointers to sort arrays and move zeroes, I applied the exact same pattern to string manipulation today. The challenge was figuring out if a shorter string (s) is a valid subsequence of a longer string (t) without disturbing the relative order of the characters. Instead of generating all possible subsequences (which would be a massive O(2ⁿ) performance drain), the Two-Pointer approach solves this beautifully in a single pass. Knocking this out in O(n) time and O(1) space is another great reminder of how incredibly versatile this algorithmic pattern is for both arrays and strings. #DSA #Java #LeetCode #IsSebsequenceProblem
To view or add a comment, sign in
-
-
🚀 Day 31 of #LeetCode Challenge 🔍 Problem: Decode the Slanted Ciphertext Today’s problem was all about understanding patterns and matrix traversal in a clever way! 💡 Key Idea: * The encoded string is formed by writing characters in a matrix row-wise * The trick is to read diagonally (↘️ direction) to decode the original message * No need to build a 2D matrix — we can directly calculate indices! 🧠 What I Learned: * How to convert 1D string into virtual 2D matrix * Diagonal traversal techniques * Importance of trimming trailing spaces in string problems ⚡️ Approach: * Calculate number of columns → cols = n / rows * Traverse diagonally from each column * Append characters using index formula i * cols + j * Remove extra spaces at the end 💻 Language: Java 🎯 Time Complexity: O(n) 📦 Space Complexity: O(n) Consistency is the key 🔑 — one problem at a time, getting better every day! #Day31 #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟵𝟯/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟲𝟬𝟴. 𝗦𝗽𝗲𝗰𝗶𝗮𝗹 𝗔𝗿𝗿𝗮𝘆 𝗪𝗶𝘁𝗵 𝗫 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗼𝗿 𝗘𝗾𝘂𝗮𝗹 𝗫 | 🟢 Easy | Java A self-referential condition — x elements must be ≥ x. Elegant problem, elegant solution. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Find x such that exactly x elements in the array are ≥ x. Return -1 if no such x exists. ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗙𝗿𝗲𝗾𝘂𝗲𝗻𝗰𝘆 𝗖𝗼𝘂𝗻𝘁 + 𝗦𝘂𝗳𝗳𝗶𝘅 𝗦𝘂𝗺 ✅ Cap all values at n (array length) — anything larger contributes the same way ✅ Build a frequency count array of size n+1 ✅ Traverse from right to left, accumulating a running suffix sum ✅ When suffix sum == current index i → x = i is the answer! 💡 𝗪𝗵𝘆 𝗰𝗮𝗽 𝗮𝘁 𝗻? x can never exceed n (can't have more elements than the array size). So values above n are equivalent — capping them avoids index overflow and keeps the logic clean. 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(n) — two passes 📦 Space: O(n) — frequency array No sorting. No binary search. Just a clever frequency count + suffix accumulation. Sometimes the cleanest approach is right under your nose. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gm2c4-6x 𝟳 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗦𝗼 𝗰𝗹𝗼𝘀𝗲 𝘁𝗼 𝟭𝟬𝟬! 💪 #LeetCode #Day93of100 #100DaysOfCode #Java #DSA #Arrays #FrequencyCount #CodingChallenge #Programming
To view or add a comment, sign in
-
Day 14 of LeetCode — Longest Common Prefix Today I solved the classic problem: finding the longest common prefix among a list of strings. Approach I used: Start with the first string as the prefix Compare it with each string in the array Shrink the prefix until it matches the start of every string If it becomes empty → no common prefix Time Complexity: O(n * m) (n = number of strings, m = length of prefix) This problem reinforced string manipulation and edge case handling (empty arrays, no matches). #Java #DSA #CodingJourney #LeetCode #ProblemSolving #TechGrowth
To view or add a comment, sign in
-
-
📅 Date: April 27, 2026 Day 6 of my LeetCode Journey 🚀 ✅ Problem Solved: 14. Longest Common Prefix 🧠 Approach & Smart Solution: To solve this problem, I used a highly efficient Horizontal Scanning technique. Instead of comparing characters index by index across all strings, I assumed the very first string was the common prefix. Then, I iteratively compared it with the next strings, shaving off characters from the end until a match was found. By leveraging Java's built-in startsWith() method, the logic is kept clean and readable! • Pseudo-code: Assume the first string in the array is the initial 'prefix'. Loop through the rest of the strings in the array: While the current string does not start with the 'prefix': Shorten the 'prefix' by removing its last character. If the 'prefix' becomes completely empty, return "" (no common prefix exists). Return the final 'prefix' after checking all strings. This step-by-step reduction ensures we only do as much work as absolutely necessary. ⏱️ Time Complexity: O(S) (where S is the sum of all characters in all strings) 📦 Space Complexity: O(1) (Only modifying the prefix string, no extra arrays used) 📊 Progress Update: • Streak: 5 Days 🔥 • Difficulty: Easy • Pattern: String / Horizontal Scanning 🔗 LeetCode Profile: https://lnkd.in/gBcDQwtb (@Hari312004) Smartly utilizing built-in string methods like startsWith and substring can drastically reduce code complexity! 💡 #LeetCode #DSA #Strings #Java #CodingJourney #ProblemSolving #InterviewPrep #Consistency #BackendDevelopment
To view or add a comment, sign in
-
-
Day 35 of Consistency 💻🔥 Solved Longest Substring Without Repeating Characters — a classic sliding window problem that really tests optimization skills. ✨ Key Takeaways: Mastered the sliding window technique Used HashMap for efficient lookup Improved understanding of two-pointer approach ⚡ Performance: Runtime: 5 ms Beat: 87%+ submissions Every day is about getting sharper, not just solving problems. #LeetCode #DataStructures #Algorithms #CodingJourney #Consistency #Java #ProblemSolving
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟵/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟰𝟰. 𝗙𝗶𝗻𝗱 𝗦𝗺𝗮𝗹𝗹𝗲𝘀𝘁 𝗟𝗲𝘁𝘁𝗲𝗿 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗧𝗮𝗿𝗴𝗲𝘁 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks easy — but the circular wrap-around is the trap most people miss. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Given a sorted circular array of letters, find the smallest letter strictly greater than the target. If none exists, wrap around to the first letter. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵: ✅ Standard binary search for the first letter > target ✅ If letters[mid] > target → go left (end = mid - 1) ✅ Else → go right (start = mid + 1) ✅ After the loop, start points to the answer ✅ Use start % letters.length to handle the circular wrap-around 𝗧𝗵𝗲 𝗰𝗹𝗲𝘃𝗲𝗿 𝗯𝗶𝘁: If target is greater than or equal to all letters, start lands at letters.length — modulo wraps it back to index 0 automatically. No special case needed. 🔄 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(log n) 📦 Space: O(1) One modulo operation handles the entire circular edge case cleanly. This is binary search with a twist — and the wrap-around trick is worth remembering! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gvK_p44b 11 more days. So close! 💪 #LeetCode #Day89of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
🚀 Day 78 of #100DaysOfCode Solved 443. String Compression on LeetCode 🔗 🧠 Key Insight: We compress the array in-place by: 👉 Replacing consecutive characters with: char + count (if count > 1) Example: ["a","a","b","b","c","c","c"] → ["a","2","b","2","c","3"] ⚙️ Approach (Two Pointers): 1️⃣ Use two pointers: 🔹 i → iterate through array 🔹 idx → position to write compressed result 2️⃣ For each character: 🔹 Count consecutive occurrences using a loop 3️⃣ Write character: 🔹 chars[idx++] = ch 4️⃣ If count > 1: 🔹 Convert count to string 🔹 Add each digit to array 5️⃣ Continue until end 6️⃣ Return idx (new length) ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Strings #TwoPointers #Array #Java #InterviewPrep #CodingJourney
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