Skip to main content

Command Palette

Search for a command to run...

Longest Common Prefix

Published
2 min read
D

Hello everyone, I'm Dhanraj, a CSIT Btech student currently pursuing my degree at Rajarambapu Institute of Technology. I'm a passionate web developer and Java programmer, with a keen interest in data structures and algorithms.

Programming has always been my passion since my school days, and I have been constantly exploring the world of computer science. My fascination with web development began when I discovered the power of the internet, and since then, I have been honing my skills to create beautiful, functional websites.

I believe that perseverance and hard work are the keys to success in any field, and programming is no exception. I enjoy the challenge of solving complex problems and take pride in delivering high-quality code that meets the requirements of my clients.

Apart from programming, I also have a keen interest in playing chess and expanding my knowledge of the stock market. I strongly believe that a well-rounded personality is crucial for success in life, and I always strive to balance my academic pursuits with my extracurricular activities.

I'm excited to share my experiences and knowledge with you through my blog. Whether you're a beginner or an experienced programmer, I hope that my articles will inspire you to push your limits and reach new heights in your programming journey.

Hello friends today I have solved the problem of longest common prefix on the leetcode which is based on the topic strings. In this problem as give below we have to solve the longest common prefix between strings that are present in arrays so first we sort the array using Arrays .sort() method because strings are sorted by alphabetical manner, hence we can compare first and list string only. Initialize the string s1 and s2 to the first and last strings of arrays. Iterate the loop until the length of s1 and s2 note that you are using and operator and then check the characters one by one by incrementing i if condition becomes not true then get out of loop using break statement and return the substring of any of s1 or s2 till the incremented i using substring method.

Problem : Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Constraints:

  • 1 <= strs.length <= 200

  • 0 <= strs[i].length <= 200

  • strs[i] consists of only lowercase English letters.

Solution:

class Solution {
    public String longestCommonPrefix(String[] strs) {

        Arrays.sort(strs);
        String s1 = strs[0];
        String s2 = strs[strs.length-1];

        int i = 0;

        while(i<s1.length() && i<s2.length())
        {
            if(s1.charAt(i) == s2.charAt(i))
                i++;
            else
                break;
        }

        return s1.substring(0, i);

    }
}
B

Your solution demonstrates a good understanding of string manipulation and the use of built-in methods. Keep up the good work! 🙌

D

Thank you for your kind words. I’m glad you liked my solution.😊