Source: Internet
Author: User
Tags lintcode

For a given source string and a target string, should output the first index (from 0) of target string in sour Ce string.

If target does not exist in source, just return -1 .

If Source = "source" and target = "target" , return -1 .

If Source = "abcdabcdefg" and target = "bcd" , return 1 .

classSolution {/*** Returns A index to the first occurrence of target in source, * or-1 If Target was not part of source. * @paramsource string to is scanned. * @paramtarget string containing the sequence of characters to match. */     Public intstrStr (string source, string target) {//Write your code here        if(Source = =NULL|| target = =NULL)            return-1; if(source.length () = = 0 && target.length () = = 0)            return0; if(target.length () = = 0)            return0; intSize1 =source.length (); intSize2 =target.length (); if(Size1 <size2)return-1;  for(inti = 0; I <= size1-size2; i++){            if(Source.charat (i) = = Target.charat (0)){                intj = 0;  for(; J < Size2; J + +){                    if(Source.charat (i + j)! =Target.charat (j)) Break; }                if(J = =size2)returni; }        }                return-1; }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.