forked from SjxSubham/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path514. Freedom Trail.cpp
More file actions
32 lines (30 loc) · 840 Bytes
/
514. Freedom Trail.cpp
File metadata and controls
32 lines (30 loc) · 840 Bytes
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
class Solution {
public:
int n;
string ring,key;
vector<vector<int>> memo;
array<vector<int>,26> pos;
int dfs(int cur,int kidx) {
if (kidx==(int)key.size()) return 0;
int &res=memo[cur][kidx];
if (res != -1) return res;
res=INT_MAX/4;
int ch=key[kidx]-'a';
for (int p : pos[ch]) {
int diff=abs(cur-p);
int dist=min(diff,n-diff);
int sub=dfs(p,kidx+1);
res=min(res,dist+1+sub);
}
return res;
}
int findRotateSteps(string ring_,string key_) {
ring=ring_;
key=key_;
n=ring.size();
memo.assign(n,vector<int>(key.size(),-1));
for (int i=0; i<26; ++i) pos[i].clear();
for (int i=0; i<n; ++i) pos[ring[i]-'a'].push_back(i);
return dfs(0,0);
}
};