Day 75/100: LeetCode Challenge - Count and Say 🔢 Today I tackled the "Count and Say" sequence problem - a fascinating exercise in string manipulation and pattern recognition! Problem: The count-and-say sequence starts with "1". Each subsequent term is generated by "saying" the previous term - counting consecutive digits and saying the count followed by the digit. Example: 1 → "one 1" → "11" 11 → "two 1s" → "21" 21 → "one 2, one 1" → "1211" My approach: Started with base case "1" Iteratively built each next term by traversing the current string Counted consecutive identical characters and appended count + digit to StringBuilder Used StringBuilder for efficient string concatenation Results: ✅ Runtime: 3 ms (beats 72.74%) ✅ Memory: 42.90 MB (beats 84.80%) Key takeaway: Sometimes the most straightforward iterative approach is the best! This problem teaches the importance of careful string traversal and StringBuilder usage for optimal performance. The key was recognizing the pattern: each new string is just a "run-length encoding" of the previous one. #100DaysOfCode #LeetCode #Java #StringManipulation #CodingChallenge #ProblemSolving #Day74 #Programming
Count and Say Sequence Java Solution
More Relevant Posts
-
🔥 𝗗𝗮𝘆 𝟴𝟯/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟯𝟱𝟭. 𝗖𝗼𝘂𝗻𝘁 𝗡𝗲𝗴𝗮𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿𝘀 𝗶𝗻 𝗮 𝗦𝗼𝗿𝘁𝗲𝗱 𝗠𝗮𝘁𝗿𝗶𝘅 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks simple — but the optimal solution is a beautiful staircase traversal that most people miss. 𝙏𝙝𝙚 𝙜𝙧𝙞𝙙 𝙥𝙧𝙤𝙥𝙚𝙧𝙩𝙮: Rows and columns are both sorted in non-increasing order. That means once you hit a negative, everything below it in the same column is also negative. 𝙎𝙩𝙖𝙞𝙧𝙘𝙖𝙨𝙚 𝙖𝙥𝙥𝙧𝙤𝙖𝙘𝙝 (𝙩𝙤𝙥-𝙧𝙞𝙜𝙝𝙩 𝙘𝙤𝙧𝙣𝙚𝙧): ✅ Start at row 0, last column ✅ If grid[row][col] < 0 → all elements below are negative too → add (rows - row) to count, move left ✅ If grid[row][col] >= 0 → move down 𝘾𝙤𝙢𝙥𝙡𝙚𝙭𝙞𝙩𝙮: ⏱ Time: O(m + n) — at most m+n steps 📦 Space: O(1) The naive brute force is O(m×n). This approach uses the sorted structure to skip entire column segments in one shot. That's the difference between knowing a pattern and just looping. This staircase trick works on any sorted matrix — keep it in your toolkit! 🧠 📂 𝑭𝒖𝒍𝒍 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒐𝒏 𝑮𝒊𝒕𝑯𝒖𝒃: https://lnkd.in/g_Edk5d3 17 more days. Staying consistent! 💪 #LeetCode #Day83of100 #100DaysOfCode #Java #DSA #Matrix #TwoPointers #CodingChallenge #Programming
To view or add a comment, sign in
-
🔥 𝗗𝗮𝘆 𝟴𝟬/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟱𝟳𝟮. 𝗠𝗮𝘁𝗿𝗶𝘅 𝗗𝗶𝗮𝗴𝗼𝗻𝗮𝗹 𝗦𝘂𝗺 | 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Given an n×n matrix, return the sum of both diagonals without double-counting the centre on odd-sized grids. 𝗠𝘆 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵: ✅ Single loop — O(n) time, O(1) space ✅ Add both mat[i][i] and mat[n-i-1][i] per iteration ✅ Subtract mat[n/2][n/2] once if n is odd No extra arrays. No second pass. Just clean linear logic. The beauty of this one is that both diagonals can be walked in the same loop — and the edge case resolves with a single parity check. Sometimes the simplest solution is the best solution. 💡 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gcWbQbbA 20 more days to go. Let's finish strong! 💪 #LeetCode #Day80of100 #100DaysOfCode #Java #DSA #CodingChallenge #ProblemSolving #Programming
To view or add a comment, sign in
-
Day 11/100 Day Coding Challenge Today, I solved the LeetCode problem: Container With Most Water using Java. Here’s a detailed breakdown of my approach and learning: Problem Statement: Given an array of non-negative integers representing heights of vertical lines on a coordinate plane, find two lines that, along with the x-axis, form a container that holds the maximum water. Challenges: A brute-force approach would check all possible pairs of lines (O(n²)), which is inefficient for large arrays. I aimed for an optimized solution using the two-pointer technique. Approach (Two-Pointer Technique): Initialize two pointers: l at the start, r at the end of the array. Compute the current area: curWater = (r - l) * min(height[l], height[r]). Update maximum area found so far. Move the pointer pointing to the shorter line inward: If height[l] < height[r], increment l. Else, decrement r. Repeat until the pointers meet. #100DaysOfCode #Java #LeetCode #TwoPointerTechnique #ProblemSolving #Algorithms #SoftwareEngineering #Day11
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟏/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐅𝐢𝐧𝐝 𝐀𝐥𝐥 𝐏𝐨𝐬𝐬𝐢𝐛𝐥𝐞 𝐒𝐭𝐚𝐛𝐥𝐞 𝐁𝐢𝐧𝐚𝐫𝐲 𝐀𝐫𝐫𝐚𝐲𝐬 𝐈𝐈 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Use Dynamic Programming to track valid arrays using i zeros and j ones while remembering the last placed element. To enforce the limit constraint, subtract invalid configurations that would exceed the allowed consecutive count. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Dynamic Programming with constraint handling using prefix subtraction. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐳𝐞𝐫𝐨 × 𝐨𝐧𝐞) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐳𝐞𝐫𝐨 × 𝐨𝐧𝐞) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: When counting valid sequences under constraints, DP combined with invalid-state subtraction efficiently enforces limits. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #DynamicProgramming #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟭/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟴. 𝗦𝘂𝗯𝘀𝗲𝘁𝘀 | 𝗠𝗲𝗱𝗶𝘂𝗺 | 𝗝𝗮𝘃𝗮 Given an integer array, return all possible subsets (the power set) — no duplicates allowed. 𝗠𝘆 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗮𝗰𝗸𝘁𝗿𝗮𝗰𝗸𝗶𝗻𝗴: ✅ At each index, make a choice: include or exclude ✅ Recurse with the element added → then backtrack (remove it) ✅ Recurse again without the element ✅ Base case: when index == nums.length, save the current subset This explores every branch of the decision tree, giving us all 2ⁿ subsets cleanly. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n × 2ⁿ) — 2ⁿ subsets, each copied in O(n) 📦 Space: O(n) recursion depth Backtracking is one of those patterns that feels magical once it clicks — the "add → recurse → remove" rhythm is the heart of so many classic problems. 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gZmezt_g 19 more days to go. Almost there! 💪 #LeetCode #Day81of100 #100DaysOfCode #Java #DSA #Backtracking #Recursion #CodingChallenge #Programming
To view or add a comment, sign in
-
Hi there 👋 Today I want to give you a small glimpse into the future of C++ and honestly, it’s pretty exciting. With static reflection in C++26, we can finally inspect our types and structs at compile time and do things that used to feel like magic in languages like Java or C#. But here’s the real twist: ⚡ No runtime reflection overhead ⚡ Everything happens at compile time ⚡ Zero runtime penalty Yes, compilation might take a little longer… but let’s be honest if you’re already using heavy templates, you’re probably used to that 😅 The power you get in return is incredible. As a small example, I wrote a tiny function that can print almost any struct automatically using our new friend std::meta. Of course, it’s a quick prototype and there are still edge cases I didn’t wrap in it but the idea is clear: 👉 We can inspect types 👉 Iterate over members at compile time 👉 Generate code based on structure And printing structs is just the beginning. If you watched my Python bindings talk at CppCon this year, you might already imagine where this is going… Reflection + C++ bindings = extremely powerful tooling. Think about: automatic bindings serialization schema generation logging / debugging tools meta-driven frameworks The number of possibilities here is huge. Personally, I can’t wait to start using this in production. C++ keeps evolving, and features like this show that the language is still pushing forward in powerful ways. ⚙️ What would YOU build first with C++ static reflection? #cpp #cpp26 #programming #softwareengineering #reflection
To view or add a comment, sign in
-
-
LeetCode Problem || Find All Possible Stable Binary Arrays II (3130)🚀 🔹 Problem Idea: We are given a number of 0s and 1s and a constraint called limit. The goal is to count the number of possible binary arrays such that no more than limit consecutive identical elements appear. 🔹 Approach: I used Dynamic Programming to track the number of valid arrays formed with: i zeros j ones and the last placed element (0 or 1). The DP state helps ensure that the consecutive limit condition is maintained while building the array. Always enjoying the process of learning and improving problem-solving skills! 💡 #LeetCode #DynamicProgramming #ProblemSolving #Java #CodingPractice #SoftwareEngineering
To view or add a comment, sign in
-
-
After reading and understanding the question and the test cases for LeetCode 1689 – Partitioning Into Minimum Number of Deci-Binary Numbers, my first thought was to build the sum using deci-binary numbers (0s and 1s) and then count how many numbers are required. That approach felt natural because Example 1 itself explains the solution like this: Input: n = "32" Output: 3 Explanation: 10 + 11 + 11 = 32 So my thinking was to follow the same idea — construct the sum first and then count the numbers. But when I looked at Example 3: Input: n = "27346209830709182346" Output: 9 and noticed the constraint: 1 <= n.length <= 10^5 it became clear that converting the string into numeric types or constructing the sum directly would not scale given the constraints. After carefully going through the hints and the solution, I understood the key insight: Each deci-binary number can contribute only 0 or 1 per digit. So if any digit in the string is 9, we need at least 9 deci-binary numbers to form that digit. Since all digits must be satisfied together, the maximum digit in the string decides the minimum number of deci-binary numbers required. This problem was a good reminder that sometimes the challenge isn’t coding — it’s thinking about constraints the right way. #LeetCode #ProblemSolving #DSA #Learning #Java
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟲/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟮𝟰. 𝗙𝗶𝗻𝗱 𝗣𝗶𝘃𝗼𝘁 𝗜𝗻𝗱𝗲𝘅 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Deceptively simple — and a perfect introduction to the prefix sum pattern. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Find the index where the sum of all elements to the left equals the sum of all elements to the right. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗣𝗿𝗲𝗳𝗶𝘅 𝗦𝘂𝗺: ✅ Compute the total sum in one pass ✅ Track leftTotal as we iterate ✅ rightTotal = total - leftTotal - nums[i] ✅ If leftTotal == rightTotal → pivot found! No extra arrays. No nested loops. Just one pre-computation and one clean scan. 𝗧𝗵𝗲 𝗸𝗲𝘆 𝗶𝗻𝘀𝗶𝗴𝗵𝘁: Instead of recalculating both sides at every index, derive the right sum from what you already know — total and left. That drops it from O(n²) to O(n) instantly. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n) — two passes 📦 Space: O(1) This pattern — precompute total, derive the other side on the fly — shows up everywhere: product arrays, equilibrium points, range queries. A must-know! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/g_E5Ahfe 14 more days. The finish line is in sight! 💪 #LeetCode #Day86of100 #100DaysOfCode #Java #DSA #PrefixSum #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
Day 55 of #100DaysOfLeetCode 💻✅ Solved #434. Number of Segments in a String problem in Java. Approach: • Initialized a counter to track number of words (segments) • Traversed the string character by character • Checked for non-space characters • If the current character is not a space and either it is the first character or the previous character is a space, incremented the count • This ensures counting only the starting of each word Performance: ✓ Runtime: 0 ms (Beats 100% submissions) 🚀 ✓ Memory: 41.83 MB (Beats 99.81% submissions) Key Learning: ✓ Practiced string traversal without using extra space ✓ Learned how to identify word boundaries efficiently ✓ Improved logic building for handling spaces and edge cases Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #ProblemSolving #CodingJourney #100DaysOfCode
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