fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. string s1 , s2;
  7. cin>>s1>>s2;
  8.  
  9. int i=0 , j=0;
  10. while(i < s1.size() && j < s2.size())
  11. {
  12. if(s1[i]==s2[j]) // when both the characters matches
  13. {
  14. // move both the pointers
  15. i++;
  16. j++;
  17. }
  18. else // else there are not matching
  19. {
  20. i++; // we will do i++
  21. }
  22. }
  23.  
  24. // s1 = a b c z
  25. // s2 = a z d b
  26. // size of s2 string is 4
  27. // a z d b
  28. // j
  29. // 'j' is pointing at index 2 in s2
  30. // 4 - 2 = 2
  31. // ans = 2
  32. int ans = s2.size() - j;
  33. cout<<ans<<" ";
  34.  
  35.  
  36. return 0;
  37. }
  38. /*
  39. abcz
  40. azdb
  41.  
  42. o/p: 2
  43. */
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
0