Problem Title: Longest Common Prefix in an Array
Day 7: Longest Common Prefix in an Array

Problem Title: Longest Common Prefix in an Array

This Problem is listed in GFG sheet

Problem Statement

Given an array of N strings, find the longest common prefix among all strings present in the array.

Example 1

Input:
N = 4
arr[] = {geeksforgeeks, geeks, geek,
         geezer}
Output: gee
Explanation: "gee" is the longest common
prefix in all the given strings.        

Example 2

Input: 
N = 2
arr[] = {hello, world}
Output: -1
Explanation: There's no common prefix
in the given strings.        

Your Task

You don't need to read input or print anything. Your task is to complete the function longestCommonPrefix() which takes the string array arr[] and its size N as inputs and returns the longest common prefix common in all the strings in the array. If there's no prefix common in all the strings, return -1.

  • Expected Time Complexity: O(N*min(|arri|)).
  • Expected Auxiliary Space: O(min(|arri|))


Java Code

class Solution{
    String longestCommonPrefix(String arr[], int n){
       if (n == 0) {
            return "-1";
        }
        String res = arr[0];
        for (int i = 1; i < n; i++) {
            while (arr[i].indexOf(res) != 0) {
                res = res.substring(0, res.length() - 1);
                if (res.isEmpty()) {
                    return "-1";
                }
            }
        }
        return res;
    }
}        



Approach

  • This problem will find the longest common prefix among an array of strings.
  • Initially we need to pass the parameters in the method
  • arr : An array of string for which the method longestCommonPrefix will find the longest common prefix. n The number of strings in the array.
  • If Condition :
  • if there are no string in the array (n == 0) the method returns -1 because there is no common prefix to find.
  • If string is present :
  • The variable res is initialized with the first string in the array. This will be used to find the common prefix.
  • For Loop:
  • for(int i = 1; i < n; i++) The loop starts from the second index and iterates each string in the array.
  • Checking for common prefix:
  • This can be done using while loop
  • while(arr[i].indexOf(res) !=0) For each string arr[i] this loop checks if res is a prefix or not.
  • The method indexOf(res) will return the index within the string if res is found.
  • What if there is no common prefix :

if(res.isEmpty()){
return "-1";
}        

  • The above condition will return -1 if there is no common prefix.
  • If the res is found return the result.


Happy Coding !!!
Happy Coding


要查看或添加评论,请登录

Dhruva Bhat S N的更多文章

  • Problem Title : Left Rotate Matrix K times

    Problem Title : Left Rotate Matrix K times

    Problem Statement: You are given an integer k and matrix mat. Rotate the elements of the given matrix to the left k…

  • Problem Title : Summed Matrix

    Problem Title : Summed Matrix

    Problem Statement: A matrix is constructed of size and given an integer ‘’. The value at every cell of the matrix is…

  • Problem Title: K Sized Subarray Maximum

    Problem Title: K Sized Subarray Maximum

    Problem Statement: Given an array arr[] of size N and an integer K. Find the maximum for each and every contiguous…

  • Problem title: Minimum Platforms

    Problem title: Minimum Platforms

    Problem Statement: Given arrival and departure times of all trains that reach a railway station. Find the minimum…

  • Problem Title: Count Inversions

    Problem Title: Count Inversions

    Problem Description Given an array of integers. Find the Inversion Count in the array.

  • Problem Title: Minimize the Heights II

    Problem Title: Minimize the Heights II

    Problem Statement Given an array denoting heights of towers and a positive integer . For each tower, you must perform…

  • Problem Title: Kth Smallest

    Problem Title: Kth Smallest

    Problem Statement Given an array arr[] and an integer k where k is smaller than the size of the array, the task is to…

  • Problem Title : Sort 0s, 1s and 2s

    Problem Title : Sort 0s, 1s and 2s

    Problem Description: Given an array A of size N containing 0s, 1s, and 2s; you need to sort the array in ascending…

  • Problem Title: Minimum number of jumps

    Problem Title: Minimum number of jumps

    Problem Statement : Given an array arr[] of size n of non-negative integers. Each array element represents the maximum…

  • Problem Title : Find all pairs with a given sum

    Problem Title : Find all pairs with a given sum

    Problem Statement Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all…

社区洞察

其他会员也浏览了