thorpedosg

pHw9MSKs

Aug 6th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. StringBuilder newLine = new StringBuilder(line6);
  2. boolean keepLooping = true;
  3. while (keepLooping) {
  4.  
  5. int firstBracket = newLine.indexOf("(");
  6. int lastBracket = newLine.lastIndexOf(")");
  7.  
  8. if (firstBracket != -1 && lastBracket != -1) {
  9. if(firstBracket > lastBracket){
  10. //we hit a situation where the string has ")(" it can never be balanced
  11. System.out.println("NO");
  12. keepLooping = false;
  13. break;
  14. }
  15. newLine.setCharAt(lastBracket, ' ');
  16. newLine.setCharAt(firstBracket, ' ');
  17. continue;
  18. }
  19. if (firstBracket == -1 && lastBracket == -1) {
  20. // no more brackets or emoticons
  21. System.out.println("YES");
  22. keepLooping = false;
  23. }
  24. if (firstBracket != -1) {
  25. // possible emoticon :(
  26. if (newLine.charAt(firstBracket - 1) == ':') {
  27. newLine.setCharAt(firstBracket, ' ');
  28. newLine.setCharAt(firstBracket - 1, ' ');
  29. continue;
  30. } else {
  31. System.out.println("NO");
  32. keepLooping = false;
  33. }
  34. }
  35. if (lastBracket != -1) {
  36. // possible emoticon :)
  37. if (newLine.charAt(lastBracket - 1) == ':') {
  38. newLine.setCharAt(lastBracket, ' ');
  39. newLine.setCharAt(lastBracket - 1, ' ');
  40. } else {
  41. System.out.println("NO");
  42. keepLooping = false;
  43. }
  44. }
  45. }
Add Comment
Please, Sign In to add comment