Last active
December 24, 2016 20:11
-
-
Save tylersloeper/6966fd77b439173b4b7051c7f5b16024 to your computer and use it in GitHub Desktop.
hackerrank Stacks: Balanced Brackets (in C)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
The challenge: | |
https://www.hackerrank.com/challenges/ctci-balanced-brackets | |
**/ | |
#include <math.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <limits.h> | |
#include <stdbool.h> | |
bool is_balanced(char expression[]) | |
{ | |
int s = strlen(expression); | |
float strlenhalf = s/2; | |
int countpairs = 0; | |
int difference = 0; | |
//printf("%i \n", s); | |
if((s %2) != 0) //not whole number or odd strlen | |
{ | |
return 0; //false | |
} | |
// "()" **************************************************************************************** | |
//iterate through string | |
for(int i = 0; i < s-1; i++) //from left | |
{ | |
//compare char by ASCII number. | |
//printf("expression[i] %c \n", expression[i]); | |
//printf("expression[i] numerical %i \n", expression[i]); | |
//printf("check i %i \n", i); | |
if(expression[i] == 40) //40 = "(") | |
{ | |
for(int j = s-1; j > 0; j--) //from right | |
{ | |
//printf("check j %i \n", j); | |
if(expression[j] == 41) //41 = ")" | |
{ | |
expression[i] = 0; | |
expression[j] = 0; | |
difference = j- i; | |
//difference = difference %2; | |
countpairs = countpairs+1; | |
//printf("countpairs %i \n", countpairs); | |
//printf("difference %i \n", difference); | |
/* | |
if((difference %2) != 0) //even difference between brackets. so unmatching. not included in as final condition of prototype is a comparison between strlen/2 and total pairs, which does the same thing. | |
{ | |
return 0; //false | |
} | |
*/ | |
break; | |
} | |
} | |
} | |
} | |
// "()" **************************************************************************************** | |
// "[]]" **************************************************************************************** | |
//iterate through string | |
for(int i = 0; i < s-1; i++) //from left | |
{ | |
//compare char by ASCII number. | |
if(expression[i] == 91) //40 = "[") | |
{ | |
for(int j = s-1; j > 0; j--) //from right | |
{ | |
if(expression[j] == 93) //41 = "]" | |
{ | |
expression[i] = 0; | |
expression[j] = 0; | |
difference = j- i; | |
countpairs = countpairs+1; | |
break; | |
} | |
} | |
} | |
} | |
// "[]]" **************************************************************************************** | |
// "{}}]" **************************************************************************************** | |
//iterate through string | |
for(int i = 0; i < s-1; i++) //from left | |
{ | |
//compare char by ASCII number. | |
if(expression[i] == 123) //123 = "{") | |
{ | |
for(int j = s-1; j > 0; j--) //from right | |
{ | |
//printf("check j %i \n", j); | |
if(expression[j] == 125) //125 = "}" | |
{ | |
expression[i] = 0; | |
expression[j] = 0; | |
difference = j- i; | |
countpairs = countpairs+1; | |
break; | |
} | |
} | |
} | |
} | |
// "{}" **************************************************************************************** | |
// END **************************************************************************************** | |
// printf("totalpairs %i \n", countpairs); | |
if(strlenhalf > countpairs) | |
{ | |
return 0; //false | |
} | |
return 1; | |
} | |
int main(){ | |
int t; | |
scanf("%d",&t); | |
for(int a0 = 0; a0 < t; a0++){ | |
char* expression = (char *)malloc(512000 * sizeof(char)); | |
scanf("%s",expression); | |
//for loop and use is_balanced(expression[i]) | |
bool answer = is_balanced(expression); | |
if(answer) | |
printf("YES\n"); | |
else | |
printf("NO\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No constraints included. But 17/18. So I decided to stop here.