📌  相关文章
📜  计算字符串中子序列的最大出现次数,以使子序列中的索引位于AP中

📅  最后修改于: 2021-04-24 15:32:03             🧑  作者: Mango

给定字符串S ,任务是计算给定字符串中子序列的最大出现次数,以使子序列字符的索引处于算术级数中。

例子:

方法:问题中的关键发现是,字符串是否有两个字符的总和大于任何单个字符,则这些字符将在字符串形成最大出现子序列,而该字符处于算术级数,原因是每两个整数将始终形成算术级数。下面是步骤说明:

  • 遍历字符串和计数字符串的字符的频率。那是在考虑长度为1的子序列。
  • 遍历字符串,并选择该字符串的每两个可能的字符和增加字符串的子序列的频率。
  • 最后,从长度1和2找到子序列的最大频率。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
#include 
 
using namespace std;
 
// Function to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
int maximumOccurrence(string s)
{
    int n = s.length();
 
    // Frequencies of subsequence
    map freq;
 
    // Loop to find the frequencies
    // of subsequence of length 1
    for (int i = 0; i < n; i++) {
        string temp = "";
        temp += s[i];
        freq[temp]++;
    }
     
    // Loop to find the frequencies
    // subsequence of length 2
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            string temp = "";
            temp += s[i];
            temp += s[j];
            freq[temp]++;
        }
    }
 
    int answer = INT_MIN;
 
    // Finding maximum frequency
    for (auto it : freq)
        answer = max(answer, it.second);
    return answer;
}
 
// Driver Code
int main()
{
    string s = "xxxyy";
 
    cout << maximumOccurrence(s);
    return 0;
}


Java
// Java implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
import java.util.*;
 
class GFG
{
    // Function to find the
    // maximum occurence of the subsequence
    // such that the indices of characters
    // are in arithmetic progression
    static int maximumOccurrence(String s)
    {
        int n = s.length();
      
        // Frequencies of subsequence
        HashMap freq = new HashMap();
        int i, j;
 
        // Loop to find the frequencies
        // of subsequence of length 1
        for ( i = 0; i < n; i++) {
            String temp = "";
            temp += s.charAt(i);
            if (freq.containsKey(temp)){
                freq.put(temp,freq.get(temp)+1);
            }
            else{
                freq.put(temp, 1);
            }
        }
          
        // Loop to find the frequencies
        // subsequence of length 2
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < n; j++) {
                String temp = "";
                temp += s.charAt(i);
                temp += s.charAt(j);
                if(freq.containsKey(temp))
                    freq.put(temp,freq.get(temp)+1);
                else
                    freq.put(temp,1);
            }
        }
        int answer = Integer.MIN_VALUE;
      
        // Finding maximum frequency
        for (int it : freq.values())
            answer = Math.max(answer, it);
        return answer;
    }
      
    // Driver Code
    public static void main(String []args)
    {
        String s = "xxxyy";
      
        System.out.print(maximumOccurrence(s));
    }
}
 
// This code is contributed by chitranayal


Python3
# Python3 implementation to find the
# maximum occurence of the subsequence
# such that the indices of characters
# are in arithmetic progression
 
# Function to find the
# maximum occurence of the subsequence
# such that the indices of characters
# are in arithmetic progression
def maximumOccurrence(s):
    n = len(s)
 
    # Frequencies of subsequence
    freq = {}
 
    # Loop to find the frequencies
    # of subsequence of length 1
    for i in s:
        temp = ""
        temp += i
        freq[temp] = freq.get(temp, 0) + 1
 
    # Loop to find the frequencies
    # subsequence of length 2
    for i in range(n):
        for j in range(i + 1, n):
            temp = ""
            temp += s[i]
            temp += s[j]
            freq[temp] = freq.get(temp, 0) + 1
 
    answer = -10**9
 
    # Finding maximum frequency
    for it in freq:
        answer = max(answer, freq[it])
    return answer
 
# Driver Code
if __name__ == '__main__':
    s = "xxxyy"
 
    print(maximumOccurrence(s))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
static int maximumOccurrence(string s)
{
  int n = s.Length;
 
  // Frequencies of subsequence
  Dictionary freq = new Dictionary();
  int i, j;
 
  // Loop to find the frequencies
  // of subsequence of length 1
  for ( i = 0; i < n; i++)
  {
    string temp = "";
    temp += s[i];
    if (freq.ContainsKey(temp))
    {
      freq[temp]++;
    }
    else
    {
      freq[temp] = 1;
    }
  }
 
  // Loop to find the frequencies
  // subsequence of length 2
  for (i = 0; i < n; i++)
  {
    for (j = i + 1; j < n; j++)
    {
      string temp = "";
      temp += s[i];
      temp += s[j];
      if(freq.ContainsKey(temp))
        freq[temp]++;
      else
        freq[temp] = 1;
    }
  }
  int answer =int.MinValue;
 
  // Finding maximum frequency
  foreach(KeyValuePair it in freq)
    answer = Math.Max(answer, it.Value);
  return answer;
}
       
// Driver Code
public static void Main(string []args)
{
  string s = "xxxyy";
  Console.Write(maximumOccurrence(s));
}
}
 
// This code is contributed by Rutvik_56


C++
// C++ implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
 
#include 
 
using namespace std;
 
// Function to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
int maximumOccurrence(string s)
{
    int n = s.length();
 
    // Frequency for characters
    int freq[26] = { 0 };
    int dp[26][26] = { 0 };
     
    // Loop to count the occurence
    // of ith character before jth
    // character in the given string
    for (int i = 0; i < n; i++) {
        int c = (s[i] - 'a');
 
        for (int j = 0; j < 26; j++)
            dp[j] += freq[j];
 
        // Increase the frequency
        // of s[i] or c of string
        freq++;
    }
 
    int answer = INT_MIN;
     
    // Maximum occurence of subsequence
    // of length 1 in given string
    for (int i = 0; i < 26; i++)
        answer = max(answer, freq[i]);
         
    // Maximum occurence of subsequence
    // of length 2 in given string
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            answer = max(answer, dp[i][j]);
        }
    }
 
    return answer;
}
 
// Driver Code
int main()
{
    string s = "xxxyy";
 
    cout << maximumOccurrence(s);
    return 0;
}


Java
// Java implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
 
 
class GFG{
  
// Function to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
static int maximumOccurrence(String s)
{
    int n = s.length();
  
    // Frequency for characters
    int freq[] = new int[26];
    int dp[][] = new int[26][26];
      
    // Loop to count the occurence
    // of ith character before jth
    // character in the given String
    for (int i = 0; i < n; i++) {
        int c = (s.charAt(i) - 'a');
  
        for (int j = 0; j < 26; j++)
            dp[j] += freq[j];
  
        // Increase the frequency
        // of s[i] or c of String
        freq++;
    }
  
    int answer = Integer.MIN_VALUE;
      
    // Maximum occurence of subsequence
    // of length 1 in given String
    for (int i = 0; i < 26; i++)
        answer = Math.max(answer, freq[i]);
          
    // Maximum occurence of subsequence
    // of length 2 in given String
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            answer = Math.max(answer, dp[i][j]);
        }
    }
  
    return answer;
}
  
// Driver Code
public static void main(String[] args)
{
    String s = "xxxyy";
  
    System.out.print(maximumOccurrence(s));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the
# maximum occurence of the subsequence
# such that the indices of characters
# are in arithmetic progression
import sys
 
# Function to find the maximum occurence
# of the subsequence such that the
# indices of characters are in
# arithmetic progression
def maximumOccurrence(s):
     
    n = len(s)
 
    # Frequency for characters
    freq = [0] * (26)
 
    dp = [[0 for i in range(26)]
             for j in range(26)]
 
    # Loop to count the occurence
    # of ith character before jth
    # character in the given String
    for i in range(n):
        c = (ord(s[i]) - ord('a'))
 
        for j in range(26):
            dp[j] += freq[j]
 
        # Increase the frequency
        # of s[i] or c of String
        freq += 1
 
    answer = -sys.maxsize
 
    # Maximum occurence of subsequence
    # of length 1 in given String
    for i in range(26):
        answer = max(answer, freq[i])
 
    # Maximum occurence of subsequence
    # of length 2 in given String
    for i in range(26):
        for j in range(26):
            answer = max(answer, dp[i][j])
 
    return answer
 
# Driver Code
if __name__ == '__main__':
     
    s = "xxxyy"
 
    print(maximumOccurrence(s))
 
# This code is contributed by Princi Singh


C#
// C# implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
using System;
class GFG{
  
// Function to find the maximum
// occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
static int maximumOccurrence(string s)
{
    int n = s.Length;
      
    // Frequency for characters
    int []freq = new int[26];
    int [,]dp = new int[26, 26];
          
    // Loop to count the occurence
    // of ith character before jth
    // character in the given String
    for(int i = 0; i < n; i++)
    {
       int x = (s[i] - 'a');
        
       for(int j = 0; j < 26; j++)
          dp[x, j] += freq[j];
           
       // Increase the frequency
       // of s[i] or c of String
       freq[x]++;
    }
      
    int answer = int.MinValue;
          
    // Maximum occurence of subsequence
    // of length 1 in given String
    for(int i = 0; i < 26; i++)
       answer = Math.Max(answer, freq[i]);
              
    // Maximum occurence of subsequence
    // of length 2 in given String
    for(int i = 0; i < 26; i++)
    {
       for(int j = 0; j < 26; j++)
       {
          answer = Math.Max(answer, dp[i, j]);
       }
    }
    return answer;
}
      
// Driver Code
public static void Main(string[] args)
{
    string s = "xxxyy";
      
    Console.Write(maximumOccurrence(s));
}
}
 
// This code is contributed by Yash_R


输出:
6




时间复杂度: O(N 2 )

高效的方法:想法是使用动态编程范例来计算字符串中长度为1和2的子序列的频率。下面是步骤说明:

  • 计算在频阵列字符串的字符的频率。
  • 对于长度为2的字符串,DP状态为
dp[i][j] = Total number of times ith
  character occured before jth character.


下面是上述方法的实现:

C++

// C++ implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
 
#include 
 
using namespace std;
 
// Function to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
int maximumOccurrence(string s)
{
    int n = s.length();
 
    // Frequency for characters
    int freq[26] = { 0 };
    int dp[26][26] = { 0 };
     
    // Loop to count the occurence
    // of ith character before jth
    // character in the given string
    for (int i = 0; i < n; i++) {
        int c = (s[i] - 'a');
 
        for (int j = 0; j < 26; j++)
            dp[j] += freq[j];
 
        // Increase the frequency
        // of s[i] or c of string
        freq++;
    }
 
    int answer = INT_MIN;
     
    // Maximum occurence of subsequence
    // of length 1 in given string
    for (int i = 0; i < 26; i++)
        answer = max(answer, freq[i]);
         
    // Maximum occurence of subsequence
    // of length 2 in given string
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            answer = max(answer, dp[i][j]);
        }
    }
 
    return answer;
}
 
// Driver Code
int main()
{
    string s = "xxxyy";
 
    cout << maximumOccurrence(s);
    return 0;
}

Java

// Java implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
 
 
class GFG{
  
// Function to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
static int maximumOccurrence(String s)
{
    int n = s.length();
  
    // Frequency for characters
    int freq[] = new int[26];
    int dp[][] = new int[26][26];
      
    // Loop to count the occurence
    // of ith character before jth
    // character in the given String
    for (int i = 0; i < n; i++) {
        int c = (s.charAt(i) - 'a');
  
        for (int j = 0; j < 26; j++)
            dp[j] += freq[j];
  
        // Increase the frequency
        // of s[i] or c of String
        freq++;
    }
  
    int answer = Integer.MIN_VALUE;
      
    // Maximum occurence of subsequence
    // of length 1 in given String
    for (int i = 0; i < 26; i++)
        answer = Math.max(answer, freq[i]);
          
    // Maximum occurence of subsequence
    // of length 2 in given String
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            answer = Math.max(answer, dp[i][j]);
        }
    }
  
    return answer;
}
  
// Driver Code
public static void main(String[] args)
{
    String s = "xxxyy";
  
    System.out.print(maximumOccurrence(s));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation to find the
# maximum occurence of the subsequence
# such that the indices of characters
# are in arithmetic progression
import sys
 
# Function to find the maximum occurence
# of the subsequence such that the
# indices of characters are in
# arithmetic progression
def maximumOccurrence(s):
     
    n = len(s)
 
    # Frequency for characters
    freq = [0] * (26)
 
    dp = [[0 for i in range(26)]
             for j in range(26)]
 
    # Loop to count the occurence
    # of ith character before jth
    # character in the given String
    for i in range(n):
        c = (ord(s[i]) - ord('a'))
 
        for j in range(26):
            dp[j] += freq[j]
 
        # Increase the frequency
        # of s[i] or c of String
        freq += 1
 
    answer = -sys.maxsize
 
    # Maximum occurence of subsequence
    # of length 1 in given String
    for i in range(26):
        answer = max(answer, freq[i])
 
    # Maximum occurence of subsequence
    # of length 2 in given String
    for i in range(26):
        for j in range(26):
            answer = max(answer, dp[i][j])
 
    return answer
 
# Driver Code
if __name__ == '__main__':
     
    s = "xxxyy"
 
    print(maximumOccurrence(s))
 
# This code is contributed by Princi Singh

C#

// C# implementation to find the
// maximum occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
using System;
class GFG{
  
// Function to find the maximum
// occurence of the subsequence
// such that the indices of characters
// are in arithmetic progression
static int maximumOccurrence(string s)
{
    int n = s.Length;
      
    // Frequency for characters
    int []freq = new int[26];
    int [,]dp = new int[26, 26];
          
    // Loop to count the occurence
    // of ith character before jth
    // character in the given String
    for(int i = 0; i < n; i++)
    {
       int x = (s[i] - 'a');
        
       for(int j = 0; j < 26; j++)
          dp[x, j] += freq[j];
           
       // Increase the frequency
       // of s[i] or c of String
       freq[x]++;
    }
      
    int answer = int.MinValue;
          
    // Maximum occurence of subsequence
    // of length 1 in given String
    for(int i = 0; i < 26; i++)
       answer = Math.Max(answer, freq[i]);
              
    // Maximum occurence of subsequence
    // of length 2 in given String
    for(int i = 0; i < 26; i++)
    {
       for(int j = 0; j < 26; j++)
       {
          answer = Math.Max(answer, dp[i, j]);
       }
    }
    return answer;
}
      
// Driver Code
public static void Main(string[] args)
{
    string s = "xxxyy";
      
    Console.Write(maximumOccurrence(s));
}
}
 
// This code is contributed by Yash_R
输出:
6




时间复杂度: O(26 * N)