fork download
  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. string haystack = "leetcode" ;
  5. string needle = "leeto";
  6. bool valid = false;
  7. for(int i =0, j=0; i<haystack.length(); i++,j++){
  8. if(haystack[i]==needle[j] && needle[j] != '\0'){
  9. valid = true;
  10. }
  11. if(needle[j] =='\0'){
  12. cout <<i -j;
  13. return 0;
  14. }
  15. }
  16. cout<< "-1";
  17. }
Success #stdin #stdout 0s 5284KB
stdin
45
stdout
Standard output is empty