Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string = "I like parentheticals (a lot). Sometimes(when I nest them(my parentheticals) too much (like
- this(and this))), it get confusing."
- def parentheticals_open_close_position(str)
- starting_parentheticals = []
- ending_parenthetical = []
- str.each_char.with_index do |char, index|
- if char == '('
- starting_parentheticals << index
- end
- if char == ')'
- ending_parenthetical << index
- end
- end
- p starting_parentheticals
- p ending_parenthetical
- hash = Hash[starting_parentheticals.zip(ending_parenthetical)]
- p hash
- end
- # A: nA, nB, nC
- # B: nA, nB, nC
- parentheticals_open_close_position(string)
- # How I would solve by hand...
- # I think that I would just strike off the starting and ending at the same time since that would help me easily keep the order between the two. This is assuming the string had a lot of "tricky" to track parentheticals. If it were simple, I think I would just find the first opening one and then immediately find the closing parenthetical.
- # Time complexity
- # I think the time complexity is O(n) since it should take the same amount of time to given a similar string. This is an area of programming I would like to learn more about. I am not a CS grad, so I didn't get educated in Big-O notation, but I under it's important for performance and it's on my list of things to improve on.
- # I think the space complexity is O(n log n), but again, I'm no expert in this area...
Add Comment
Please, Sign In to add comment