-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordRank.cpp
73 lines (62 loc) · 1.78 KB
/
wordRank.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
using namespace std;
bool recPermute(string strSoFar, string rest, const string & given,int & rank, unordered_set<string> & myset) {
if (rest.empty()) {
//cout<<strSoFar<<endl;
if (strSoFar == given) { cout << rank << endl; return true; }
if ( myset.find(strSoFar) == myset.end()) {
myset.insert(strSoFar);
rank += 1;
}
} else {
for (int i = 0; i<rest.length();++i) {
string rem = rest.substr(0,i)+ rest.substr(i+1);
if (recPermute(strSoFar+rest[i],rem,given,rank, myset)) return true;
}
}
return false;
}
/*
void RecPermute(string soFar, string rest)
{
if (rest.empty()) {
cout << soFar << endl;
} else {
for (int i = 0; i < rest.length(); i++) {
string remaining = rest.substr(0, i)
+ rest.substr(i+1);
RecPermute(soFar + rest[i], remaining);
}
}
*/
/*
* Complete the function below.
*/
vector < int > get_ranks(vector < string > words) {
vector<int> output;
unordered_set<string> myset;
for (int i = 0; i< words.size();++i) {
int rank = 0;
string str(words[i]);
sort(str.begin(), str.end());
recPermute("",str,words[i],rank, myset);
//cout<<rank<<endl;
output.push_back(rank);
}
return output;
}
int main(){
int rank = 0;
vector<string> words = { "bac", "aaa", "abba"};
get_ranks(words);
/*
string str("bac");
sort(str.begin(),str.end());cout<<str<<endl;
recPermute("","abc", "bac",rank);
cout<<rank<<endl;
//cout<<RecPermute("abc","");*/
}