📌  相关文章
📜  频率最高的字符串最长的前缀

📅  最后修改于: 2021-05-05 01:12:50             🧑  作者: Mango

给定一个字符串,找到具有最高频率的前缀。如果两个前缀的频率相同,则应考虑使用最大长度的前缀。

例子:

Input : str = "abc" 
Output : abc
Each prefix has same frequency(one) and the 
prefix with maximum length is "abc".

Input : str = "abcab"
Output : ab
Both prefix "a" and "ab" occur two times and the 
prefix with maximum length is "ab".

我们的想法是观察到给定的字符串的每个前缀将包含在它的字符串和单独的第一个字符的第一个字符也是给定的字符串的前缀。因此,出现次数最高的前缀是第一个字符。现在的任务仍然是最大化最高频率前缀的长度。

方法 :

  1. 取一个向量,该向量将存储字符串的第一个元素的索引。
  2. 如果第一个元素仅出现一次,则最长的前缀将是整个字符串。
  3. 否则,循环直到第一个元素的第二次出现,并在每个存储的索引之后检查一个字母。
  4. 如果没有不匹配,我们将继续前进,否则我们将停止。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to find Longest prefix string with the 
// highest frequency
void prefix(string str)
{
    int k = 1, j;
    int n = str.length();
  
    vector g;
    int flag = 0;
  
    // storing all indices where first element is found
    for (int i = 1; i < n; i++) {
        if (str[i] == str[0]) {
            g.push_back(i);
            flag = 1;
        }
    }
  
    // if the first letter in the string does not occur 
    // again  then answer will be the whole string
    if (flag == 0) {
        cout << str << endl;
    }
    else {
        int len = g.size();
  
        // loop till second appearance of the first element
        while (k < g[0]) {
  
            int cnt = 0;
            for (j = 0; j < len; j++) {
  
                // check one letter after every stored index
                if (str[g[j] + k] == str[k]) {
                    cnt++;
                }
            }
  
            // If there is no mismatch we move forward
            if (cnt == len) {
                k++;
            }
            // otherwise we stop
            else {
                break;
            }
        }
  
        for (int i = 0; i < k; i++) {
            cout << str[i];
        }
  
        cout << endl;
    }
}
  
// Driver Code
int main()
{
    string str = "abcab";
    prefix(str);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
  
class GFG
{
  
    // Function to find Longest prefix string with the 
    // highest frequency
    static void prefix(char[] str) 
    {
        int k = 1, j;
        int n = str.length;
  
        Vector g = new Vector<>();
        int flag = 0;
  
        // storing all indices where first element is found
        for (int i = 1; i < n; i++)
        {
            if (str[i] == str[0])
            {
                g.add(i);
                flag = 1;
            }
        }
  
        // if the first letter in the string does not occur 
        // again then answer will be the whole string
        if (flag == 0) 
        {
            System.out.println(String.valueOf(str));
        }
        else
        {
            int len = g.size();
  
            // loop till second appearance of the first element
            while (k < g.get(0)) 
            {
  
                int cnt = 0;
                for (j = 0; j < len; j++) 
                {
  
                    // check one letter after every stored index
                    if ((g.get(j) + k) < n && 
                        str[g.get(j) + k] == str[k])
                    {
                        cnt++;
                    }
                }
  
                // If there is no mismatch we move forward
                if (cnt == len)
                {
                    k++;
                } 
                // otherwise we stop
                else 
                {
                    break;
                }
            }
  
            for (int i = 0; i < k; i++) 
            {
                System.out.print(str[i]);
            }
  
            System.out.println();
        }
    }
  
    // Driver Code
    public static void main(String args[])
    {
        String str = "abcab";
        prefix(str.toCharArray());
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the above approach 
  
# Function to find Longest prefix string with the 
# highest frequency 
def prefix(string) :
  
    k = 1; 
    n = len(string); 
  
    g = []; 
    flag = 0; 
  
    # storing all indices where first element is found 
    for i in range(1, n) :
        if (string[i] == string[0]) :
            g.append(i); 
            flag = 1; 
  
    # if the first letter in the string does not occur 
    # again then answer will be the whole string 
    if (flag == 0) :
        print(string); 
       
    else :
        length = len(g);
          
        # loop till second appearance of the first element
        while (k < g[0]) :
            cnt = 0;
              
            for j in range(length) :
                  
                # check one letter after every stored index
                if (string[g[j] + k] == string[k]) :
                    cnt += 1;
                      
            # If there is no mismatch we move forward
            if (cnt == len) :
                k += 1;
             
            # otherwise we stop
            else :
                break;
         
        for i in range(k+1) :
            print(string[i],end="");
         
        print() 
  
# Driver Code 
if __name__ == "__main__" : 
  
    string = "abcab"; 
    prefix(string); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic; 
  
class GFG
{
  
    // Function to find Longest prefix string with the 
    // highest frequency
    static void prefix(char[] str) 
    {
        int k = 1, j;
        int n = str.Length;
  
        List g = new List();
        int flag = 0;
  
        // storing all indices where first element is found
        for (int i = 1; i < n; i++)
        {
            if (str[i] == str[0])
            {
                g.Add(i);
                flag = 1;
            }
        }
  
        // if the first letter in the string does not occur 
        // again then answer will be the whole string
        if (flag == 0) 
        {
            Console.WriteLine(String.Join("",str));
        }
        else
        {
            int len = g.Count;
  
            // loop till second appearance of the first element
            while (k < g[0]) 
            {
  
                int cnt = 0;
                for (j = 0; j < len; j++) 
                {
  
                    // check one letter after every stored index
                    if ((g[j] + k) < n && 
                        str[g[j] + k] == str[k])
                    {
                        cnt++;
                    }
                }
  
                // If there is no mismatch we move forward
                if (cnt == len)
                {
                    k++;
                } 
                // otherwise we stop
                else
                {
                    break;
                }
            }
  
            for (int i = 0; i < k; i++) 
            {
                Console.Write(str[i]);
            }
  
            Console.WriteLine();
        }
    }
  
    // Driver Code
    public static void Main()
    {
        String str = "abcab";
        prefix(str.ToCharArray());
    }
}
  
// This code contributed by Rajput-Ji


输出:
ab

时间复杂度: O(N)