Question: formal proof of how this algorithm is correct? bool isSubsequence(string sequence, string subsequence) { /* Objective : To check for subsequence in a given string
formal proof of how this algorithm is correct?
| bool isSubsequence(string sequence, string subsequence) { | |
| /* | |
| Objective : To check for subsequence in a given string | |
| Input parameters: sequence - string, sequence S | |
| subsequence - string, subsequence S' | |
| Return Value : true, if subsequence found | |
| otherwise, false | |
| */ | |
| int n = sequence.length(); //length of sequence | |
| int m = subsequence.length(); //length of subsequence | |
| int j = 0; | |
| //for loop till length of sequence | |
| for(int i = 0; i < n; ++i) { | |
| //break if all chars of subsequence are checked | |
| if(j == m) { | |
| break; | |
| } | |
| //increment j if subsequence[j] is equal to sequence[i] | |
| if(subsequence[j] == sequence[i]) { | |
| j++; | |
| } | |
| } | |
| //subsequence found if j is equal to length of subsequence | |
| if(j == m) { | |
| return true ; | |
| } | |
| return false; | |
| } |
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
