-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSave your life
33 lines (27 loc) · 845 Bytes
/
Save your life
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
class Solution{
public:
string maxSum(string w,char x[], int b[],int n){
unordered_map<char,int>mp;
for(int i=0;i<n;i++)
mp[x[i]] = b[i];
int curr_val=(mp.count(w[0])==1) ? mp[w[0]] : (int)(w[0]), res = curr_val;
string curr="",ans;
curr+=w[0];
ans=curr;
for(int i=1;i<w.size();i++){
int temp = (mp.count(w[i])==1)? mp[w[i]] : (int)(w[i]);
if(temp+curr_val>=temp)
curr_val+=temp;
else{
curr_val = temp;
curr="";
}
curr+=w[i];
if(curr_val>res){
res= curr_val;
ans=curr;
}
}
return ans;
}
};