-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin diff.cpp
More file actions
64 lines (58 loc) · 1.67 KB
/
min diff.cpp
File metadata and controls
64 lines (58 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*Given an array of words containing n strings of the same lengths.
In one move, you can choose any position in any one string and change the letter at that position to the previous or next letter in alphabetical order. For example:
'c' can be changed to 'd' or 'b'.
'a' can only be changed to 'b'.
'z' can only be changed to 'y'.
Find the minimum difference over all
the possible pairs of the n strings.
The difference between the two strings is the minimum
number of moves required to make them equal.
Example 1:
Input:
n = 5
words[] = { "cdd", "zba","dgf","xyz","mnp"}
Output: 6
Explanation:
Among all the pairs, the minimum difference
is between the pairs ["cdd", "dgf"].
Convert 'c' to 'd' in one move.
Convert 'd' to 'g' in three moves.
Convert 'd' to 'f' in two moves.
Example 2:
Input:
n = 3
words[] = {"ab","ab","ab"}
Output: 0*/
#include<bits/stdc++.h>
using namespace std;
void minimum_difference(int n, vector<string> &words)
{
int sn=words[0].size();
int ans=INT_MAX;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int temp=0;
for(int k=0;k<sn;k++)
{
temp=temp+abs(words[i][k]-words[j][k]);
}
ans=min(ans,temp);
}
}
cout<<ans<<" ";
}
int main()
{
int n;
cout<<"\n enter array size:";
cin>> n;
vector<string> words(n);
cout<<"\n enter array elements:";
for (int i = 0; i < n; i++)
{
cin >> words[i];
}
minimum_difference( n,words) ;
}