fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string canUntangleWires(const string& sequence) {
  8. stack<char> s;
  9.  
  10. for (char ch : sequence) {
  11. // If the stack is not empty and the top character is the same as the current one,
  12. // pop the top character (i.e., they cancel each other out).
  13. if (!s.empty() && s.top() == ch) {
  14. s.pop();
  15. } else {
  16. // Otherwise, push the current character onto the stack.
  17. s.push(ch);
  18. }
  19. }
  20.  
  21. // If the stack is empty, all crossings are resolved
  22. return s.empty() ? "Yes" : "No";
  23. }
  24.  
  25. int main() {
  26. string sequence;
  27. cin >> sequence;
  28. cout << canUntangleWires(sequence) << endl;
  29. return 0;
  30. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Yes