Problem Statement
Structured langugages such as Java, C# and C++ are made up of blocks of code (usually nested several levels), where the start and end markers for a block are denoted by curly brackets, '{' and '}'. A variable declared in a block is only accessible within that block and its sub-blocks.
In C#, you are not allowed to declare a variable in a block if any of its sub-blocks also declares a variable with the same name. The following code, for instance, will give a compiler error in C# (but not in Java or C++):
public void MyMethod() { for(int j = 0; j < 5; j++) { int i = j; } int i = 0; }
Changing int i = 0; to { int i = 0; } would be legal C#.
Your task is to write a method which finds this kind of illegal variable declaration. The input will be a
Create a class VariableDeclaration containing the method badVariable which takes a
Definition
- Class:
- VariableDeclaration
- Method:
- badDeclaration
- Parameters:
- String
- Returns:
- String
- Method signature:
- String badDeclaration(String code)
- (be sure your method is public)
Constraints
- code will contain between 2 and 50 characters, inclusive.
- Each character in code will be a '{', a '}' or a lowercase letter.
- The curly brackets in code will match up properly.
- Tbere will be a single outer block; that is, the first character in code will be a '{', whose corresponding '}' will be the last character.
- A variable won't be declared more than once in the same block.
Examples
"{{ji}i}"
Returns: "i"
This is the example in the problem statement.
"{{ji}{i}}"
Returns: ""
"{d{{e}{fd}}{e}{df{{a}}}a}"
Returns: "ad"
Make sure not to output duplicates, and to sort the badly declared variables in alphabetical order.
"{a{{{{{{{{{{{{{{{{{{{{{{{a}}}}}}}}}}}}}}}}}}}}}}}}"
Returns: "a"
"{{{{{{{{{{{{{{{{{{{{{{{{z}}}}}}}}}}}}}}}}}}}}}}}z}"
Returns: "z"
"{{{{{{{{{{{{{{{{{{{{{{{k}}}}}}}}}}}}}{k}}}}}}}}}}}"
Returns: ""
"{abcdefgopqrstuvw{wvutsrqponmlkjihgfedcba}hijklmn}"
Returns: "abcdefghijklmnopqrstuvw"
"{abcdefghijklmnopqrstuvwxyz}"
Returns: ""
"{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}"
Returns: ""
"{{{v{{g}vt}{g}a{g}q}}q{q{q}{{}}{}u{{{}}}ovjg{}}}"
Returns: "qv"
"{{}m{w}{se{oky{k{m}}a}{}{sokew}on}{{}ay}}"
Returns: "ekmos"
"{p{hk{rqkx{}}}r{k{c}c{{{}{}y}{}{c}xkbr{x}}}}"
Returns: "ckrx"
"{{}{}{d}u{}{}{dti{{}iu}{{}}{}}td{t}{d}{}}"
Returns: "ditu"
"{{{h}z}{}{dty}h{e{}zt}{{y}i{}{}}i{dh{}{}}y}"
Returns: "hiy"
"{{fc}{}{md}{mdfco{f}{df{{}f}}{}}c}"
Returns: "cdf"
"{b{xa{b{xb}}{}b{pm{}{}xbnl}}{}p{}{}}"
Returns: "bpx"
"{u{ei{d{{u}{}}{l}}{iwel}{l}}{}iw{}d{}l{}{}}"
Returns: "deiluw"
"{{}{{o}}{{}{}{}y{}{gu{}}{g}}wj{l}{}{w}g{j}{jw}}"
Returns: "gjw"
"{{gn{}{s{jnr}}{s}b{b}{}{vk}jv}b{{l}{b}{}{gr}}e}"
Returns: "bjnv"
"{{}{}{}zb{}lq{}{o}{{lq}}{qb}{l}{sohr}v}"
Returns: "blq"
"{{b}jh{}{{kqjbo}z{z}}w{g}b{}g{jw}{}m}"
Returns: "bgjwz"
"{c{ep}{{c}y{by}{n{}}{gy}{a{bge}}cab}{}y}"
Returns: "abcy"
"{{}e{pez}pza{a{wlz}ce{z}l}r{}{epc}{}{}}"
Returns: "aelpz"
"{vl{{}i}{itz}zq{i}{d{e}{t}q{qt{}}}e}"
Returns: "eqz"
"{{{r}or{}{y}{gr{}h}}{{}{}g}{yr{}{y}qj}{}}"
Returns: "ry"
"{e{ieg}k{}{}g{{{ig}}ie{}{eg}}{}{i}i}"
Returns: "egi"
"{{}{{s}o}{}z{}{o}b{}s{o}{}{}{s}{{}b}onu{{}}{o}}"
Returns: "bos"
"{j{}{km}b{}{{}j{}{}e{mpoj{h{}j}}po}}"
Returns: "jop"
"{{q}{{f}}{k}{p{}lkt{}f}{g}bl{lpo}o{fkbp}{}{}f}"
Returns: "bflo"
"{f{{v{xoi}{o}{i}}{}{{}x}{{}}{{}f{x}{}}{ov}{}v}z{}}"
Returns: "fv"
"{cz{z{b{}c{}{{}}{{}}{}u{}{cuz{}k{}}{}}b}}"
Returns: "bcuz"
"{{}p{p}{j}n{}{}a{{}na{}{j}{{ja{j}}p}{}}}"
Returns: "ajnp"
"{{p}r{}{vp{k}{}xy}vx{}{{v}{}xpk{x}{r}}{{}}{}{}}"
Returns: "rvx"
"{{{q}}ce{}{{}{}}{}{edb{c{}}w{}m{}i{w{}e}}q{}}"
Returns: "ceqw"
"{{a}{n{}zaf{qz}{}w{{q{}nd}}{w}}{}z{qa}fq}"
Returns: "fnqwz"
"{bk{}{ka{a}{}{ka{bda}b}{}{{b}}{b}}{}}"
Returns: "abk"
"{{{}sf{mgb{}{}}{}{f{}m{g{m}mt}t}b}f{}bts{}}"
Returns: "bfmst"
"{{{}z}x{ly{}{}{{}{}}}zlt{yd{}{y}{}z}{x}s}"
Returns: "lxyz"
"{h{o{}}yx{}f{f{}{y}k{}{y}x{{}}b}{{}y{}{}c}}"
Returns: "fxy"
"{z{y{{}}}v{{{{uy}{}}u}}{ye{}}{{g{}}uog}{{}}e}"
Returns: "egu"
"{{jp}t{{}}q{j{aq}{{xp}xa}}dj{{}{}{}p}aw{w}}"
Returns: "ajqwx"
"{{{w}h{}{wm}q{wsm}{qc}{r}{}r}{cm}{{c}}m}"
Returns: "mqr"
"{ijs{po{}{moh{{}jo{}{{m}mjiso}h{}}{ih}}{}s}p}"
Returns: "hijmops"
"{k{g{c}v}a{{k}k{i}c{}}{ag}ov{xz}{cgv}{oz{}}z}"
Returns: "akovz"
"{{qb}{n}{q{}}qbn{}{ql}{lb{n}}{b}l{n}}"
Returns: "blnq"
"{b{f}{}{}{}{b}{xo{}{}{ufbj{}{}h{}}j}ojs}"
Returns: "bjo"
"{r{}{}{l{z{pe}{eq{}p}xlpq}{c}rz}czl{}}"
Returns: "clpqrz"
"{{up}{}{u}t{}{r}o{}{}{}{{la}l}r{to{ytp}}}"
Returns: "lort"
"{{{ir}m}{uir{}{{}i{mu}{{}ur}}}{m}{m}}"
Returns: "iru"
"{{}di{}f{s}p{}{d{}{}fi{}}m{pi{}}}"
Returns: "dfip"
"{{h}mi{{{jgi{}}}}{}j{i{g}{}{}{}m{}}{}h}"
Returns: "hijm"
"{{u}s{u}y{}v{}{s}{}{}{u{}}u{i}{v}i}"
Returns: "isuv"
"{{{}{x}v}{{{}v}x{vx}{vcrx}{}{x}}{w}w{}{}{rx}x}"
Returns: "wx"
"{jtzh{}kn{}{{{j{wi}}}{w{h}}z{{}i}{}}{knw}}"
Returns: "hjknz"
"{mfz{f{r{m{}r}{}}}{}wg{h}{{{}w}g{r{}}m}}"
Returns: "fgmrw"
"{{jf{mf}{}}{}{{jfkv}}mf{v{f}k}{mv}{{km}f}{}}"
Returns: "fm"
"{hptf{{}hp{{h{p}p}p}}{hp}{h{{t}{f{}}{}}{h}}}"
Returns: "fhpt"
"{y{{}}{r{}{rh}g{{w{v}{a}}a{}{{}a{b}{}{bw}{}}tby}}}"
Returns: "abry"
"{{tz{}v{}}bz{t{}x{}g{}v{}}{z}vgtx{{}}}"
Returns: "gtvxz"