🚀 𝗗𝗮𝘆 𝟳: Just solved LeetCode Problem 28: a string challenge to find the 𝗜𝗻𝗱𝗲𝘅 𝗼𝗳 𝗮 𝗦𝘂𝗯𝘀𝘁𝗿𝗶𝗻𝗴’𝘀 𝗙𝗶𝗿𝘀𝘁 𝗢𝗰𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝗲 in a larger string! 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given two strings 𝗻𝗲𝗲𝗱𝗹𝗲 and 𝗵𝗮𝘆𝘀𝘁𝗮𝗰𝗸, return the index of the first occurrence of needle in haystack, or -𝟭 if needle is not part of haystack. The task was to locate where one string (the needle) first appears in another string (the haystack). I used a straightforward iteration with the 𝘀𝘂𝗯𝗦𝘁𝗿() method to extract slices of the haystack string matching the length of the needle. Each slice was compared to the needle to check for a match, allowing me to efficiently locate the index of the first occurrence. If no match was found, the function returned -𝟭 as specified by the problem. 📍 Excited to continue tackling more problems and sharpening my skills one challenge at a time. Onward to the next! #LeetCode #CodingJourney #DataStructures #Algorithms #Cpp #DSA #FindingIndex #FirstOccurance #Programming #DailyLearning #Developer #ProblemSolving
Rushil Sharma’s Post
More Relevant Posts
-
🚀 𝗗𝗮𝘆 𝟮𝟱: Just solved LeetCode Problem 747: 𝗟𝗮𝗿𝗴𝗲𝘀𝘁 𝗡𝘂𝗺𝗯𝗲𝗿 𝗮𝘁 𝗟𝗲𝗮𝘀𝘁 𝗧𝘄𝗶𝗰𝗲 𝗼𝗳 𝗢𝘁𝗵𝗲𝗿𝘀, a fun problem that combines array traversal and conditional checks! 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given an integer array 𝗻𝘂𝗺𝘀 where the largest integer is at least twice as much as every other number in the array. Determine whether this condition holds true. If it does, return the index of the largest number. Otherwise, return -𝟭. I looped through the array to identify the index of the largest number. Using a second loop, I compared the largest number with each element to ensure it was at least twice as large as all others. If any element violated this condition, I returned -1. If the checks passed, I returned the index of the dominant number. 📍 Feeling motivated to keep tackling more such problems! #LeetCode #CodingJourney #DataStructures #Algorithms #Cpp #DSA #LargestNumber #TwiceOfOthers #Programming #DailyLearning #Developer #ProblemSolving
To view or add a comment, sign in
-
🔍 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 3 SUM problem on leetcode 🧩 📝 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧 𝐎𝐯𝐞𝐫𝐯𝐢𝐞𝐰: 𝐓𝐚𝐬𝐤: Given an integer array, return all unique triplets [nums[i], nums[j], nums[k]] such that they sum to zero. 🔧 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Sort the Array: Sorting helps in easily avoiding duplicates and simplifying the two-pointer technique. Fix One Element: Iterate through the array, fixing one element at a time. Two-Pointer Technique: Use two pointers to find the other two elements that sum to zero with the fixed element. ✅ 𝐊𝐞𝐲 𝐂𝐨𝐧𝐬𝐢𝐝𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬: Sorting: Essential for simplifying the search and avoiding duplicates. Set for Results: Using a set ensures no duplicate triplets. Edge Cases: Handle arrays with less than three elements gracefully. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(n^2) due to the nested loop structure. 💾 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(n) for storing the result set. Mastering the art of finding triplets that sum to zero! 🌟 #ThreeSum #CodingChallenge #ProblemSolving #100DaysOfCode #DeveloperLife #LearnToCode #TechSkills #DailyCoding #CodeNewbie #Programming #Algorithm #TechCommunity #SoftwareDevelopment #CodeChallenge
To view or add a comment, sign in
-
🚀 𝗗𝗮𝘆 𝟵: Just tackled LeetCode Problem 58: a simple yet insightful challenge to find the 𝗟𝗲𝗻𝗴𝘁𝗵 𝗼𝗳 𝘁𝗵𝗲 𝗟𝗮𝘀𝘁 𝗪𝗼𝗿𝗱! 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a string 𝘀 consisting of words and spaces, return the length of the last word in the string. I tackled the problem by iterating backward from the last index of the string, efficiently handling trailing spaces by skipping them until encountering a non-space character. Once the last word began, I incremented a counter 𝗹𝗮𝘀𝘁𝗟𝗲𝗻 for each character, stopping the count when a space was found after the word. This approach avoided creating additional strings or arrays, ensuring both time and space efficiency. 📍 Excited to keep refining my problem-solving skills with every challenge! #LeetCode #CodingJourney #DataStructures #Algorithms #Cpp #DSA #LengthOfLastWord #Programming #DailyLearning #Developer #ProblemSolving
To view or add a comment, sign in
-
🚀 𝗗𝗮𝘆 𝟰: Just tackled LeetCode Problem 27: a classic challenge, 𝗥𝗲𝗺𝗼𝘃𝗲 𝗘𝗹𝗲𝗺𝗲𝗻𝘁! 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given an integer array 𝗻𝘂𝗺𝘀 and an integer 𝘃𝗮𝗹, remove all occurrences of 𝘃𝗮𝗹 in 𝗻𝘂𝗺𝘀 in-place. The relative order of the elements may be changed. I used a two-pointer technique to efficiently process the array, keeping track of elements that should remain. One pointer iterates through each element, while the other pointer helps overwrite positions where the target value is found, effectively "removing" elements in-place. This approach kept the solution fast and memory-efficient, as required by the problem constraints. 📍 Excited to keep growing my skills one problem at a time. LeetCode, bring on the next challenge! #LeetCode #CodingJourney #DataStructures #Algorithms #Cpp #DSA #RemoveElement #Programming #DailyLearning #Developer #ProblemSolving
To view or add a comment, sign in
-
🚀 𝗗𝗮𝘆 𝟴: Just solved LeetCode Problem 66: the famous one, 𝗣𝗹𝘂𝘀 𝗢𝗻𝗲! 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: You are given a non-negative integer represented as a non-empty array of 𝗱𝗶𝗴𝗶𝘁𝘀, and you need to add 𝟭 to that number. The digits are stored in reverse order (from least significant to most significant), and the goal is to return the result as an array. Here’s how I approached this: • Since we need to add 1, start from the last element of the array. • If the digit is not equal to 9, just add 1 and return the array. • If the digit is 9, set it to 0 (carry over) and move to the next digit. • If we finish the array and still have a carry (like adding 1 to [9,9,9]), prepend a 1 to the result. 📍 Feeling accomplished after solving this. Let’s keep up the momentum and tackle more challenges! #LeetCode #CodingJourney #DataStructures #Algorithms #Cpp #DSA #PlusOne #Programming #DailyLearning #Developer #ProblemSolving
To view or add a comment, sign in
-
100DaysOfCode Challenge DAY 🗓: 16/100 TOPIC : Binary Search 🎯 LEETCODE 2187 : Minimum Time to Complete Trips- https://lnkd.in/dBat3fk3 Problem Statement📝: You are given an array time where time[i] denotes the time taken by the ith bus to complete one trip. Each bus can make multiple trips successively; that is, the next trip can start immediately after completing the current trip. Also, each bus operates independently; that is, the trips of one bus do not influence the trips of any other bus. You are also given an integer totalTrips, which denotes the number of trips all buses should make in total. Return the minimum time required for all buses to complete at least totalTrips trips. Approach 💡 : 1) Finding Maximum Element in the array 2) Binary Search Algorithm ( Search Space -> lo = 1 , hi = max*totalTrips ) 3) Function for Checking time and trips TIME COMPLEXITY⏱️ : O(nlogn) SOURCE CODE🖥 : https://lnkd.in/dUdPBrym #100DaysOfCodechallenge #dsa #dailycoding #leetcodechallenge #problems #codenewbie #codepassion #programming #solved
To view or add a comment, sign in
-
53/75 Days of Leetcode Challenge !! Problem: Harshad Number Level: Easy Problem Statement: An integer divisible by the sum of its digits is known as a Harshad number. Given an integer x, we need to determine if it's a Harshad number. If it is, we return the sum of its digits; otherwise, we return -1. Here are a couple of examples: Example 1: Input: x = 18 Output: 9 Explanation: The sum of digits of x is 9. 18 is divisible by 9. So 18 is a Harshad number and the answer is 9. Here's an approach to solve this problem: Calculate the sum of digits: Iterate over the digits of the input number and add them together. You can convert the number to a string and iterate over each character, converting it back to an integer to perform the sum. Check for divisibility: If the original number is divisible by the calculated sum of digits, then it is a Harshad number. Otherwise, it is not. LeetCode Profile: https://lnkd.in/eHfs5JCC #competitiveprogramming #leetcode #harshadnumber#LeetCode #CompetitiveProgramming #CodingChallenge #Algorithm #Programming #HarshadNumber #Day53Of75 #LinkedInChallenge #TechSkills #ProblemSolving
To view or add a comment, sign in
-
Solved 3 out of 4 problems in today's LeetCode Weekly Contest 402! Question 1: I solved it simply using brute force: if (nums[i] + nums[j]) % 24 == 0, increase the count. Submission Link: https://lnkd.in/giV9CfV8 Question 2: Here, I utilized a hashMap to reduce the time complexity because my previous solution was O(N*N). However, in hurry, I received a penalty because I didn't notice the return type of the function (long). I mistakenly assumed it was the same return type as the previous problem (int). Submission Link: https://lnkd.in/gRYBpQj9 Question 3: I quickly realized this required a standard take or not take DP approach (as we can only take power[i] and not the remaining +-2), but implementing it took a long time. Submission Link: https://lnkd.in/g3BZCx7W Happy Coding! 💕 #Algorithm #CodingChallenge #CompetitiveProgramming #TechSolution #DataStructures #LeetCode #Programming #SoftwareDevelopment #ProblemSolving #TechEditorial #DynamicProgramming #CodingTips #SoftwareEngineering #TechCommunity #DevLife #TechSkills
To view or add a comment, sign in
-
Those tricky algorithms keep me on my toes! Day 20 of #100DaysOfCode focused on trapping rain water in C++. This one required some outside-the-box thinking. 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧: 𝐓𝐫𝐚𝐩𝐩𝐢𝐧𝐠 𝐑𝐚𝐢𝐧 𝐖𝐚𝐭𝐞𝐫 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. 𝐄𝐱𝐚𝐦𝐩𝐥𝐞: Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. #100DaysOfCode #LeetCode #developmentjourney #techcommunity #DataStructures #ProblemSolving #CodeNewbie #CodingChallenge #ProgrammingJourney #LearningToCode #geeksforgeeks #coderarmy #codingninjas #development #Motivation #cpp #array #twopointers You can see the code I have written to solve this problem in the image attached below, and if you want to access it directly, here is my GitHub link to the code: https://lnkd.in/gj7xqdr3 If you have any suggestions regarding the post or the approach to the question, feel free to write them down in the comment section, and let's help each other grow in this journey of coding :)
To view or add a comment, sign in
-
🚀 Day 20 of #100DaysOfCode Challenge: Today, I tackled the "Three Indices" problem, where I had to find three indices i, j, and k in a permutation such that pi<pj and pj>pk. 🧠💻 Question link : https://lnkd.in/dsd_4AGd The task was to determine if such indices exist or not in a given permutation. My approach was a simple brute-force method: I iterated through the array and checked for the condition. If the condition was met, I outputted "yes"; otherwise, "no." Github link of my solution : https://lnkd.in/dP2TvA4c I'm excited to share this approach with you and would love to hear your thoughts. If you have a different or more efficient approach, or if you'd like clarification on my method, let's connect and discuss! 🔗 #100DaysOfCode #CodingChallenge #PersonalGrowth #Consistency #Discipline #Programming #TechJourney #CodeforcesStreak #Coding #Algorithm #Optimization
To view or add a comment, sign in
--
1moWishing you the best