Anagrams | Basics of Input/Output
Problem Statement :
Solution Explained :
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.
Problem Link :
https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/anagrams-651/
Input :
- test cases,t
- two strings a and b, for each test case
Output:
Desired O/p
Constraints :
string lengths<=10000
Note :
Anagram of a word is formed by rearranging the letters of the word.
For e.g. -> For the word RAM - MAR,ARM,AMR,RMA etc. are few anagrams.
Solution :
//Code is here
#include<iostream>
using namespace std;
int main()
{
int t; cin>>t; // for test cases
while(t--)
{
string a,b; // to store two strings
cin>>a; cin>>b;
int count=0;
for(int i=0;i<a.length();i++) // iterate string-1
{
for(int j=0;j<b.length();j++) // iterare string-2
{
if(a[i]==b[j]) // delete single occurance of character
{
b[j]=0;
count++;
break;
// break to avoid multiple deletion for single character
}
}
}
int ans=(a.length()-count)+(b.length()-count);
// minus same characters from string lengths
cout<<ans<<endl;
}
}
| Code Snippet |
| Submission |
We use t to store test cases. For each test case we declare two strings a and b to store input strings. Now here we are using a logic in which we will count the total numbers of characters of string a which are common in string b. To accompany this, we use a loop of i to iterate over string a, for each character of string a, we iterate through string b, and if we found that particular character, we increment count, remove that character from string b by assigning it with 0 and break to avoid multiple character deletion ( i.e., to only delete a single character at a time). Finally our count contains the total number of characters common to both strings, we minus that number of characters from string lengths and assign it to ans variable And finally we prints ans.
This completes our Program.
Do comment your response plz.
Do comment your response plz.
FOR ANY QUERY REGARDING SOLUTION
free feel to contact
Rahul Badgujar
WhatsApp : +917775919753
Email: rahulrb15899@gmail.com
what is the work of test case t?pls explain
ReplyDeleteTest Cases simply means number of trials...here t test cases means that we need to execute our logic to count required steps for anagrams for t strings...each time accepting the same input pattern
Delete