Here is the solution , all the best !! normal;mso-layout-grid-align:none;text-autospace:none">=========================================== normal.."/>
Posted by: Colo June 27, 2010
Could anyone please help me with these two problems.
Login in to Rate this Post:     0       ?        

Here is the solution , all the best !!

===========================================


using System;

using System.Collections.Generic;



using System.Linq;



using System.Text;



 namespace ClassLibrary1



{



    public class Class1



    {



        public static void Main(string[]
args)



        {



            string s = "((())())";



            Console.WriteLine(s+"
Should return 1:"
+ nesting(s));



 



            string s1 = "(())())";



            Console.WriteLine(s1 + "
Should return 0:"
+ nesting(s1));



 



            string s2 = string.Empty;



            Console.WriteLine(s2 + "
Should return 1:"
+ nesting(s2));



           



            string s3 = "s(sddsfs(sdasdf)()()
))"
;



            Console.WriteLine(s3 + "
Should return 0:"
+ nesting(s3));



            Console.WriteLine("Test
Number o jump "
);



            int[] arr  =
{2,3,1,1,3};



           int[] arrNeverEnds = { 2, 3, 1, -1, 2, 3 };



            Console.WriteLine("Number
of jump for {2,3,1,1,3} :"
+ arrayJmp(arr));



            Console.WriteLine("Jump
never ends { 2, 3, 1, -1, 2, 3 } :"
+ arrayJmp(arrNeverEnds));



            Console.ReadLine();



       }



 



       public static  int arrayJmp(int[] A)



        {



            if (A.Length == 0)



            {



               
throw new  ArgumentException("empty array");



            }



 



            int numberOfJump = 0;



            int nextJumpIndex 
=0 ;



 



            for (int i =0; i <
A.Length ; i ++)



            {



               
nextJumpIndex = nextJumpIndex+A[nextJumpIndex];



               
numberOfJump++;



               
if (nextJumpIndex > A.Length)



               
{



                    return
numberOfJump;



               
}



 



            }



 



            return -1;



        }



        public static int nesting(string s)



        {



            if (string.IsNullOrEmpty(s))



            {



               
return 1;



          



            }



             char[] arrS = s.ToCharArray();



            Stack<char>
stack = new Stack<char>();



             foreach (char c in arrS)



            {



               
if (c == '(')



               
{



                   
stack.Push(c);



                }



 



               
if (c == ')')



               
{



                   
if (stack.Count > 0)



                   
{



                        stack.Pop();



                   
}



                   
else



                   
{



                        return 0;



 



                   
}



               
}



 



               



            }



 



            if (stack.Count > 0)



            {



               
return 0;



            }



            else



            {



               
return 1;



            }



        }



 



    }



}



 



 

Read Full Discussion Thread for this article