📜  这至少两个字符串的前缀字符串最长的字符串

📅  最后修改于: 2021-05-17 16:50:45             🧑  作者: Mango

给定一组相同长度的字符串,我们需要找到其中至少两个字符串的前缀字符串中最长的字符串的长度。
例子:

Input:  ["abcde", "abcsd", "bcsdf", "abcda", "abced"]
Output: 4
Explanation:  
Longest prefix string is "abcd".

Input:  ["pqrstq", "pwxyza", "abcdef", "pqrstu"]
Output: 5

方法:

  • 从第0个位置开始,迭代每个字符,并检查该字符是否在当前位置的字符串中的至少两个中出现。
  • 如果发生,则递归调用下一个位置。除此以外,
  • 通过使用Current_position – 1取最大值来更新最大值。
  • 最后,返回最大值。
C++
// C++ program to find longest
// string which is prefix string
// of at least two strings
#include
using namespace std;
int max1=0;
  
// Function to find Max length 
// of the prefix
int MaxLength(vector v, int i, 
                                int m)
{
    // Base case
    if(i>=m)
    {
        return m-1;
    }
      
    // Iterating over all the alphabets
    for(int k = 0; k < 26; k++)
    {
        char c = 'a' + k;
        vector v1;
          
        // Checking if char exists in
        // current string or not
        for(int j = 0; j < v.size(); j++)
        {
            if(v[j][i] == c)
            {
                v1.push_back(v[j]);
            }
        }
          
        // If atleast 2 string have 
        // that character
        if(v1.size()>=2)
        {
           // Recursive call to i+1 
           max1=max(max1,
                    MaxLength(v1, i+1, m));
        }
        else
        {
            max1=max(max1, i - 1);
        }
    }
    return max1;
}
  
// Driver code
int main()
{
  // Initialising strings  
  string s1, s2, s3, s4, s5;
    
  s1 = "abcde";
  s2 = "abcsd";
  s3 = "bcsdf";
  s4 = "abcda";
  s5 = "abced";
       
  vector v;
      
  // push strings into vectors.
  v.push_back(s1);
  v.push_back(s2);
  v.push_back(s3);
  v.push_back(s4);
  v.push_back(s5);
      
  int m = v[0].size();
      
  cout<


Java
// Java program to find longest
// String which is prefix String
// of at least two Strings
import java.util.*;
class GFG{
static int max1 = 0;
  
// Function to find Max length 
// of the prefix
static int MaxLength(Vector v, 
                     int i, int m)
{
    // Base case
    if(i>=m)
    {
        return m-1;
    }
      
    // Iterating over all the alphabets
    for(int k = 0; k < 26; k++)
    {
        char c = (char)('a' + k);
        Vector v1 = new Vector();
          
        // Checking if char exists in
        // current String or not
        for(int j = 0; j < v.size(); j++)
        {
            if(v.get(j).charAt(i) == c)
            {
                v1.add(v.get(j));
            }
        }
          
        // If atleast 2 String have 
        // that character
        if(v1.size() >= 2)
        {
           // Recursive call to i+1 
           max1=Math.max(max1, 
                         MaxLength(v1, i + 1, m));
        }
        else
        {
            max1=Math.max(max1, i - 1);
        }
    }
    return max1;
}
  
// Driver code
public static void main(String[] args)
{
  // Initialising Strings  
  String s1, s2, s3, s4, s5;  
  s1 = "abcde";
  s2 = "abcsd";
  s3 = "bcsdf";
  s4 = "abcda";
  s5 = "abced";
       
  Vector v = new Vector();
      
  // push Strings into vectors.
  v.add(s1);
  v.add(s2);
  v.add(s3);
  v.add(s4);
  v.add(s5);
      
  int m = v.get(0).length();    
  System.out.print(MaxLength(v, 0, m) + 1); 
}
} 
  
// This code is contributed by shikhasingrajput


Python3
# Python3 program to find longest
# string which is prefix string
# of at least two strings
  
max1 = 0
  
# Function to find max length 
# of the prefix
def MaxLength(v, i, m):
      
    global max1
      
    # Base case
    if(i >= m):
        return m - 1
          
    # Iterating over all the alphabets
    for k in range(26):
        c = chr(ord('a') + k)
        v1 = []
          
        # Checking if char exists in
        # current string or not
        for j in range(len(v)):
            if(v[j][i] == c):
                v1.append(v[j])
          
        # If atleast 2 string have 
        # that character
        if(len(v1) >= 2):
              
            # Recursive call to i+1
            max1 = max(max1, MaxLength(v1, i + 1, m))
        else:
            max1 = max(max1, i - 1)
              
    return max1
  
# Driver code
if __name__ == '__main__':
      
    # Initialising strings 
    s1 = "abcde"
    s2 = "abcsd"
    s3 = "bcsdf"
    s4 = "abcda"
    s5 = "abced"
    v = []
  
    # Push strings into vectors.
    v.append(s1)
    v.append(s2)
    v.append(s3)
    v.append(s4)
    v.append(s5)
      
    m = len(v[0])
      
    print(MaxLength(v, 0, m) + 1)
  
# This code is contributed by BhupendraSingh


C#
// C# program to find longest
// String which is prefix String
// of at least two Strings
using System;
using System.Collections.Generic;
  
class GFG{
      
static int max1 = 0;
  
// Function to find Max length 
// of the prefix
static int MaxLength(List v, 
                     int i, int m)
{
      
    // Base case
    if (i >= m)
    {
        return m - 1;
    }
      
    // Iterating over all the alphabets
    for(int k = 0; k < 26; k++)
    {
        char c = (char)('a' + k);
        List v1 = new List();
          
        // Checking if char exists in
        // current String or not
        for(int j = 0; j < v.Count; j++)
        {
            if (v[j][i] == c)
            {
                v1.Add(v[j]);
            }
        }
          
        // If atleast 2 String have 
        // that character
        if (v1.Count >= 2)
        {
              
            // Recursive call to i+1 
            max1 = Math.Max(max1, 
                            MaxLength(v1, i + 1, m));
        }
        else
        {
            max1 = Math.Max(max1, i - 1);
        }
    }
    return max1;
}
  
// Driver code
public static void Main(String[] args)
{
      
    // Initialising Strings 
    String s1, s2, s3, s4, s5; 
    s1 = "abcde";
    s2 = "abcsd";
    s3 = "bcsdf";
    s4 = "abcda";
    s5 = "abced";
          
    List v = new List();
          
    // push Strings into vectors.
    v.Add(s1);
    v.Add(s2);
    v.Add(s3);
    v.Add(s4);
    v.Add(s5);
          
    int m = v[0].Length; 
      
    Console.Write(MaxLength(v, 0, m) + 1); 
}
} 
  
// This code is contributed by Amit Katiyar


输出:
4