Trends

Find length of the longest non-intersecting anagram Subsequence

Trending 1 year ago
beritaja.com

Improve Article

Save Article

Improve Article

Save Article

Given a drawstring S of magnitude N, find nan magnitude of nan 2 longest non-intersecting subsequences successful S that are anagrams of each other.

Input: S = “aaababcd”
Output: 3
Explanation: Index of characters successful nan 2 subsequences are:

  • {0, 1, 3} = {a, a, b} 
  • {2, 4, 5} = {a, a, b} 

The supra 2 subsequences of S are anagrams.

  • Frequency of ‘a’ = 4, truthful 2 ‘a’s tin beryllium utilized successful some nan anagrams.
  • Frequency of ‘b’ = 2, truthful 1 ‘a’ tin beryllium utilized successful some nan anagrams.

Hence 2 + 1 = 3 is nan magnitude of 2 longest subsequence successful S that are anagrams of each other.

Input: S = “geeksforgeeks”
Output: 5
Explanation: The 2 longest subsequences that are anagrams of 1 different are “geeks”(0, 3) and “geeks”(8, 12), each of magnitude 5.

Approach: To lick nan problem travel nan beneath idea:

The attack calculates nan maximum magnitude of a subsequence of anagrams by dividing each characteristic wave by 2 and taking nan floor. This is because each characteristic tin look astatine astir 2 times successful a subsequence of anagrams. For example, if nan wave of a characteristic is 3, we tin usage 2 of those successful a subsequence of anagrams. Hence, we return nan level of half of its wave to get nan maximum number of times it tin beryllium used. Adding nan consequence for each characteristic gives america nan last reply which is nan magnitude of nan longest subsequence of anagrams.

Below are nan steps for nan supra approach:

  • Initialize an array count[] to shop nan wave of each characteristic successful nan drawstring S.
  • Then, we loop done each characteristic successful nan drawstring S and count nan wave of each character. 
    • If a characteristic is not successful nan count[] array, we group its wave to 1. 
    • If a characteristic already exists successful nan count[] array, we increment its wave by 1.
  • Iterate nan array count[] and disagreement each worth i.e nan wave of each characteristic by 2 and return nan level worth and adhd nan adaptable sum to get nan maximum magnitude of nan 2 longest subsequences of S that are anagrams of 1 another.

Below is nan implementation for nan supra approach:

C++

// Program to find nan magnitude of nan two // longest subsequences successful nan drawstring that // are anagrams of each other #include <bits/stdc++.h using namespace std; int maxLengthOfAnagramSubsequence(string s) { // Count nan wave of each // characteristic successful nan string int count[26] = { 0 }; for (int one = 0; one < s.length(); i++) count[s[i] - 'a']++; // Calculate nan sum of wave of // each characteristic divided by 2 Round // down to nan nearest integer int sum = 0; for (int one = 0; one < 26; i++) sum += count[i] / 2; // Return nan sum arsenic nan answer return sum; } // Drivers code int main() { drawstring s = "aabcdabcd"; // Function call cout << maxLengthOfAnagramSubsequence(s) << endl; return 0; }

Java

// Program to find nan magnitude of nan 2 longest subsequences successful nan drawstring that are anagrams of each other import java.util.HashMap; public people GFG { nationalist fixed int longestAnagramSubsequence(String S) { int maxLength = 0; HashMap<Character, Integer> charFrequency = caller HashMap<>(); // Count nan wave of each characteristic successful the // string for (int one = 0; one < S.length(); i++) { char c = S.charAt(i); charFrequency.put( c, charFrequency.getOrDefault(c, 0) + 1); } // Calculate nan sum of wave of each character // divided by 2 Round down to nan nearest integer for (int worth : charFrequency.values()) { maxLength += worth / 2; } // Return nan sum arsenic nan answer return maxLength; } nationalist fixed void main(String[] args) { String S1 = "aaababcd"; System.out.println( "The magnitude of nan 2 longest subsequences of " + S1 + " that are anagrams of 1 another: " + longestAnagramSubsequence(S1)); } }

Output

The magnitude of nan 2 longest subsequences of aaababcd that are anagrams of 1 another: 3

Time Complexity: O(N), wherever N is nan magnitude of nan string.
Auxiliary Space: O(1)

Editor: Naga



Read other contents from Beritaja.com at
More Source



close