fork download
  1. #include <iostream>
  2. using namespace std;
  3. int idx=-1;
  4. bool getSequence(string &a,string &b){
  5. int count=0;
  6. int i=0;
  7. int j=0;
  8. int n1=a.size();
  9. int n2=b.size();
  10. while(i<n1 && j<n2){
  11. if(a[i]==b[j]){
  12. if(count==0){
  13. idx=i;
  14. }
  15. i++;
  16. j++;
  17. count++;
  18. }
  19. else{
  20. i++;
  21. }
  22. }
  23. return count==n2;
  24. }
  25.  
  26. int main() {
  27. // your code goes here
  28. string s1="abcde";
  29. string s2="bd";
  30. int ans=0;
  31. for(int i=1;i<s2.size();i++){ //as per the condition checking for the character starting from index 1
  32. for(char j='a';j<='z';j++){
  33. string a=s2;
  34. a[i]=j;
  35. idx=-1;
  36. if(getSequence(s1,a)==true){
  37. ans=idx+1; //1-based answer
  38. }
  39. }
  40. }
  41. cout<<"index of subsequence in s1 of s2 is:"<<ans;
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
index of subsequence in s1 of s2 is:2