+ 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

8th May 2021, 8:25 AM
Parth Shendge
3 ответов
+ 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
8th May 2021, 12:00 PM
MrMysterious5
+ 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.
8th May 2021, 9:49 AM
Shadow
Shadow - avatar
0
Oh ! Thanks I forgot to do that .
9th May 2021, 11:16 AM
Parth Shendge