[InterviewBit] Hotel Reviews

Hotel Reviews

  • Time :
  • Space :
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
void modify(string& s) {
for(int i = 0; i < s.length(); i++) if(s[i] == '_') s[i] = ' ';
}

vector<int> Solution::solve(string A, vector<string> &B) {
modify(A);
stringstream ss;
string now;
ss<<A;
unordered_set<string> good;
while(ss>>now) good.insert(now);
vector<pair<int, int>> S;
for(int i = 0; i < B.size(); i++) {
modify(B[i]);
ss.clear();
ss<<B[i];
int score = 0;
while(ss>>now) if(good.count(now)) score += 1;
S.push_back({score,i});
}
sort(begin(S), end(S), [](auto a, auto b) {
if(a.first == b.first) return a.second < b.second;
return a.first > b.first;
});
vector<int> res;
for(auto [_, idx] : S) res.push_back(idx);
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/25/PS/interviewbit/hotel-reviews/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.