diff --git a/cpp/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp b/cpp/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp new file mode 100644 index 000000000..0116ad06a --- /dev/null +++ b/cpp/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector replaceElements(vector& arr) { + // O(N) Time Complexity , O(1) Space complexity + int n = arr.size(); + int maxSoFar = arr[n-1]; + arr[n-1] = -1; + + for(int i=n-2;i>=0;i--) + { + int temp = maxSoFar; + if(maxSoFar < arr[i]) maxSoFar = arr[i]; + arr[i] = temp; + } + return arr; + } +}; diff --git a/cpp/392-Is-Subsequence.cpp b/cpp/392-Is-Subsequence.cpp new file mode 100644 index 000000000..7bf6aa9df --- /dev/null +++ b/cpp/392-Is-Subsequence.cpp @@ -0,0 +1,19 @@ +// Time Complexity is O(N) where n is the size of the target string. +// Space Complexity is O(1) + +class Solution { +public: + bool isSubsequence(string s, string t) { + int i = 0 , j = 0; + while(j < s.size() && i < t.size()) + { + if(s[j] == t[i]) + j++; + + i++; + } + + if(j >= s.size()) return true; + return false; + } +};