class Solution {
public:
int findTime(string S1, string S2) {
// code here
int n=S1.size();
int m=S2.size();
unordered_map<char,int>mp;
for(int i=0;i<n;i++)
{
mp[S1[i]]=i;
}
int ans=0;
int temp=0;
for(int j=0;j<m;j++)
{
int b=mp[S2[j]];
ans+=abs(b-temp);
temp=b;
}
return ans;
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter