+ 1
Please help me with this exercise.
What's wrong with this code. I cannot view test case 5 which is wrong. Please help. Code : #include <iostream> using namespace std; int main() { string inp, inp2; cin >> inp; inp2 = inp; bool isUnique = true; for (int i=0; i<inp.size(); i++){ for (int j=0; j<i; j++){ if(inp[i] == inp2[j]){ cout<<"Deja Vu"; isUnique = false; break; } } } if(isUnique){ cout<<"Unique"; } return 0; } https://code.sololearn.com/cK3TeZZK9Dj9/?ref=app
3 Respostas
+ 3
Parth Shendge
Break statement breaks the inner loop but not the outer loop. You can use this code.
https://code.sololearn.com/ceX80apj3Ef0/?ref=app
+ 1
Breaking from the inner loop is not going to terminate the outer loop. As a consequence, you print "Deja Vu" multiple times if the same letter has more occurences than two, or multiple different letters have duplicates.
You could modify the condition of your outer loop to prevent this, for example:
for ( ...; i < inp.size() && isUnique; ... )
{
...
}
This way, you would correctly exit both loops as soon as a duplicate letter has been found.
0
Oh ! Thanks I forgot to do that .