Continuing with the Strings module. The focus shifted from basic string handling to actually solving problems using string logic. This involved counting specific characters, working with substrings, and understanding how nested loops apply to text just like they do with arrays. String s = "abcd"; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j <= s.length(); j++) { System.out.print(s.substring(i, j)); } System.out.println(); } Output: a ab abc abcd b bc bcd c cd d What became clear : - String problems often require nested loop thinking, similar to matrix traversal - Substrings help break a large problem into smaller continuous parts - Time complexity starts to matter once we generate all possible substrings - Logic matters more than syntax in problem-solving questions This felt like the point where Strings stopped being just text handling and started becoming real DSA practice. Continuing deeper into Strings. #Java #DSA #Strings #LearningInPublic #ProblemSolving #CodingJourney #Programming #JavaDeveloper #DeveloperJourney
Java String Problems Require Nested Loop Logic
More Relevant Posts
-
Continuing further with the Strings module today. This part focused on how strings interact with numbers and type conversion, and how built-in functionality can simplify problems that would normally require loops or extra logic. Understanding string concatenation behavior and converting integers to strings showed how flexible strings are in computation as well. int n = 1256; String s = Integer.toString(n); System.out.println(s.length()); // 4 What became clear : - Strings can be used as a tool for computation, not just for storing text - Converting numbers to strings can simplify certain logical problems - Some problems that look loop-based can be solved using built-in conversions - Thinking in terms of data representation becomes important in DSA This felt like a small but meaningful shift toward smarter problem solving instead of longer code. Still progressing through the Strings module. #Java #DSA #Strings #LearningInPublic #ProblemSolving #CodingJourney #Programming #JavaDeveloper #DeveloperJourney
To view or add a comment, sign in
-
Day 64/100: The "Special Binary String" Puzzle 🧩 Today's challenge was a fascinating problem that combines recursion with sorting logic - "Make Largest Special Binary String". At first glance, this problem seems deceptively simple. We're given a special binary string (defined recursively as a string that can be composed of smaller special strings concatenated together), and we need to rearrange it to form the lexicographically largest possible special string. The key insight? Special binary strings are essentially valid parentheses where '1' represents '(' and '0' represents ')'. This transforms the problem into finding valid parenthetical groupings and recursively optimizing them! My Approach: Track the balance of 1s and 0s while traversing the string Whenever the balance returns to zero, we've found a valid "special" substring Recursively process each inner substring Sort all special substrings in descending order and concatenate Wrap each processed result with '1' and '0' The trickiest part was understanding that each special substring needs to be transformed independently before sorting them at the current level. This ensures we're always working with the "largest" version at every nesting level. Runtime: 4ms (beats 15.40%) Memory: 43.13 MB (beats 79.62%) Learning to see binary strings as parentheses opened up a whole new perspective on this problem! Sometimes the hardest part isn't writing the code, but recognizing the pattern hiding in plain sight. 🔍 #100DaysOfCode #LeetCode #Java #CodingChallenge #Algorithms #ProblemSolving #Recursion #BinaryStrings #Programming #DeveloperJourney #CodeNewbie #TechCommunity #StringManipulation #DataStructures #Day64
To view or add a comment, sign in
-
-
Wrapping up the core concepts of the Strings module today. The final focus was on understanding what actually happens inside memory when strings are created and compared. Concepts like immutability, string interning, and the difference between == and .equals() made it clear that strings are not just simple text values, but structured objects with specific behavior. String a = "hello"; String b = "hello"; String c = new String("hello"); System.out.println(a == b); // true System.out.println(a == c); // false System.out.println(a.equals(c)); // true What became clear : - Strings in Java are immutable, meaning their value cannot be changed after creation - The string pool allows memory reuse when identical literals are created - "==" compares references, while ".equals()" compares actual content - Understanding memory behavior is essential for writing correct and efficient programs This felt like the point where Strings moved from simple syntax to real computer science understanding. Completed the core learning of the Strings module today. #Java #DSA #Strings #LearningInPublic #ProblemSolving #CodingJourney #Programming #JavaDeveloper #DeveloperJourney
To view or add a comment, sign in
-
𝐉𝐚𝐯𝐚 𝐂𝐨𝐧𝐭𝐫𝐨𝐥 𝐅𝐥𝐨𝐰 𝐢𝐧 𝐀𝐜𝐭𝐢𝐨𝐧: 𝐂𝐋𝐈 𝐂𝐚𝐥𝐜𝐮𝐥𝐚𝐭𝐨𝐫. 🧮 I built a CLI Calculator to test my understanding of Java's control flow statements. Instead of a simple linear script, I implemented: 𝐂𝐨𝐧𝐭𝐢𝐧𝐮𝐨𝐮𝐬 𝐈𝐧𝐩𝐮𝐭: Wrapped the logic in a while loop. 𝐎𝐩𝐞𝐫𝐚𝐭𝐨𝐫 𝐇𝐚𝐧𝐝𝐥𝐢𝐧𝐠: Parsed char inputs to direct program flow. 𝐌𝐚𝐭𝐡 𝐋𝐨𝐠𝐢𝐜: Handled standard and modular arithmetic dynamically. 𝐓𝐞𝐜𝐡𝐧𝐢𝐜𝐚𝐥 𝐈𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭𝐚𝐭𝐢𝐨𝐧𝐬 : 𝐒𝐭𝐚𝐭𝐞 𝐌𝐚𝐧𝐚𝐠𝐞𝐦𝐞𝐧𝐭: Maintained program execution using a while loop. 𝐂𝐨𝐧𝐝𝐢𝐭𝐢𝐨𝐧𝐚𝐥𝐬: Used complex if/else logic to handle operators (+, -, *, /) and modulo (%). 𝐈𝐧𝐩𝐮𝐭 𝐇𝐚𝐧𝐝𝐥𝐢𝐧𝐠: Processed standard input via Scanner for dynamic variable assignment. 𝐂𝐡𝐞𝐜𝐤 𝐨𝐮𝐭 𝐭𝐡𝐞 𝐝𝐞𝐦𝐨 𝐛𝐞𝐥𝐨𝐰. 👇 #Java #Coding #TechTips #LearningInPublic #BackEnd #computer #science #engineering #learning #WeMakeDevs #IntelliJ #100DaysOfCode
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
-
-
Concatenating strings in a loop? Use StringBuilder. Here's the math. Inefficient way (creates many objects): ```java String result = ""; for (int i = 0; i < 100; i++) { result += i; // New String object each iteration! } // Creates ~100 String objects ``` Efficient way: ```java StringBuilder sb = new StringBuilder(); for (int i = 0; i < 100; i++) { sb.append(i); } String result = sb.toString(); // Creates 1 StringBuilder + 1 String ``` Why it matters: · Each + in a loop creates a new String object (expensive!) · StringBuilder uses a mutable character array internally · Difference is negligible for 2-3 concatenations but significant in loops Rule of thumb: Use + for simple one-liners. Use StringBuilder in loops or when building complex strings programmatically. #Java #StringBuilder #Performance #Programming #Optimization
To view or add a comment, sign in
-
-
💡 Detecting Duplicates using Insertion Sort Logic (No Extra Space!) Instead of using HashSet or extra memory, I tried a different approach — modifying Insertion Sort. 👉 Idea: Assume the left part of the array is already sorted. When inserting the next element (key), we shift bigger elements right. During shifting: If we meet an element equal to the key → Duplicate found immediately If smaller → insert at correct position In simple words: Keep shifting while elements are bigger If equal → duplicate If smaller → insert 📊 Complexity: • Worst Case: O(n²) • Space: O(1) • Early exit if duplicate appears Nice reminder that sometimes classic sorting logic can double as a searching technique! #DSA #Java #Algorithms #ProblemSolving #CodingInterview
To view or add a comment, sign in
-
-
🎯 Day 68 of #100DaysOfCode 📌 Problem: Combination Sum II Today's challenge was a twist on the classic combination sum problem! Each number in candidates can only be used once, and the solution set must not contain duplicate combinations. 🧠 Approach: Sorted the array first to handle duplicates efficiently Used recursion with backtracking Skipped duplicate elements to avoid repeated combinations Explored all possible combinations by picking/unpicking elements 📊 Stats: ✅ 176/176 test cases passed ⚡ Runtime: 6 ms | Beats 73.38% 💾 Memory: 45.61 MB | Beats 19.56% 📝 Takeaway: The key challenge was avoiding duplicate combinations while ensuring each element is used at most once. Sorting + skipping duplicates during recursion made this elegant. Memory optimization is the next frontier! 🔗 Problem: Combination Sum II 🏷️ #LeetCode #CodingChallenge #Java #Backtracking #Recursion #Algorithms #DuplicateHandling #TechJourney #Programming
To view or add a comment, sign in
-
-
🚀 DSA Practice – Longest Subarray with Sum = K (Positive Numbers) Today I practiced a classic Sliding Window / Two Pointer problem. Problem: Find the longest subarray whose sum equals K, when the array contains only positive numbers. 💡 Key Idea: * Since all numbers are positive, we can use the sliding window technique: Expand the window by moving the right pointer. * If the sum becomes greater than k, shrink the window using the left pointer. Whenever sum == k, update the maximum length. ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(1) Example: arr = [1, 2, 3, 1, 1, 1, 1, 4, 2, 3] k = 3 Longest subarray: [1, 1, 1] Length = 3 This problem is a great example of how understanding constraints (positive numbers) allows us to replace complex approaches like HashMap + Prefix Sum with a simpler and more efficient Sliding Window technique. #DSA #Algorithms #Java #SlidingWindow #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
-
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
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