Solved Longest Common Prefix problem in LeetCode Challenge

💻 LeetCode 50 Days Challenge — Day 2: Longest Common Prefix Day 2 of my #LeetCode50DaysChallenge ✅ Today’s problem was all about string manipulation — Longest Common Prefix ✨ 🧩 Problem: Given an array of strings, find the longest common prefix among them. If there’s no common prefix, return an empty string "". 💡 Approach: I sorted the array alphabetically, then compared the first and last strings (since they’ll have the maximum difference). By comparing characters until they differ, I was able to build the common prefix using a StringBuilder. This approach is both elegant and efficient for the problem! ⏱️ Time Complexity: O(n × m) — where n is the number of strings and m is the length of the shortest string. 📊 Example: Input: ["flower", "flow", "flight"] Output: "fl" This problem really highlights how a simple sorting trick can simplify logic significantly. Feeling more confident with string problems — consistency is the key! 🔥 #LeetCode #CodingChallenge #Day2 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories