Strings: Making Anagrams
Problem Statement :
A student is taking a cryptography class and has found anagrams to be very useful. Two strings are anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency. For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not. The student decides on an encryption scheme that involves two large strings. The encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Determine this number. Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings. Function Description Complete the makeAnagram function in the editor below. makeAnagram has the following parameter(s): string a: a string string b: another string Returns int: the minimum total characters that must be deleted Input Format The first line contains a single string, a. The second line contains a single string, b. Constraints 1 <= | a |, | b | <= 10^4 The strings a and b consist of lowercase English alphabetic letters, ascii[a-z].
Solution :
Solution in C :
In C :
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
char* a = (char *)malloc(512000 * sizeof(char));
scanf("%s",a);
char* b = (char *)malloc(512000 * sizeof(char));
scanf("%s",b);
int ind1,ind2;
int arr1[26]={0};
int arr2[26]={0};
int count=0;
for(int i=0;i<strlen(a);i++)
{
ind1=a[i]-'a';
arr1[ind1]++;
}
for(int j=0;j<strlen(b);j++)
{
ind2=b[j]-'a';
arr2[ind2]++;
}
for(int i=0;i<26;i++)
{
count+=abs(arr1[i]-arr2[i]);
}
printf("%d",count);
return 0;
}
Solution in C++ :
In C ++ :
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int number_needed(string a, string b) {
int i,c[26]={0},c1[26]={};
for(i=0;i<a.length();i++)
{
if(97<=a[i]&&a[i]<=123)
c[a[i]-97]++;
}
for(i=0;i<b.length();i++)
{
if(97<=b[i]&&b[i]<=123)
c1[b[i]-97]++;
}
int s=0;
for(i=0;i<26;i++)
{
s=s+abs(c[i]-c1[i]);
}
return (s);
}
int main(){
string a;
cin >> a;
string b;
cin >> b;
cout << number_needed(a, b) << endl;
return 0;
}
Solution in Java :
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int numberNeeded(String first, String second) {
int[] charSet = new int[256];
for(int i=0; i<first.length(); i++)
charSet[first.charAt(i)]++;
for(int i=0; i<second.length(); i++)
charSet[second.charAt(i)]--;
int numberNeeded = 0;
for(int i=0; i<256; i++)
numberNeeded += Math.abs(charSet[i]);
return numberNeeded;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String first = in.next();
String second = in.next();
System.out.println(numberNeeded(first, second));
}
}
Solution in Python :
In Python3 :
a = input().strip()
b = input().strip()
main_count = 0
total = 0
string1_set = set(a + b)
for i in range(len(string1_set)):
count = a.count(list(string1_set)[i])
count2 = b.count(list(string1_set)[i])
if count > count2:
main_count = count - count2
elif count2 > count :
main_count = count2 - count
elif count2 == count :
main_count = 0
total = total + main_count
print(total)
View More Similar Problems
Insert a Node at the head of a Linked List
Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below
View Solution →Insert a node at a specific position in a linked list
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e
View Solution →Delete a Node
Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo
View Solution →Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →