Given two first names of two persons, please calculate name_proximity_score. Name proximity score is calculated as follows:
. If a letter occurs in the same position in both names, the score increments by two.
• If a letter occurs in both names, but in different positions, the score is incremented by 1.
• Once a letter in a particular position is used in any of the names, it cannot be considered again. However, if the same letter occurs in an
used.
Input: The first line contains the first name & the second line contains the second name
Output: Only an integer value, showing the name_proximity_score
Sample test cases :
Input:
Amitabh
Ajitabh
Output:
12
Input:
Ryaan
Nagesh
Output:
2.
Answers
Answer:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1, s2;
cin >> s1;
cin >> s2;
unordered_set<int> myset;
int res;
res = 0;
for(int i = 0; i < s1.size() and i < s2.size(); i++)
{
if(myset.find(i) == myset.end() and s1[i] == s2[i])
{
myset.insert(i);
res += 2;
}
}
unordered_map<char, int> umap;
for(int i = 0; i < s1.size(); i++)
{
umap[s1[i]] += 1;
}
for(int i = 0; i < s2.size(); i++) {
if(umap.count(s2[i])) {
if(myset.find(i) != myset.end()) res += (umap[s2[i]] - 1);
else res = res + umap[s2[i]];
}
}
cout<<res<<endl;
}
Program:
#include <stdio.h>
void strchng(char *str);
int main()
{
int count=0;
char fn1[1000],fn2[1000];
scanf("%s",fn1);
scanf("%s",fn2);
strchng(fn1);
strchng(fn2);
for(int i=0; fn1[i]!='\0'; i++)
{
for(int j=0;fn2[j]!='\0'; j++)
{
if(fn2[i]!='0' && fn1[i]==fn2[j])
{
if(i==j)
count=count+2;
else
count=count+1;
fn2[j]='0';
}
}
}
printf("%d",count);
return 0;
}
void strchng(char *str)
{
int k=0;
while(str[k]!='\0')
{
if(str[k]>='A' && str[k]<='Z'){
str[k]=str[k]+32;
}
++k;
}
}
Test cases:
Input 1:
Amitabh
Ajitabh
Output 1:
12
Input 2:
Ryaan
Nagesh
Output 2:
2
Learn more:
1. Write a program to check whether it is a Lead number or not in java
https://brainly.in/question/15644815
2. Write a java program to input name and mobile numbers of all employees of any office. Using "Linear Search", search array of phone numbers for a given "mobile number" and print name of employee if found, otherwise print a suitable message.
brainly.in/question/18217872