bool IsWin(string A, string B)
{
vector<int> vec = {0,1,2,3,4,5,6,7,8,9};
for(long int i=0;i<A.length();i++){
vec[A[i]-'0'] = 11;
}
if(IsArray(vec)){ //IsArray(vec) It just checks whether all elements are 11.
return true;
}
for(long int i=0;i<B.length();i++){
vec[B[i]-'0'] = 11;
}
return IsArray(Array);
}

- 400,186
- 35
- 402
- 621

- 335
- 1
- 5
- 16
-
2Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 28 '18 at 18:39
-
Till Now I've just come up with this solution. But this isn't efficient as required. – Bhavay Anand Jan 28 '18 at 18:40
-
3If you have a solution that works, but you're looking for a more efficient way, you could try https://codereview.stackexchange.com/ or https://codegolf.stackexchange.com/ – Ates Goral Jan 28 '18 at 18:42
-
1@bhavayanand Define _efficient_ please. Percormance wise, memory wise, less code? – Jan 28 '18 at 18:43
-
1@TheDude add "readable" to that list. – Jesper Juhl Jan 28 '18 at 18:45
-
performance wise @TheDude – Bhavay Anand Jan 28 '18 at 18:50
-
Will sure do @AtesGoral Thanks – Bhavay Anand Jan 28 '18 at 18:50
-
2@bhavayanand Use a profiler then to find the bottleneck. – Jan 28 '18 at 18:51
-
Sorry, no idea what a bottleneck is? @TheDude – Bhavay Anand Jan 28 '18 at 18:52
-
Bottleneck is the point at which the program is most slow, either because it has to do a lot of work (may or may not be necessary), sits around waiting, or is just plain slow. The necessary is unavoidable, look for a better algorithm, the unnecessary you prune out and carry on. – user4581301 Jan 28 '18 at 18:57
-
Thank You so much for the explanation @user4581301 – Bhavay Anand Jan 28 '18 at 18:59
4 Answers
Seems like the obvious way would be something like this:
bool has_all_digits(std::string const &input) {
std::vector<char> present(10, 0);
for (auto c : input)
present[c - '0' ] = 1;
return std::find(present.begin(), present.end(), 0) == present.end();
}

- 476,176
- 80
- 629
- 1,111
-
Can you please explain this code a little? I'm new to programming. – Bhavay Anand Jan 28 '18 at 18:58
-
-
@bhavayanand http://en.cppreference.com/w/cpp/algorithm/find you can inform yourself about that. – Jan 28 '18 at 19:49
In this case std::bitset is probably the most efficient way to solve this.
bool check(string s){
std::bitset<10> digits;
int digitCounter = 0;
int index;
for (char c : s){
if (std::isdigit(c)){
index = c - '0';
if (!digits[index]){
digitCounter++;
digits[index] = true;
if (digitCounter == 10)
return true;
}
}
}
return false;
}

- 5,540
- 3
- 16
- 42
-
Still not efficient enough. 16 out of 19 submissions get timed out. – Bhavay Anand Jan 28 '18 at 19:59
-
Try now. And if it's not enough I'd like to see the subsmissions sampler. – Marco Luzzara Jan 28 '18 at 20:21
-
[link](https://www.hackerrank.com/contests/hackerrank-hiring-contest/challenges/winning-lottery-ticket/) – Bhavay Anand Jan 28 '18 at 20:36
This will have out of range exceptions for vec if A or B contains anything beside digits.
Between the loops vec needs to be reinitialised.
Further the last line should be IsArray(vec)
.
And why not use a vector<bool> vec
?
Before you care for efficiency the algorithm should work and be exception safe.

- 1,438
- 2
- 15
- 20
-
It's a competitive programming question. so vector will never get any input other than digits according to the constraints given in the question. – Bhavay Anand Jan 28 '18 at 19:05
Any talk about performance is meaningless without benchmarking. So you need to measure and then measure some more.
With that in mind here is a version that is optimized for long strings with dense digits, i.e. that have a high probability to contain all the digits in a small substring. The function will exits as soon as it found all the digits and will not continue to the rest of the string:
auto has_all_digits(const std::string& str) -> bool
{
std::array<int, 10> digits = {};
int num_digits = 0;
for (char ch : str)
{
if (!is_digit(ch))
continue;
// here is a cool branchless trick
// to count unique digits
int x = 1;
std::swap(digits[ch - '0'], x); // put 1 to digits and get the old value in x
num_digits += 1 - x; // increment if the old value was 0
if (num_digits == 10)
return true;
}
return false;
}
Again, don't forget to measure and see if this algorithm is indeed eficient for your use case.

- 72,283
- 15
- 145
- 224
-
In std::swap function you swap the value of x with some uninitialised digits[ ] element. Wouldn't x will get some garbage value? If this is some stupid or obvious ques please forgive me i'm new to programming. – Bhavay Anand Jan 28 '18 at 19:40
-
-
But there's no value in braces. Does this mean all value to be initialised to 0 or NULL ? – Bhavay Anand Jan 28 '18 at 20:04
-
@bhavayanand `0` in this case. See http://en.cppreference.com/w/cpp/language/aggregate_initialization – bolov Jan 28 '18 at 22:02
-
Okay I got it now. From your ans i got to know about auto , range based for loop, array default constructor , that i didn't know before. Just one last thing, in first line, -> bool what does this mean?? – Bhavay Anand Jan 29 '18 at 08:36
-
@bhavayanand see https://stackoverflow.com/questions/11215227/should-the-trailing-return-type-syntax-style-become-the-default-for-new-c11-pr – bolov Jan 29 '18 at 09:13