-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaypal.cpp
More file actions
159 lines (139 loc) · 3.33 KB
/
paypal.cpp
File metadata and controls
159 lines (139 loc) · 3.33 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<iostream>
#include<fstream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include<utility>
#include<unordered_map>
#include<set>
#include<map>
#include<unordered_set>
#include<string>
#include<limits.h>
using namespace std;
const int mod=1e9+7;
/*
This question was asked in paypal interview , you are given a list of commonly used password and
a list of passwords , now you have to classify the passwords as weak or strong , the conditions are :
Condition for a password to be weak is :
1. password is less than 6 characters
2. password has all CAPITAL characters
3. password has all small characters
4. password has all digits
5. any substring of password should not be in commonly used passwords
*/
vector<string> generateSubsets(string s)
{
//This function generates subsets [Note : we are not using it anywhere]
int n=s.length();
long long int bit=1<<n;
vector<string> ans;
for(int i=0;i<=bit;i++)
{
string temp="";
int ci=i;
int j=0;
while(ci>0)
{
if(ci&1)
temp.push_back(s[j]);
j++;
ci=ci>>1;
}
ans.push_back(temp);
}
return ans;
}
vector<string> getSubstrings(string s) {
vector<string> substrings;
for (int i = 0; i < s.length(); i++) {
for (int j = 1; j <= s.length() - i; j++) {
string substring = s.substr(i, j);
substrings.push_back(substring);
}
}
return substrings;
}
vector<string> getPasswordStrength(vector<string> passwords,vector<string> common_words)
{
sort(common_words.begin(),common_words.end());
vector<string> ans;
for(auto &password:passwords)
{
bool flag=false; //by default strong
if(password.size()<6)
{
flag=true;
}
bool flag_all_long=true;
for(auto &letter:password)
{
if(!(letter>='A' && letter<='Z'))
{
flag_all_long=false;
break;
}
}
bool flag_all_short=true;
for(auto &letter:password)
{
if(!(letter>='a' && letter<='z'))
{
flag_all_short=false;
break;
}
}
bool flag_all_digit=true;
for(auto &letter:password)
{
if(!(letter>='0' && letter<='9'))
{
flag_all_digit=false;
break;
}
}
if(flag_all_long || flag_all_short || flag_all_digit)
{
flag=true;
}
vector<string> subsets=getSubstrings(password);
for(auto &words:subsets)
{
if(words!="" && binary_search(common_words.begin(),common_words.end(),words))
{
flag=true;
break;
}
}
if(flag)
ans.push_back("weak");
else
ans.push_back("strong");
}
return ans;
}
int main()
{
vector<string> p,cw,ans;
int n,m;
cin>>n;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
p.push_back(s);
}
cin>>m;
for(int i=0;i<m;i++)
{
string s;
cin>>s;
cw.push_back(s);
}
ans=getPasswordStrength(p,cw);
for(auto &words:ans)
cout<<words<<"\n";
}