1
- // https://www.interviewbit.com/problems/hotel-reviews/
2
-
3
1
struct TrieNode {
4
- unordered_map< char ,TrieNode*> children ;
5
- bool is_word ;
6
- TrieNode (bool x) : is_word(x ) {}
2
+ bool isWord ;
3
+ unordered_map< char , TrieNode*> children ;
4
+ TrieNode (bool word): isWord(word ) {}
7
5
};
8
-
9
- struct ResNode {
10
- int index;
11
- int val;
6
+
7
+ struct res {
8
+ int index, score;
12
9
};
13
-
14
- bool myfunction (ResNode i,ResNode j) {
15
- bool ret = 0 ;
16
- if (i.val >j.val ){
17
- ret=1 ;
18
- }
19
- else if (i.val ==j.val ){
20
- if (i.index <j.index ){
21
- ret=1 ;
10
+
11
+ void tokenize (string good_string, vector<string> &goodWords) {
12
+ good_string += " _" ;
13
+ string temp=" " ;
14
+
15
+ for (int i=0 ; i<good_string.length (); i++) {
16
+ if (good_string[i]!=' _' ) {
17
+ temp += good_string[i];
18
+ } else {
19
+ if (temp.length ()) {
20
+ goodWords.push_back (temp);
21
+ temp = " " ;
22
+ }
22
23
}
23
24
}
24
- return ret;
25
25
}
26
-
27
- void construct_trie (string good_words, TrieNode* A){
28
- TrieNode* current = A;
29
- for (int i=0 ; i<good_words.length (); i++){
30
26
31
- if (good_words[i]==' _' ){
32
- current=A;
33
- continue ;
34
- }
35
- else {
36
- unordered_map<char ,TrieNode*>::iterator it = (current->children ).find (good_words[i]);
37
- if (it != (current->children ).end ()){
27
+ bool sortByScore (res lhs, res rhs) {
28
+ if (lhs.score > rhs.score ) return true ;
29
+ else if (lhs.score == rhs.score ) return lhs.index < rhs.index ;
30
+ return false ;
31
+ }
32
+
33
+ void constructTrie (TrieNode* A, vector<string> goodWords) {
34
+ TrieNode* current = A;
35
+
36
+ for (int i=0 ; i<goodWords.size (); i++) {
37
+ current = A;
38
+
39
+ for (int j=0 ; j<goodWords[i].length (); j++) {
40
+ auto it = (current->children ).find (goodWords[i][j]);
41
+
42
+ if (it != (current->children ).end ()) {
38
43
current = it->second ;
44
+ } else {
45
+
46
+ TrieNode* B = new TrieNode (false );
47
+ (current->children )[goodWords[i][j]] = B;
48
+ current = B;
49
+
39
50
}
40
- else {
41
- TrieNode* B = new TrieNode (0 );
42
- (current->children )[good_words[i]] = B;
43
- current=B;
44
- }
45
- if (i==good_words.length ()-1 || good_words[i+1 ]==' _' ){
46
- current->is_word = 1 ;
47
- }
51
+
52
+ if (j == goodWords[i].length ()-1 ) current->isWord = true ;
48
53
}
49
54
}
55
+
50
56
}
51
-
52
- int solveReview (TrieNode* A, string review){
53
- int result = 0 ;
54
- review = review+" _" ;
55
- vector<string> arr;
56
- int start = 0 ;
57
- for (int i=0 ; i<review.length (); i++){
58
- if (review[i]==' _' ){
59
- if (start!=i){
60
- arr.push_back (review.substr (start,i-start));
61
- }
62
- start = i+1 ;
63
- }
64
- }
65
- for (int i=0 ; i<arr.size (); i++){
66
- TrieNode* current = A;
67
- for (int j=0 ; j<arr[i].length (); j++){
68
- unordered_map<char ,TrieNode*>::iterator it = (current->children ).find (arr[i][j]);
69
- if (it != (current->children ).end ()){
70
- current = it->second ;
71
- }
72
- else {
73
- break ;
74
- }
75
- if (j==arr[i].length ()-1 ){
76
- if (current->is_word == 1 ){
77
- result++;
57
+
58
+ vector<int > sortReviews (vector<string> reviews, TrieNode* root) {
59
+ int score;
60
+ vector<res> result;
61
+ TrieNode* current;
62
+
63
+ vector<string> temp;
64
+
65
+
66
+
67
+ for (int i=0 ; i<reviews.size (); i++) {
68
+ res t;
69
+ temp.clear ();
70
+ score = 0 ;
71
+ reviews[i] += " _" ;
72
+ tokenize (reviews[i], temp);
73
+
74
+ for (int j=0 ; j<temp.size (); j++) {
75
+ current = root;
76
+ for (int k=0 ; k<temp[j].length (); k++) {
77
+
78
+ auto it = (current->children ).find (temp[j][k]);
79
+ if (it == (current->children ).end ()) {
80
+ break ;
78
81
}
82
+
83
+ current = it->second ;
84
+
85
+ if (k == temp[j].length ()-1 && current->isWord ) score++;
79
86
}
80
87
}
88
+ t.index = i;
89
+ t.score = score;
90
+ result.push_back (t);
81
91
}
82
- return result;
83
- }
84
-
85
- vector<int > Solution::solve (string good_words, vector<string> &reviews) {
86
- // Do not write main() function.
87
- // Do not read input, instead use the arguments to the function.
88
- // Do not print the output, instead return values as specified
89
- // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
90
-
91
- vector<int > result;
92
- vector<ResNode> temp_res;
93
- TrieNode* A = new TrieNode (0 );
94
- construct_trie (good_words, A);
95
-
96
- for (int i=0 ; i<reviews.size (); i++){
97
- ResNode res;
98
- res.val = solveReview (A,reviews[i]);
99
- res.index = i;
100
- temp_res.push_back (res);
101
- }
102
- sort (temp_res.begin (),temp_res.end (),myfunction);
103
- for (int i=0 ; i<temp_res.size (); i++){
104
- result.push_back (temp_res[i].index );
92
+
93
+ sort (result.begin (), result.end (), sortByScore);
94
+
95
+ vector<int > ret;
96
+
97
+ for (int i=0 ; i<result.size (); i++) {
98
+ ret.push_back (result[i].index );
105
99
}
100
+
101
+ return ret;
102
+ }
103
+
104
+ vector<int > Solution::solve (string A, vector<string> &B) {
105
+ TrieNode* root = new TrieNode (false );
106
+ vector<string> good_words;
107
+
108
+ tokenize (A, good_words);
109
+
110
+ constructTrie (root, good_words);
111
+
112
+ vector<int > result = sortReviews (B, root);
113
+
106
114
return result;
107
- }
115
+ }
0 commit comments