Balanced Smileys


  • 0
    L

    Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go :(

    Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.

    A message has balanced parentheses if it consists of one of the following:

    • An empty string ""
    • One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
    • An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'.
    • A message with balanced parentheses followed by another message with balanced parentheses.
    • A smiley face ":)" or a frowny face ":("
      Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.

    Input
    The first line of the input contains a number T (1 ≤ T ≤ 50), the number of test cases.
    The following T lines each contain a message of length s that you got from John.

    Output
    For each of the test cases numbered in order from 1 to T, output "Case #i: " followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be "YES", else it should be "NO" (all quotes for clarity only)

    Constraints
    1 ≤ length of s ≤ 100


  • 2
    T

    Keep count of open parenthesis, close parenthesis, smileys and frowns.
    If number of close parenthesis (excluding the smileys) are ever more than the number of open parenthesis, then parentheses are not balanced.
    If number of open parenthesis at the end (excluding the frowns) are more than the close ones, then also the parentheses are not balanced.
    Here'e the code in Objective-C:

     -(BOOL)areParanthesesBalanced:(NSString*)string
        {
            if([string length] == 0)
                return YES; // YES is the same as 'true'
            
            NSInteger closingBrackets = 0; // NSInteger is the same as 'long'
            NSInteger openBrackets = 0;
            NSInteger smileys = 0;
            NSInteger frowns = 0;
            
            BOOL isPotentialEmoticon = NO; // NO is the same as 'false'
            
            for(NSInteger index = 0; index < [string length]; index++)
            {
                if([string characterAtIndex:index] == '(')
                {
                    if(isPotentialEmoticon)
                        frowns++;
                    openBrackets++;
                }
                else if([string characterAtIndex:index] == ')')
                {
                    if(isPotentialEmoticon)
                        smileys++;
                    closingBrackets++;
                }
                
                if(closingBrackets > openBrackets)
                {
                    if(closingBrackets - smileys > openBrackets)
                        return NO;
                    else
                        closingBrackets = openBrackets;
                }
                
                if([string characterAtIndex:index] == ':')
                    isPotentialEmoticon = YES;
                else
                    isPotentialEmoticon = NO;
            }
            
            if(closingBrackets == openBrackets)
                return YES;
            
            if(openBrackets - frowns > closingBrackets)
                return NO;
            
            return YES;
        }

  • 0
    W

    @tanu15 I didn't look through your code. But according to your description, I think you miss a key idea, which is that every time we meet a colon followed by an open/close parentheses, we have two choice to interpret it, namely either it is part of a emoji or it is a parentheses. For each choice, we continue to do DFS.


  • 1
    S
    public static void main (String[] args)  {
        Scanner in = new Scanner(System.in);
        int t = Integer.valueOf(in.nextLine);
        while (t-- > 0) {
    	    String msg = in.nextLine();
    	    System.out.println(isValid(msg));
        }
    }
    
    private static boolean isValid(String msg) {
        int minOpen = 0, maxOpen = 0;
        for (int i = 0; i < msg.length(); i++) {
            char crr = msg.charAt(i);
            if (crr == '(') {
                maxOpen++;
                if (i == 0 || msg.charAt(i - 1) != ':') {
                    minOpen++;
                }
            } else if (crr == ')') {
                minOpen = Math.max(0, minOpen - 1);
                if (i == 0 || msg.charAt(i - 1) != ':') {
                    maxOpen--;
                    if (maxOpen < 0) break;
                }
            }
        }
        return maxOpen >= 0 && minOpen == 0;
    }
    
    

  • 0
    S
    This post is deleted!

  • 0
    Y
    This post is deleted!

  • 0
    N

    can some one explain this in more detail would it be more of a dp programming like integer break??? if we need to analyze whether a smiley is counted as a smiley or close paren


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.