thanks Amahdy& Gururajan , I know Amahdy my code does n't work when the length of substring is not equal to length of string.
so how can i loop to search the substring in a string , for this i have posted my problem. thanks once again for your time. On 9/8/11, Amahdy <[email protected]> wrote: > While +Gururajan is correct, u should substract '0', but I recommend > substracting it better in the original conversion loop: > > for(int k=0 ;k<str1.length();k++) { > str[k] =(str1.charAt(k)) - '0'; > > One more thing, the problem statement, ur code may work for the sample case > but not for others, u assume that the substring length (if exists) is equal > to the string length which is not true, for example this test case: 3114, > its solution is: 2. > > -- > Amahdy AbdElAziz > www.amahdy.net > On Sep 8, 2011 8:02 AM, "Gururajan Raghavendran" <[email protected]> > wrote: >> You should subtract value 0 from each character of string. >> >> On Thu, Sep 8, 2011 at 2:18 AM, micke <[email protected]> wrote: >> >>> Question : >>> >>> write program which takes a single argument. The single argument is a >>> string s, which contains only non-zero digits. >>> This function should print the length of longest contiguous substring >>> of s, such that the length of the substring is 2*N digits and the sum >>> of the leftmost N digits is equal to the sum of the rightmost N >>> digits.If there is no such string, your function should print 0. >>> Sample Test Cases: >>> >>> Input #00: >>> 123231 >>> >>> Output #00: >>> 6 >>> >>> >>> > ---------------------------------------------------------------------------------------------------------------------------------------------- >>> my solution is : >>> >>> >>> public class Str { >>> public static void main(String args[]) >>> { >>> >>> String str1 = "123231"; >>> int[] str = new int[str1.length()]; >>> for(int k=0 ;k<str1.length();k++) >>> { >>> str[k] =(str1.charAt(k)); >>> } >>> >>> // int str[] = {1,2,3,2,3,1}; // problem here in >>> conversion in this manner >>> int len = str.length; >>> int sum = 0; >>> int sum1 =0; >>> for(int i=0;i<(len/2);i++) >>> { >>> sum = sum+str[i] - '0'; >>> } >>> >>> for(int j=(len/2);j<(len);j++) >>> { >>> sum1 = sum1+str[j] - '0'; >>> } >>> >>> if((sum==len)&&(sum1==len)) >>> { >>> System.out.println(len); >>> } >>> else >>> { >>> System.out.println(0); >>> } >>> } >>> >>> } >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "google-codejam" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/google-code?hl=en. >>> >>> >> >> -- >> You received this message because you are subscribed to the Google Groups > "google-codejam" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to > [email protected]. >> For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
