LeetCode 28 – Find the Index of the First Occurrence in a String 🧠 What I learned today: Sometimes the smartest solution is knowing when NOT to overthink 😌 Java’s built-in indexOf() does exactly what the problem asks—clean, efficient, and readable. 💡 Solution Insight: Returns the first index of needle in haystack Automatically returns -1 if not found Time saved = energy saved ⚡ 😄 Fun note: Why write 20 lines of logic when one method call can do the job? indexOf() today, custom logic tomorrow 😉 📈 Consistency > Complexity On to Day 4! 🔥 #100DaysOfCode #DSA #Java #LeetCode #CodingJourney #ProblemSolving #Consistency #LearnByDoing
Java indexOf() Method for String Search
More Relevant Posts
-
Day27 - LeetCode Journey Solved LeetCode 709: To Lower Case in Java ✅ Sometimes the simplest problems remind you how powerful built-in methods can be when used correctly. This one was all about clean transformation and understanding how strings behave in Java. Converting every uppercase character to lowercase looks easy on the surface, but it reinforces an important idea: knowing your language tools well can save time and keep your code simple and readable. Using toLowerCase() felt straightforward and efficient. No extra loops, no unnecessary logic. Just clean and precise. Key takeaways: • Better understanding of Java’s String utility methods • Writing minimal and readable code • Trusting standard library functions for optimized performance ✅ All test cases passed ✅ Runtime performance at 100% Small wins like these build confidence. One line of code, one step closer to mastering the fundamentals 💪 #LeetCode #Java #DSA #Strings #ProblemSolving #CleanCode #CodingJourney #DailyPractice #Consistency #InterviewPreparation
To view or add a comment, sign in
-
-
Day 1/30 – LeetCode #1 (Two Sum) | Java Kicked off my 30-day LeetCode challenge with Two Sum, focusing on writing a clean and efficient Java solution. My initial approach was the brute-force method using two nested loops to check all index pairs. While this approach is straightforward and logically correct, it runs in O(n²) time, which becomes inefficient as the input size grows. This made me pause and rethink the solution from a performance standpoint. The key realization was that the problem isn’t about comparing every pair, but about tracking previously seen values. By using a HashMap<Integer, Integer> to store each number along with its index, I was able to check whether the required complement already exists in constant time. This reduced the overall time complexity to O(n) while maintaining O(n) space complexity. What this problem reinforced for me: Optimizing code often starts with identifying unnecessary repeated work Java collections like HashMap are powerful when used intentionally A correct solution isn’t complete unless it’s also efficient This was a good reminder that strong problem-solving comes from understanding time–space tradeoffs, not just passing test cases. Looking forward to building consistency over the next 30 days. #LeetCode #Java #DSA #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
💡 I Got Stuck Understanding Local vs Instance Variables in Java I faced a simple but tricky confusion Week 3 of Learning Java At first glance, both local variable and an instance variable looked the same to me. But once I tested it with code, things became clear: Here’s the clarity I gained: Local variables------ Declared inside a method Exist only during method execution Must be initialized before use Not accessible outside the method Instance variables----- Declared inside a class but outside methods Belong to an object Automatically get default values Can be accessed by any non-static method of that class 👉 this.variableName always refers to the instance variable, not the local one. #Java #CoreJava #OopsConcepts #learnInPublic #BuildInPublic
To view or add a comment, sign in
-
DSA journey 🚀 📌 LeetCode #167 – Two Sum II (Input Array Is Sorted) 💻 Language: Java 🔹 Approach (Two Pointer Technique): Initialize two pointers at the start and end of the sorted array Calculate the sum of both pointers If the sum equals the target → return 1-based indices If the sum is smaller than the target → move the start pointer forward If the sum is greater than the target → move the end pointer backward Efficient use of the sorted property 💡 ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(1) Step-by-step clarity before optimization ✨ Consistency over perfection 💯 #DSA #Java #LeetCode #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Day 8/30 – LeetCode #14 (Longest Common Prefix) | Java The challenge here wasn’t complexity, but handling edge cases cleanly. My initial thought was to compare characters column by column, but managing bounds made the logic harder to read. Using the first string as a reference prefix and gradually shrinking it with substring() until it matched all other strings simplified the solution. The startsWith() check kept the code readable while maintaining correctness. This problem reinforced how clarity in string manipulation often matters more than clever logic. #LeetCode #Java #DSA #Strings #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
🚀Day 13 of #120DaysOfCode 📌 Problem: Reverse String(344) 📌 Language: Java 🔍Approach(Two Pointer Technique) 1. Use two pointer . left --> start of the array index . right --> end of the array 2. While left < right: . Swaps[left] and s[right] . Move left forward (left++) . Move right backward (right--) 3. Stop when pointers meet or cross ⏱️ Time and Space Complexity Time Complexity: O(n) Space complexity: O(1) 🔥One problem closer to mastery #120DaysOfCode #Day13 #Java #Array #Leetcode #ProblemSolving #Consistency #LearningEveryday #LearningPublic #Akshay #DSA
To view or add a comment, sign in
-
-
🚀Day 9 of #120DaysOfCode 📌 Problem: Largest Number At Least Twice of Others(747) 📌 Language: Java 🔍Approach 1. Traverse the array once to find the largest (max1) and second largest (max2) elements. 2. While updating the largest element, also store it's index. 3. After traversal, check if max >= 2 * max2 4. If true, return the stored index; otherwise, return -1. ⏱️ Time and Space Complexity Time Complexity: O(n) Space complexity: O(1) 🔥One problem closer to mastery #120DaysOfCode #Day9 #Java #Array #Leetcode #ProblemSolving #Consistency #LearningEveryday #LearningPublic #DSA
To view or add a comment, sign in
-
-
🚀Day 5 of #120DaysOfCode 📌 Problem: Height Checker 📌 Language: Java 🔍Approach(Two Pointer Technique) 1. make a copy of heights 2. Sort the copy --> this becomes expected 3. Traverse both arrays 4. Count indices where values differ 🔥 Key Insight 1. Create the expected order by sorting a copy of heights 2. Compare both arrays index by index 3. Count mismatches ⏱️ Time and Space Complexity Time Complexity: O(n log n) Space complexity: O(n) 🔥One problem closer to mastery #120DaysOfCode #Day5 #Java #Array #Leetcode #ProblemSolving #Consistency #LearningEveryday #LearningPublic #DSA
To view or add a comment, sign in
-
-
STOP writing "clever" code. 🛑 I was reading Java docs and found this gem: result |= c.add(element); Spent 10 minutes figuring out what it does. Should've taken 10 seconds. That's the problem right there. Swipe through the carousel to see the REAL cost of "clever" code → #CleanCode #ReadableCode #KeepItSimple #JavaTips #CodeQuality #SoftwareEngineering #ProgrammingWisdom #DeveloperLife #CodeReview #Maintainability #TechLeadership #StopWritingCleverCode #ProgrammingTips
To view or add a comment, sign in
-
Day 3 of #120DaysOfCode🚀 📌Problem: Replace Elements with Greatest Element on Right Side 📌Language: Java 🔍Approach 1. Maintain a variable maxRight 2. Initially set it to -1 (for last element) 3. Traverse from the end 4. For each element . Save current value . Replace it with maxRight . Update maxRight ⏱️Time & Space Complexity Time Complexity: O(n) Space complexity: O(1) #120DaysOfCode #Day3 #Java #Arrays #DSA #ProblemSolving #Consistenncy #LearningEverday #LearninhPublic #Leetcode
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