100 Days of Coding Challenge – Day 30 📌 Problem: Find the Index of the First Occurrence in a String 💻 Language: Java 🧠 Concept Used: String Matching (Brute Force) 🔍 Platform: LeetCode Today’s challenge was to find the first occurrence of a substring (needle) in a given string (haystack). If not found, return -1. Example: Input: "sadbutsad", "sad" Output: 0 Approach: ✔ Traverse the string from index 0 to n - m ✔ Extract substring of length m at each position ✔ Compare it with the target string ✔ Return index immediately when match is found ✔ If no match → return -1 Time Complexity: O(n × m) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/g2ktYFFS 🔗 Code: https://lnkd.in/gynFixSQ #100DaysOfCode #Day30 #Java #DSA #LeetCode #Strings #ProblemSolving #CodingJourney
Java String Matching Challenge: Find First Occurrence
More Relevant Posts
-
100 Days of Coding Challenge – Day 35 📌 Problem: Longest Repeating Character Replacement 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Sliding Window + Frequency Count 1. Use a sliding window with two pointers (left and right) 2. Maintain a frequency array for characters 3. Track the count of the most frequent character in the window 4. If (window size - max frequency) > k, shrink the window 5. Keep updating the maximum window length 👉 Key Insight: We can replace at most k characters, so window is valid if remaining characters ≤ k Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gD4WeQJU 🔗 Code Link: https://lnkd.in/gEyEXyXB #100DaysOfCode #Day34 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #SlidingWindow #Strings
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 37 📌 Problem: Longest Repeating Character Replacement 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Sliding Window + Frequency Count 1. Use two pointers (left, right) to maintain a window 2. Keep a frequency array to count characters in the window 3. Track the count of the most frequent character 4. If (window size - max frequency) > k, shrink the window 5. Update the maximum valid window length 👉 Key Insight: Replace at most k characters to make all characters in the window the same Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gD4WeQJU 🔗 Code Link: https://lnkd.in/g2qqRmfw #100DaysOfCode #Day37 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #SlidingWindow #Strings
To view or add a comment, sign in
-
-
Day 21 of #100DaysOfCode Today, I worked on the classic string problem: implementing the strStr() function in Java. The goal: Find the first occurrence of a substring (needle) inside another string (haystack). Approach I used: - Applied a sliding window technique - Compared substrings using "substring(i, j)" - Returned the starting index when a match is found Key learning: Understanding how index-based string operations work and how "substring()" helps in breaking down problems step-by-step. Also realized the importance of optimizing solutions to avoid unnecessary string creation. Consistency is slowly turning concepts into confidence! Looking forward to improving this further with more optimized approaches #Java #Coding #DSA #LeetCode #ProblemSolving #LearningJourney #Consistency
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 38 📌 Problem: Max Consecutive Ones III 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Sliding Window (Two Pointers) 1. Use two pointers (left and right) to maintain a window 2. Count the number of zeros in the current window 3. Expand the window by moving right 4. If zeros exceed k, shrink the window from the left 5. Track the maximum window size (right - left + 1) Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gcryD9Np 🔗 Code Link: https://lnkd.in/g44uWAsX #100DaysOfCode #Day38 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #SlidingWindow #TwoPointers
To view or add a comment, sign in
-
-
Day 93 - LeetCode Journey Solved LeetCode 9: Palindrome Number in Java ✅ At first glance, it feels like a string problem… but the real challenge is solving it without converting to string. Instead of reversing the whole number, I reversed only half of it and compared both parts. This avoids overflow and keeps it efficient. Smart approach > brute force 💡 Key takeaways: • Handling edge cases (negative numbers, trailing zeroes) • Reversing only half of the number • Avoiding extra space (no string conversion) • Writing optimized mathematical logic ✅ All test cases passed ⚡ O(log n) time and O(1) space Sometimes the best solutions are the simplest ones, just a different way of thinking 🔥 #LeetCode #DSA #Java #Math #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 32 📌 Problem: Subarray Product Less Than K 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Sliding Window (Two Pointers) 1. Initialize two pointers (left and right) and a variable to track product 2. Expand the window by moving right and multiply elements 3. If product becomes ≥ k, shrink the window from the left 4. For each valid window, count subarrays using right - left + 1 5. Accumulate the count to get the final answer 👉 Key Insight: If a window is valid, all its subarrays ending at right are also valid Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gFWbBHeF 🔗 Code Link: https://lnkd.in/gxZF2_JA #100DaysOfCode #Day32 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #SlidingWindow #TwoPointers
To view or add a comment, sign in
-
-
Day 73 of #90DaysDSAChallenge Solved LeetCode 451: Sort Characters By Frequency Learned an important Java design concept today. Problem Overview: The task was to sort characters in a string based on descending frequency. What confused me initially: Why create a separate Freq class instead of just using HashMap and PriorityQueue directly? Key Learning: PriorityQueue stores one complete object at a time. For this problem, each item needs two pieces of data together: Character Frequency Example: Instead of storing: e and 2 separately We package them as: Freq('e', 2) That custom class acts like a container holding both values in one object, so PriorityQueue can compare and sort them correctly. Why this matters: This taught me that custom classes in Java are often not about complexity, they simply bundle related data into one manageable unit. Alternative approach: We can also use Map.Entry<Character, Integer> instead of creating a custom class, but building Freq makes the logic easier to understand while learning. Today’s takeaway: Not every class is for business logic — sometimes it exists just to package data cleanly. #Java #90DaysDSAChallenge #LeetCode #PriorityQueue #HashMap #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Mastering Java Through LeetCode 🧠 Day 32 Today I solved an Easy-level Tree problem that strengthened my understanding of Recursion + Depth Calculation — a fundamental concept for tree-based problems 📌 LeetCode Problem Solved: Q.104. Maximum Depth of Binary Tree 💭 Problem Summary: Given a binary tree, the task is to find the maximum depth — i.e., the number of nodes along the longest path from root to leaf. 🧠 Approach (Optimal): Instead of iterating level by level, I used a clean recursive approach: ✔️ If node is null → return 0 ✔️ Recursively calculate left subtree depth ✔️ Recursively calculate right subtree depth ✔️ Return 1 + max(left, right) ⚡ Key Learning: Recursion makes tree problems much simpler when you think in terms of “what should each function return for a node?” Complexity: Time: O(n) Space: O(h) Takeaway: Tree problems become easier once you master recursion and start recognizing patterns. Consistency is the key — showing up every day #Day32 #LeetCode #Java #DSA #CodingJourney #Recursion #BinaryTree #100DaysOfCode #SoftwareEngineering
To view or add a comment, sign in
-
-
Leetcode Question Number 1848: Minimum Distance to the Target Element. I solved an interesting problem on finding the minimum distance in an array using Java!(Leetcode Daily Challenge) Given an array, a target value, and a starting index, the task is to find the minimum distance between the start index and any occurrence of the target element. Approach I used: 1.Traverse the array 2.Check for target element 3.Calculate distance using Math.abs(start - i) 4.Track minimum using Math.min() #Leetcode #DSA #Leetcode1848 #Java #Leetcodedailychallenge
To view or add a comment, sign in
-
-
Day 22 of #50DaysLeetCode Challenge 🚀 Today I solved the “Remove Element” problem using Java. 🔹 Problem: Given an array and a value, remove all occurrences of that value in-place and return the number of remaining elements. 🔹 Approach: Used the two-pointer technique. One pointer iterates through the array, while the other keeps track of where to place elements that are not equal to the given value. 🔹 Key Learning: In-place array problems often rely on pointer manipulation instead of extra space. 🔹 Time Complexity: O(n) 🔹 Example: Input: nums = [3,2,2,3], val = 3 Output: 2 (array becomes [2,2,...]) Simple problem, but important for mastering array manipulation techniques. #Day22 #LeetCode #Java #DSA #Arrays #TwoPointers #CodingJourney #ProblemSolving
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