Today’s challenge was to find the longest common prefix shared by all strings in an array. I implemented the solution in JavaScript and successfully passed all 126 test cases.
The approach is straightforward and easy to understand. I started with the first string as the reference and compared each of its characters with the corresponding character in every other string. If a character does not match, or if another string ends before that position, the common prefix is returned immediately.
For example:
Input: ["flower", "flow", "flight"]
Output: "fl"
Another example:
Input: ["dog", "racecar", "car"]
Output: ""
This problem is a great exercise for practicing nested loops, string indexing, array traversal, and early returns in JavaScript. It also highlights the importance of checking edge cases such as an empty array, an empty string, or strings with no characters in common.
My submitted solution was accepted with a runtime of 3 ms, which makes this a nice addition to my ongoing problem-solving practice.
Every LeetCode problem is an opportunity to improve logical thinking, write cleaner code, and understand different approaches to solving programming challenges.
I’m continuing to practice consistently and strengthen my JavaScript and data-structure skills one problem at a time. 💻
This journey is helping me become more confident with algorithms while building a consistent habit of learning, coding, testing, and improving every day. Small problems today can build stronger fundamentals and make larger coding challenges easier to approach with confidence and patience.
#LeetCode #JavaScript #Coding #Programming #WebDevelopment #FrontendDevelopment #DSA #DataStructures #Algorithms #ProblemSolving #100DaysOfCode #CodeNewbie #Developer #SoftwareDevelopment #TechJourney