🔥 Day 25/100 of #100DaysOfCode Problem: Longest Common Prefix - Find the longest common prefix string amongst an array of strings. Solution: Used a clever sorting approach! By sorting the array lexicographically, the longest common prefix must be between the first and last strings in the sorted array. Then compared characters position by position until finding a mismatch. Key Insight: After sorting, the first and last strings represent the lexicographic extremes - any common prefix across ALL strings must be common between these two extremes! Result: O(n log n) time complexity due to sorting, but very clean and intuitive implementation using StringBuilder for efficient string building. Smart algorithmic thinking beats brute force every time! 💡 #100DaysOfCode #LeetCode #Java #Algorithms #StringManipulation #Coding #Programming
Vikramaditya Singh’s Post
More Relevant Posts
-
#Day59 of #49DaysOfDSA I solved an interesting problem today on LeetCode — Contiguous Array. Problem: Given a binary array, find the maximum length of a contiguous subarray with equal number of 0s and 1s. 🔹 My Approach: 1️⃣ Brute Force Checked all possible subarrays Counted 0s and 1s in each ✅ Correct but Time Complexity: O(n²) → TLE for large arrays 2️⃣ Optimal Approach with HashMap Key trick: convert 0 → -1, 1 → +1 Maintain a running prefix sum Use a HashMap to store the first occurrence of each sum If the same sum appears again → subarray in between has equal 0s and 1s ✅ Time Complexity: O(n), Space Complexity: O(n) #LeetCode #Coding #ProblemSolving #Java #Programming #HashMap #PrefixSum #Optimization #100DaysOfCode
To view or add a comment, sign in
-
-
🎯 Day 134 of #150DaysOfCode on LeetCode Today’s challenge was 2048. Next Greater Numerically Balanced Number — an interesting medium-level math and logic problem that pushes you to think about digit frequency patterns and number properties. A number is numerically balanced if for every digit d in it, there are exactly d occurrences of that digit. For example: 22 → digit 2 occurs twice ✅ 1333 → digit 1 occurs once, and 3 occurs thrice ✅ The task was to find the smallest numerically balanced number strictly greater than a given integer n. I solved it in Java, exploring counting techniques, loop iteration, and logical verification of digit-frequency balance. This problem highlights the importance of systematic validation and optimization, especially when handling numeric constraints efficiently. ✨ Lesson learned: Sometimes, the most elegant solutions come from simple but precise logic. #Day134 #LeetCode #Java #ProblemSolving #150DaysOfCode #CodingChallenge #Mathematics #LogicBuilding #Programming #SoftwareEngineering #DataStructuresAndAlgorithms #ConsistencyIsKey
To view or add a comment, sign in
-
-
✅Day 54 of #100DaysOfLeetCode 1.📌Problem: 728. Self Dividing Numbers 2.🟢 Difficulty: Easy 3.📍Topic: Math 4.🎯 Goal: Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right] (both inclusive). 5.🧠key idea: Approach 1: Iterate through each number in the given range. For each number, check if it meets the self-dividing criteria. This involves extracting each digit and performing two checks: the digit cannot be zero, and the original number must be divisible by the digit. If the number passes all checks for all its digits, it is added to the result list. #100DaysOfLeetCode #CodingChallenge #Java #LeetCode #ProblemSolving #SoftwareDevelopment #Developer #Programming #CodeNewbie #Tech #ComputerScience #Algorithm #DataStructures #LinkedInLearning #SelfTaughtDev
To view or add a comment, sign in
-
-
#Day13 2 – Add Two Numbers Solved this classic linked list problem by implementing an efficient algorithm to add two numbers represented as reverse-digit linked lists. 💡 Approach: Used a dummy node to simplify list construction, iterated through both lists while handling different lengths, and maintained a carry value for proper digit summation. The solution elegantly handles cases where lists have different sizes and final carry propagation. #LeetCode #Java #LinkedList #DataStructures #Algorithms #ProblemSolving #Coding #Tech #SoftwareEngineering #DailyCoding #Programming
To view or add a comment, sign in
-
-
✅Day 68 of #100DaysOfLeetCode 1.📌Problem: Given two binary strings a and b, return their sum as a binary string. 2.🟢 Difficulty: easy 3.📍Topic: Add Binary, string 4.🎯 Goal: Add two binary strings and output the result as a binary string. 5.🧠Key idea: Approach 1: Use two pointers moving from right to left on both strings, add corresponding digits and carry, append the sum modulo 2 to the result, and handle final carry if any. Reverse the result at the end to get the correct binary output. #100DaysOfCode #LeetCode #Programming #Coding #Java #SoftwareDevelopment #ProblemSolving #Developer #CodeNewbie #CodingPractice #DataStructures #Algorithms #WebDevelopment #Tech #Python #Computersciencebest
To view or add a comment, sign in
-
-
#Day25 of Problem Solving Successfully solved the Majority Element problem using the Boyer–Moore Voting Algorithm 🧠 💡 Approach Summary: Used a simple linear scan to track the majority candidate. Efficiently determines the element that appears more than ⌊n/2⌋ times. Time Complexity: O(n) Space Complexity: O(1) ⚙️ Runtime: 2 ms (Beats 74.14%) 💾 Memory: 53.54 MB Each problem solved is a small step toward stronger problem-solving and algorithmic thinking 🚀 #LeetCode #Java #Programming #ProblemSolving #Algorithms #DSA #CodingJourney #BoyerMoore #MajorityElement #Linkedin
To view or add a comment, sign in
-
-
Day 8 of Mastering DSA Patterns: Container With Most Water : Another day, another DSA challenge conquered. Today's focus was on the Two Pointer technique, and I solved the LeetCode classic: Container With Most Water. This problem is a fantastic illustration of how a clever choice of algorithm can drastically reduce time complexity. Instead of checking every pair of lines an O(n^2) brute-force solution), the Two-Pointer approach offers an efficient O(n) solution: 1.Start pointers at the farthest ends of the array. 2.Calculate the area. 3.The area is limited by the shorter line. To potentially find a larger area, you must try to increase the height. Therefore, move the pointer of the shorter line inward. Moving the longer line's pointer would only decrease or keep the height the same, offering no guaranteed improvement. 4.Repeat until the pointers meet. It's a subtle but powerful optimization! #DSA #DataStructures #Algorithms #ContainerWithMostWater #LeetCode #CodingChallenge #TwoPointers #Java #Programming #Tech
To view or add a comment, sign in
-
-
✅Day 59 of #100DaysOfLeetCode 📌Problem: Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right. 🟢 Difficulty: Easy 📍Topic: Array 🎯 Goal: To modify the input array in place by duplicating every zero and shifting other elements accordingly, without returning anything. 🧠key idea: Approach 1: Create a new result array to hold the modified elements. Iterate through the original array; if the element is a zero, add it twice to the result array. Otherwise, add the element once. Finally, copy the contents of the result array back into the original array. #100DaysOfCode #LeetCode #CodingChallenge #Java #DataStructures #Algorithms #ProblemSolving #Developer #SoftwareEngineering #CodeNewbie #Tech #Programming #ArrayManipulation #DuplicateZeros #Day59
To view or add a comment, sign in
-
-
✅Day 42 of #100DaysOfLeetCode 📌Problem: Search in Rotated Sorted Array. You are given an integer array nums that is sorted in ascending order but has been rotated at an unknown pivot. Your task is to efficiently find the index of a given target value. 🟠 Difficulty: Medium 📍Topic: Array, Binary Search 🎯 Goal: Given the rotated array and a target, return the index of the target if it exists in the array, otherwise return -1. The solution must have a runtime complexity of O(log n). 🧠key idea: Approach 1: Implement a modified binary search. In each iteration, determine which half of the array (left or right of the midpoint) is sorted. Then, check if the target value falls within the range of that sorted half. If it does, narrow the search to that half. If not, search the other half. This continues until the target is found or the search space is exhausted. #100DaysOfLeetCode #CodingChallenge #Java #LeetCode #BinarySearch #Arrays #DataStructures #Algorithms #SoftwareDevelopment #Tech #Developer #Programming #ProblemSolving #CodeNewbie
To view or add a comment, sign in
-
-
✅Day 56 of #100DaysOfLeetCode 1.📌Problem: Reverse Integer 2.🟠 Difficulty: Medium 3.📍Topic: Math 4.🎯 Goal: Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. 5.🧠key idea: Approach 1: The approach is to iteratively extract the last digit of the number and build the reversed number. A long data type is used for the result to handle potential overflow during calculation. First, the sign of the input number is stored, and the number is made positive for processing. In a loop, the last digit is obtained using the modulo operator (% 10), and the number is reduced by integer division (/ 10). The reversed number is constructed by multiplying the current result by 10 and adding the new digit. After the loop, the reversed number is checked to ensure it falls within the 32-bit integer range. If it overflows, 0 is returned. Otherwise, the original sign is reapplied, and the result is cast back to an integer. #100DaysOfLeetCode #CodingChallenge #Java #LeetCode #ProblemSolving #SoftwareDevelopment #Developer #Programming #CodeNewbie #Tech #SoftwareEngineer #DataStructures #Algorithms #Math #ReverseInteger
To view or add a comment, sign in
-