Basic Logic for Solution


  • 0
    A
    /*
     * main.c
     *
     *  Created on: Sep 2, 2017
     *      Author: Prakhar Avasthi
     */
    
    /*
     * This problem only requires basic logic.
     * To come back to same position, no. of R should be equal to no. of L
     * likewise, no. of U should be equal to no. of D
     */
    
    #include<stdio.h>
    
    int judgeCircle(char* moves) {
        int right_moves = 0, left_moves = 0;
        int up_moves = 0, down_moves = 0;
    
        for(int i = 0; moves[i] != '\0' ;i++)
        {
        	if(moves[i] == 'U')
        		up_moves++;
    
        	if(moves[i] == 'D')
    			down_moves++;
    
        	if(moves[i] == 'R')
    			right_moves++;
    
        	if(moves[i] == 'L')
    			left_moves++;
        }
    
        if((up_moves == down_moves) && (right_moves == left_moves))
        	return 1;
        else
        	return 0;
    }
    
    int main()
    {
    	int result = judgeCircle("UDRL");
    	if(result == 1)
    		printf("TRUE");
    	else
    		printf("FALSE");
    }
    

Log in to reply
 

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