š Day 77 of #100DaysOfCode ā Find the Second Largest Digit Hey everyone! š Today's challenge isĀ "Second Largest Digit in a String"Ā ā a great problem for practicingĀ digit extraction,Ā comparison logic, andĀ edge-case handling. Weāll search through a mixed string of letters and numbers to find theĀ second largest digit, or returnĀ -1Ā if it doesnāt exist. šØš»Ā What l practiced today: ā Ā String Traversal: Looping through each character and identifying digits. ā Ā Digit Comparison: Keeping track of the largest and second-largest digits efficiently. ā Ā Edge Cases: Handling cases with no digits, only one digit, or duplicate digits. šĀ Todayās Task: ā Given an alphanumeric stringĀ s. ā Return theĀ second largest numerical digitĀ that appears inĀ s. ā ReturnĀ -1Ā if it doesnāt exist. Example: Input:Ā s = "dfa12321afd" Digits:Ā [1, 2, 3] Second largest:Ā 2Ā ā Output:Ā 2 š§ Ā Key Insight: You can solve this by scanning the string once, tracking theĀ largestĀ andĀ second largestĀ digits. Remember toĀ skip duplicatesĀ and handle cases where all digits are the same. #100DaysOfCode #Day77 #Python #LeetCode #DSA #StringManipulation #DigitExtraction #EdgeCases #AlgorithmPractice #LogicBuilding
Pradumya Guptaās Post
More Relevant Posts
-
š Day 9/30 | LeetCode Problem: Binary Search (704) Problem: Given a sorted array of integers nums and a target, return the index if the target exists. If not, return -1. š§ Approach: Binary Search Since the array is already sorted, we can apply Binary Search: Use two pointers: l (left) and r (right) Find the middle element Compare target with nums[mid] Reduce the search space by half each time This makes the solution very efficient. ā±ļø Complexity Time: O(log n) Space: O(1) š§¾ Python Code class Solution(object): def search(self, nums, target): l = 0 r = len(nums) - 1 while l <= r: mid = (l + r) // 2 if target == nums[mid]: return mid elif target < nums[mid]: r = mid - 1 else: l = mid + 1 return -1 ā Result Accepted ā Runtime: 0 ms (Beats 100%) Memory efficient šÆ Key Learning Binary Search is a foundational algorithm used in: Search problems Optimization problems Interview questions Mastering it is essential for every developer. š Hashtags #LeetCode #30DaysOfLeetCode #Day9 #BinarySearch #Python #DSA #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
āļø takeUforward ā Day 38 ā A Number After a Double Reversal | Number Manipulation | LeetCode Today I worked on a number manipulation problem involving reversing digits twice and checking if the result equals the original number. š” Problem Statement Given an integer "num", reverse the number twice and check whether the final value is equal to the original number. š§ Approach ⢠Store the original number. ⢠Reverse the number using modulo ("% 10") and division ("// 10"). ⢠Reverse the result again. ⢠Compare the final value with the original number. š Python Code class Solution: def isSameAfterReversals(self, num: int) -> bool: orginal = num sum_ = 0 sum2 = 0 while num > 0: last_digit = num % 10 sum_ = (sum_ * 10) + last_digit num //= 10 while sum_ > 0: lsd = sum_ % 10 sum2 = (sum2 * 10) + lsd sum_ //= 10 return True if sum2 == orginal else False ā± Complexity ⢠Time: O(d) ā where d is number of digits ⢠Space: O(1) š Key Learning Reversing numbers using digit extraction helps build strong fundamentals in number-based algorithms and edge case handling. š Day 38 completed ā improving number manipulation skills step by step. #Python #DSA #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
-
š Day 78 of #100DaysOfCode ā Reverse Integer Challenge Hey everyone! š Today's challenge is a classic:Ā "Reverse Integer"Ā ā a problem that tests your understanding ofĀ integer manipulation,Ā overflow handling, andĀ edge cases with negative numbers. We need to reverse the digits of a signed 32-bit integer, but catch the overflow if the reversed number goes beyond the 32-bit range. šØš»Ā What l practiced today: ā Ā Digit Extraction: Using modulo and division to peel off digits. ā Ā Integer Reversal: Building the reversed number step by step. ā Ā Overflow Prevention: Checking boundaries before the number exceeds 32-bit limits. ā Ā Sign Handling: Properly managing negative integers. šĀ Todayās Task: ā Given a signed 32-bit integerĀ x. ā ReturnĀ xĀ with its digits reversed. ā If reversing causes overflow (outsideĀ [ā231,231ā1] [ā231 ,231 ā1]), returnĀ 0. ā YouĀ cannotĀ use 64-bit integers for storage. Examples: Input:Ā x = 123Ā ā Output:Ā 321 Input:Ā x = -123Ā ā Output:Ā -321 Input:Ā x = 120Ā ā Output:Ā 21 š§ Ā Key Insight: Reverse digits usingĀ while x != 0, butĀ check for overflow before multiplyingĀ the reversed result by 10. UseĀ INT_MAXĀ andĀ INT_MINĀ equivalents (orĀ 2**31 - 1Ā andĀ -2**31Ā in Python) to validate. šĀ Hashtags: #100DaysOfCode #Day78 #Python #LeetCode #DSA #IntegerManipulation #OverflowHandling #MathInCode #AlgorithmPractice #LogicBuilding
To view or add a comment, sign in
-
-
š LeetCode 41 ā First Missing Positive (Hard) Solved the classic First Missing Positive problem using two different approaches. š§© Problem: Given an unsorted integer array, find the smallest missing positive integer in O(n) time and O(1) space. š Try the problem here:https://lnkd.in/gCAGSftq Approach 1: Using Hash Set š” Idea: Store all elements in a set and check from 1 upward to find the first missing number. ā± Time Complexity: O(n) š¦ Space Complexity: O(n) ā (extra space used) š Why Not Optimal? Uses extra memory, so it doesnāt satisfy the O(1) space requirement. Approach 2: Cyclic Sort (Optimal Solution) š” Idea: Place each number x at index x - 1 if 1 ⤠x ⤠n. Then scan the array to find the first index where nums[i] != i+1. ā± Time Complexity: O(n) š¦ Space Complexity: O(1) ā š„ Why This Works? We use the array itself as a hash map by placing numbers in their correct indices. Each element is swapped at most once ā overall linear time. If you're preparing for interviews, this is a must-know hard-level pattern šŖ #LeetCode #Coding #DataStructures #Algorithms #DSA #Python #CodingInterview #InterviewPreparation #ProblemSolving #SoftwareDeveloper #100DaysOfCode #TechCareers #ProgrammersLife #CodeNewbie #CompetitiveProgramming
To view or add a comment, sign in
-
-
Day 52 of 1022. Sum of Root To Leaf Binary Numbers Todayās problem was a great mix of binary representation + tree traversal. Each root-to-leaf path in the binary tree forms a binary number, and the goal is to compute the sum of all those numbers efficiently. Key Insight: Instead of storing the entire path, we can build the number on the go using: ā”ļø current = (current << 1) | node.val This is equivalent to: Left shift ā multiply by 2 Add current bit Reached a leaf node? That means we formed one complete binary number ā add it to the result. š± What I practiced today: ā Depth First Search (DFS) ā Bit Manipulation in Trees ā Writing clean recursive logic ā Optimizing space by avoiding extra storage ā± Time Complexity: O(n) š¦ Space Complexity: O(h) ā recursion stack ⨠This problem is a perfect example of how combining concepts (Trees + Bits) leads to elegant solutions. Consistency is the real key š ā learning something new every single day! #Day52 LeetCode BinaryTree DSA Journey Coding JavaScript Notes #Python #ProblemSolving TechGrowth
To view or add a comment, sign in
-
-
šDay 12 of #75DaysofLeetcode LeetCode #11 ā Container With Most Water (Medium) Today I solved the classic Two Pointer problem: Container With Most Water. š Problem Summary: Given an array height, each element represents a vertical line. The goal is to choose two lines such that together with the x-axis they form a container that holds the maximum amount of water. š” Key Insight: Instead of checking all pairs (O(n²)), we can use the Two Pointer Technique: ā Start with one pointer at the beginning and one at the end ā Calculate the container area using area = min(height[left], height[right]) * (right - left) ā Move the pointer pointing to the smaller height ā Keep track of the maximum area ā” Optimal Complexity: ā± Time Complexity: O(n) š¦ Space Complexity: O(1) š» Python Implementation: from typing import List class Solution: def maxArea(self, height: List[int]) -> int: left, right = 0, len(height) - 1 max_water = 0 while left < right: area = min(height[left], height[right]) * (right - left) max_water = max(max_water, area) if height[left] < height[right]: left += 1 else: right -= 1 return max_water š This problem reinforced how two pointers can reduce a brute force problem from O(n²) to O(n). Consistency in solving problems daily is slowly improving my problem-solving skills and algorithmic thinking. šŖ #LeetCode #DSA #Python #TwoPointers #ProblemSolving #CodingPractice #100DaysOfCode
To view or add a comment, sign in
-
-
š Day 29/181 ā Optimization Mindset Today I revisited Group Anagrams and focused on improving my approach. ā Step 1 ā Used defaultdict Learned the importance of defaultdict(list): Automatically initializes missing keys Cleaner and more Pythonic Reduces conditional checks ā Step 2 ā Optimized the Solution Approach 1: Sorted each word and used it as a key. Time Complexity: O(N Ć K log K) Approach 2 (Optimized): Used character frequency count (26-size array) as the key. Converted it to a tuple for hashing. Time Complexity improved to: O(N Ć K) šÆ Key Takeaway Solving a problem is good. Optimizing it is better. Understanding why it improves is best. One month of consistency completed. On to stronger patterns šŖš„ #DSA #Optimization #HashMap #Python #LeetCode #CodingJourney #Consistency
To view or add a comment, sign in
-
ā First Unique Character in a String | Hash Map | LeetCode Solved the First Unique Character in a String problem using a frequency dictionary approach. š” Problem Given a string s, return the index of the first non-repeating character. If it does not exist, return -1. ā” Approach ā Hash Map ⢠Traverse the string and store character frequencies in a dictionary. ⢠Iterate through the dictionary to find the character with frequency 1. ⢠Return its index using s.index(char). ā± Complexity ⢠Time: O(n) ⢠Space: O(1) (since character set is limited) š§ Key Learning Frequency counting is a powerful technique for string problems. Hash maps help reduce nested loops and improve efficiency. Small string problems like this build a strong foundation for advanced DSA concepts. #LeetCode #DSA #Python #HashMap #StringProblems #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
LeetCode POTD š«: Description: You are given an integer arrayĀ numsĀ that represents a circular array. Your task is to create a new arrayĀ resultĀ of theĀ sameĀ size, following these rules: For each indexĀ iĀ (whereĀ 0 <= i < nums.length), perform the followingĀ independentĀ actions: - IfĀ nums[i] > 0: Start at indexĀ iĀ and moveĀ nums[i]Ā steps to theĀ rightĀ in the circular array. SetĀ result[i]Ā to the value of the index where you land. - IfĀ nums[i] < 0: Start at indexĀ iĀ and moveĀ abs(nums[i])Ā steps to theĀ leftĀ in the circular array. SetĀ result[i]Ā to the value of the index where you land. - IfĀ nums[i] == 0: SetĀ result[i]Ā toĀ nums[i]. Return the new arrayĀ result. Note:Ā SinceĀ numsĀ is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end. Here's my solution: https://lnkd.in/gczPri7j #Python #DSA #Leetcode #DailyChallenge #Easy
To view or add a comment, sign in
-
-
Just solved LeetCode 862: Shortest Subarray with Sum at Least K If you've ever been trapped by a sliding window that just won't slide right, this one is for you! š The Brute Force (O(N²)) The most intuitive approach is to calculate the prefix sum of the array, and then use a nested loop to check the sum of every possible subarray, keeping track of the shortest one that is >= K. The problem? The array length can be up to 10āµ. An O(N²) solution will immediately hit a Time Limit Exceeded (TLE). We need O(N). Normally, to find a shortest/longest subarray, we reach for the Sliding Window technique. But there's a catch: The array contains negative numbers. Because of negative numbers, our prefix sum is NOT monotonically increasing. The standard sliding window is completely broken here. To fix this, we need a way to track prefix sums intelligently. We can use a Double-Ended Queue (Deque) to store tuples of (prefix_sum, index) and force it to be strictly increasing (monotonic). Shrinking the window: As we iterate, we check if current_sum - smallest_prefix_sum_in_deque >= K. If it is, we found a valid subarray! We record the length, and throw away that starting index (pop left). Why? Because any future subarray using that start index would only be longer and therefore worse. Maintaining Monotonicity (Pop Right): What if our current_sum dips and is smaller than the most recent prefix sums in our deque? We can keep popping the back of queue as the older greater prefix sums work but make the subarray longer. This approach has a time complexity of O(n) and a space complexity of O(n) as well as in the worst case, the queue may contain n prefix sum where n: number of elements in the array. #LeetCode #Python #Algorithms #DataStructures #SoftwareEngineering #ProblemSolving #CodingInterviews
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