Anagram of String | Practice | GeeksforGeeks

Given two strings S1 and S2 in lowercase, the task is to make them anagram. The only allowed operation is to remove a character from any string. Find the minimum number of characters to be deleted to make both the strings anagram. Two strings are called anagram of each other if one of them can be converted into another by rearranging its letters.
Example 1:
Input:
S1 = bcadeh
S2 = hea
Output: 3
Explanation: We need to remove b, c
and d from S1.

Example 2:
Input:
S1 = cddgk
S2 = gcd
Output: 2
Explanation: We need to remove d and
k from S1.
Your Task:
Complete the function remAnagram() which takes two strings S1, S2 as input parameter, and returns minimum characters needs to be deleted.
Expected Time Complexity: O(max(|S1|, |S2|)), where |S| = length of string S.
Expected Auxiliary Space: O(26)
Constraints:
1 <= |S1|, |S2| <= 105
0
radhikamendiratta19944 days ago
python solution
 
def remAnagram(str1,str2):
    count=0
    t1=str1[:]
    for i in range(len(str1)):
    
        if t1[i] not in str2:
            #str1=str1[:i]+str1[i+1:]
            #added above statement for debugging
            count+=1

        if t1[i] in str2:
            if str2:
                pos = str2.index(t1[i])
                str2=str2[:pos]+str2[pos+1:]

    if str2:
        count+=len(str2)
    return count
0
danish_nitdgp2 weeks ago
Basic Solution with constant space.int remAnagram(string str1, string str2){// Your code goes here   int n1 = str1.length();   int n2 = str2.length();      vector<int> v1(26,0);   vector<int> v2(26,0);   for(int i =0;i<n1;i++){       v1[str1[i]-'a']++;   }   for(int i =0;i<n2;i++){       v2[str2[i]-'a']++;   }   int count = 0;   for(int i =0;i<26;i++){       count+=abs(v1[i]-v2[i]);   }   return count;   }
0
srikavyasri09
This comment was deleted.
+1
itachinamikaze2211 month ago
JAVA
public int remAnagrams(String s,String s1)       {       HashMap<Character, Integer> map= new HashMap<>();       for(int i=0; i<s.length();i++)       {         char ch= s.charAt(i);         map.put(ch, map.getOrDefault(ch, 0)+1);       }        for(int i=0; i<s1.length();i++)       {         char ch= s1.charAt(i);                  if(map.containsKey(ch)==false)            map.put(ch,-1);         else             map.put(ch, map.get(ch)-1);        }       int count =0;       for(int i: map.values())       {           if(i<0)           count= count-i;           else           count=count+i;       }              return count;       }
+2
19bcs21821 month ago
SIMPLE JAVA APPROACH: TIME COMPLEXITY: (0.2)-
class GfG{public int remAnagrams(String s,String s1)       {           int al []= new int[256];          // int bl []= new int[256];           int index=0;           for(int i=0;i<s.length();i++){        char c = s.charAt(i);   index = (int) c;  al[index]++; } for(int j=0; j<s1.length(); j++) {  char c = s1.charAt(j);   index = (int) c;  al[index]--;                 }       int ans =0;       for(int i=0;i<256;i++){           if(al[i] !=0){               ans = ans+ Math.abs(al[i]);           }       }       return ans;       }}
+2
atulp2001np2 months ago
 int a[26]={0};
 int count=0;
 int b[26]={0};
 for(auto i: str1)
 {
     a[i-'a']++;
 }
 for(auto i: str2)
 {
     b[i-'a']++;
 }
 for(int i=0;i<26;i++)
 {
     count+=abs(b[i]-a[i]);
 }
 return count;
 
0
codeman99
This comment was deleted.
0
robingangil52 months ago
int remAnagram(string str1, string str2){// Your code goes here()){
int H1[26]={0};int H2[26]={0};for(int i=0;str1[i]!='\0';i++){   H1[str1[i]-97]++;}for(int i=0;str2[i]!='\0';i++){   H2[str2[i]-97]++;}int sum=0;for(int i=0;i<26;i++){   sum+=abs(H1[i]-H2[i]);}return sum;}
0
vikasingle2 months ago
Python
def remAnagram(str1,str2):

    #add code here

    m = len(str1)
    n  = len(str2)

    i = 0
    j = 0

    ans = 0

    str1 = sorted(str1)
    str2 = sorted(str2)

    while(i<m and j<n):

        if str1[i]==str2[j]:
            i+=1
            j+=1

        elif str1[i] < str2[j]:
            i+=1
            ans+=1

        else:
            j+=1
            ans+=1

    ans += m-i
    ans +=n-j

    return ans

0
bhushan91682730332 months ago
Simple apporach using two store arrays (C++)
int remAnagram(string str1, string str2) {
    // Your code goes here
    int count = 0;

	vector<int>store1(26, 0);
	vector<int>store2(26, 0);

	for (int i = 0; i < str1.length(); i++)	{
		store1[str1[i] - 'a']++;
	}

	for (int i = 0; i < str2.length(); i++)	{
		store2[str2[i] - 'a']++;
	}

	for (int i = 0; i < 26; i++)	{
		count += abs(store1[i] - store2[i]);
	}

	return count;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.