OraQA

Oracle Question and Answer


Latest Comments

  • Laurent Schneider:
    if you like a Base64 format, maybe this… select utl_raw.cast_to_varchar 2(...

  • hsafra:
    You need to give more specs for the question: What letter are acceptable? What letters aren’t? Do you...

  • ragunathansd:
    I am not inserting sequence numbers from database. I need to populate the data in a grid. If a user...

  • gamyers:
    “redo log file gets full” That is the nature of a redo log file. It gets full, switched and...

Comments RSS feed


  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

How to generate all strings of balanced parentheses in SQL

June 16th, 2008 By Frank Zhou

The following SQL pattern can be used to generate all strings of balanced parentheses.

variable input number

—————————-SQL Solution ————————–

SELECT str
FROM
(SELECT CASE WHEN instr(str,')(') = 0
	     THEN 'F'
             ELSE 'T' END flag, str FROM (SELECT rpad('()', &input * 2,'()') str from dual )
)
MODEL
DIMENSION BY (0 dim)
MEASURES(str, flag)
RULES ITERATE (10000) UNTIL (instr(str[ITERATION_NUMBER+1],')(') = 0 OR flag[0] = 'F')
(
 str[ITERATION_NUMBER+1] =
 CASE WHEN instr(str[cv()-1],')(') >0
      THEN CASE WHEN instr(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),'()')>0
		THEN rpad('()',least(length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),')')),
		                length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),'(')))*2,'()')
		    ||CASE WHEN length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),')')) =
				least(length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),')')),
	                        length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),'(')))
                           THEN rpad(')', abs(length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),')'))
	                         - length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),'(')) ),')')
	                   ELSE rpad('(', abs(length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),')'))
	                         - length(replace(substr(str[cv()-1],1,instr(str[cv()-1],')(')-1),'('))),'(')
                       END||'()'||substr(str[cv()-1], instr(str[cv()-1],')(')+2   )
		 ELSE substr(str[cv()-1],1,instr(str[cv()-1],')(')-1)||'()'||
		      substr(str[cv()-1],instr(str[cv()-1],')(')+2)
		END
END
);

Enter value for input: 5
old   5:              ELSE 'T' END flag, str FROM (SELECT rpad('()', &input * 2,'()') str from dual )
new   5:              ELSE 'T' END flag, str FROM (SELECT rpad('()', 5 * 2,'()') str from dual )

STR
----------
()()()()()
(())()()()
()(())()()
(()())()()
((()))()()
()()(())()
(())(())()
()(()())()
(()()())()
((())())()
()((()))()                                                                      

STR
----------
(()(()))()
((()()))()
(((())))()
()()()(())
(())()(())
()(())(())
(()())(())
((()))(())
()()(()())
(())(()())
()(()()())                                                                      

STR
----------
(()()()())
((())()())
()((())())
(()(())())
((()())())
(((()))())
()()((()))
(())((()))
()(()(()))
(()()(()))
((())(()))                                                                      

STR
----------
()((()()))
(()(()()))
((()()()))
(((())()))
()(((())))
(()((())))
((()(())))
(((()())))
((((()))))                                                                      

42 rows selected.

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question