-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshift-distance-between-two-strings.cpp
110 lines (95 loc) · 3.19 KB
/
shift-distance-between-two-strings.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
long long shiftDistance(string s, string t, vector<int>& nextCost, vector<int>& previousCost) {
int n = s.length();
long long totalCost = 0;
for(int i = 0; i < n; i++) {
if(s[i] == t[i]) continue;
char from = s[i], to = t[i];
long long forwardCost = 0, backwardCost = 0;
char curr = from;
while(curr != to) {
curr++;
if(curr > 'z') curr = 'a';
forwardCost += nextCost[curr - 'a'];
}
curr = from;
while(curr != to) {
curr--;
if(curr < 'a') curr = 'z';
backwardCost += previousCost[curr - 'a'];
}
cout<<forwardCost<<" "<<backwardCost<<endl;
totalCost += min(forwardCost, backwardCost);
}
return totalCost;
}
};
void runTest(string s, string t, vector<int>& nextCost, vector<int>& previousCost, long long expected) {
Solution sol;
long long result = sol.shiftDistance(s, t, nextCost, previousCost);
cout << "Test: s = \"" << s << "\", t = \"" << t << "\"" << endl;
cout << "Expected: " << expected << ", Got: " << result << endl;
assert(result == expected);
cout << "Test Passed!\n\n";
}
int main() {
// Test Case 1: Example from the problem
{
string s = "abab";
string t = "baba";
vector<int> nextCost(26, 0);
vector<int> previousCost(26, 100);
nextCost[0] = 100; // cost for 'a'
previousCost[0] = 1; // cost for 'b'
previousCost[1] = 100;
runTest(s, t, nextCost, previousCost, 2);
}
// Test Case 2: Second example from the problem
{
string s = "leet";
string t = "code";
vector<int> nextCost(26, 1);
vector<int> previousCost(26, 1);
runTest(s, t, nextCost, previousCost, 31);
}
// Test Case 3: No change needed
{
string s = "hello";
string t = "hello";
vector<int> nextCost(26, 1);
vector<int> previousCost(26, 1);
runTest(s, t, nextCost, previousCost, 0);
}
// Test Case 4: Single character change
{
string s = "a";
string t = "z";
vector<int> nextCost(26, 2);
vector<int> previousCost(26, 1);
runTest(s, t, nextCost, previousCost, 1); // should use backward shift
}
// Test Case 5: All characters need to be changed
{
string s = "aaaa";
string t = "zzzz";
vector<int> nextCost(26, 2);
vector<int> previousCost(26, 1);
runTest(s, t, nextCost, previousCost, 4); // should use backward shift 4 times
}
// Test Case 6: Mixed costs
{
string s = "abc";
string t = "def";
vector<int> nextCost(26, 10);
vector<int> previousCost(26, 5);
for(int i = 0; i < 3; i++) {
nextCost[i] = 1; // make forward movement cheaper for first 3 letters
}
runTest(s, t, nextCost, previousCost, 3); // should use forward shifts
}
cout << "All tests passed!" << endl;
return 0;
}