vector solveB(vector v) { /** Function to solve the second task Input: v: vector Output: the longest subsequence satisfying the statement condition (prints to the standard ouput) Time Complexity for the algorithm O(N) Space complexity: O(1) extra space (without counting the array) */ int i = 0, maxi = 1, limax = 0; while(i < v.length) { int ls = i; while(ls + 1 < v.length && sameDigits(v.arr[ls], v.arr[ls + 1]) == 1) ++ ls; if(maxi < ls - i + 1) { maxi = ls - i + 1; limax = i; } i = ls + 1; } vector ret; ret.length = 0; for(i = limax ; i < limax + maxi ; ++ i) { ret.arr[ret.length ++ ] = v.arr[i]; } return ret; }
int main(int argc, char **argv) { std::string string1 = argv[1]; std::string string2 = argv[2]; if(sameDigits(string1, string2)) { std::cout << string1 << " and " << string2 << " are anagrams\n"; } else { std::cout << string1 << " and " << string2 << " aren't anagrams\n"; } return 0; }