|
1 |
| -// The nested brackets problem is a problem that determines if a sequence of |
2 |
| -// brackets are properly nested. A sequence of brackets s is considered properly nested |
| 1 | +// Package nested_brackets provides functions for testing |
| 2 | +// strings propper brackets nesting. |
| 3 | +package nested_brackets |
| 4 | + |
| 5 | +// IsBalanced returns true if provided input string is properly nested. |
| 6 | +// |
| 7 | +// Input is a sequence of brackets: '(', ')', '[', ']', '{', '}'. |
| 8 | +// |
| 9 | +// A sequence of brackets `s` is considered properly nested |
3 | 10 | // if any of the following conditions are true:
|
4 |
| -// - s is empty |
5 |
| -// - s has the form (U) or [U] or {U} where U is a properly nested string |
6 |
| -// - s has the form VW where V and W are properly nested strings |
| 11 | +// - `s` is empty; |
| 12 | +// - `s` has the form (U) or [U] or {U} where U is a properly nested string; |
| 13 | +// - `s` has the form VW where V and W are properly nested strings. |
| 14 | +// |
7 | 15 | // For example, the string "()()[()]" is properly nested but "[(()]" is not.
|
8 |
| -// The function called isBalanced takes as input a string which is a sequence of brackets and |
9 |
| -// returns true if input is nested and false otherwise. |
10 |
| -//note that only an even number of brackets can be properly nested |
11 |
| - |
12 |
| -package nested_brackets |
| 16 | +// |
| 17 | +// **Note** Providing characters other then brackets would return false, |
| 18 | +// despite brackets sequence in the string. Make sure to filter |
| 19 | +// input before useage. |
| 20 | +func IsBalanced(input string) bool { |
| 21 | + if len(input) == 0 { |
| 22 | + return true |
| 23 | + } |
13 | 24 |
|
14 |
| -// IsBalanced function which checks whether the number of brackets are balanced |
15 |
| -func IsBalanced(input string) string { |
16 | 25 | if len(input)%2 != 0 {
|
17 |
| - return input + "is not balanced." |
| 26 | + return false |
18 | 27 | }
|
19 |
| - if len(input) > 0 { |
20 |
| - var stack []byte |
21 |
| - for i := 0; i < len(input); i++ { |
22 |
| - if input[i] == '(' || input[i] == '{' || input[i] == '[' { |
23 |
| - stack = append(stack, input[i]) |
24 |
| - } else { |
25 |
| - if len(stack) > 0 { |
26 |
| - pair := string(stack[len(stack)-1]) + string(input[i]) |
27 |
| - stack = stack[:len(stack)-1] |
28 | 28 |
|
29 |
| - if pair != "[]" && pair != "{}" && pair != "()" { |
30 |
| - return input + " is not balanced." |
31 |
| - } |
32 |
| - } else { |
33 |
| - return input + " is not balanced." |
| 29 | + // Brackets such as '{', '[', '(' are valid UTF-8 characters, |
| 30 | + // which means that only one byte is required to code them, |
| 31 | + // so can be stored as bytes. |
| 32 | + var stack []byte |
| 33 | + |
| 34 | + for i := 0; i < len(input); i++ { |
| 35 | + if input[i] == '(' || input[i] == '{' || input[i] == '[' { |
| 36 | + stack = append(stack, input[i]) |
| 37 | + } else { |
| 38 | + if len(stack) > 0 { |
| 39 | + pair := string(stack[len(stack)-1]) + string(input[i]) |
| 40 | + stack = stack[:len(stack)-1] |
| 41 | + |
| 42 | + if pair != "[]" && pair != "{}" && pair != "()" { |
| 43 | + // This means that two types of brackets has |
| 44 | + // been mixed together, for example "([)]", |
| 45 | + // which makes seuqence invalid by definition. |
| 46 | + return false |
34 | 47 | }
|
| 48 | + } else { |
| 49 | + // This means that closing bracket is encountered |
| 50 | + // before opening one, which makes all sequence |
| 51 | + // invalid by definition. |
| 52 | + return false |
35 | 53 | }
|
36 | 54 | }
|
37 |
| - if len(stack) == 0 { |
38 |
| - return input + " is balanced." |
39 |
| - } |
40 | 55 | }
|
41 |
| - return "Please enter a sequence of brackets." |
42 |
| -} |
43 |
| - |
44 |
| -// func main() { |
45 |
| -// reader := bufio.NewReader(os.Stdin) |
46 |
| -// fmt.Print("Enter sequence of brackets: ") |
47 |
| -// text, _ := reader.ReadString('\n') |
48 | 56 |
|
49 |
| -// text = strings.TrimSpace(text) |
50 |
| -// fmt.Println(isBalanced(text)) |
51 |
| -// } |
| 57 | + // If sequence is properly nested, all elements in stack |
| 58 | + // has been paired with closing elements. If even one |
| 59 | + // element has not been paired with a closing bracket, |
| 60 | + // means that sequence is invalid by definition. |
| 61 | + return len(stack) == 0 |
| 62 | +} |
0 commit comments