diff --git a/c++/longest-palindromic-subseq.cpp b/c++/longest-palindromic-subseq.cpp new file mode 100644 index 0000000..ade1a2f --- /dev/null +++ b/c++/longest-palindromic-subseq.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + +int tabulation(string t1, string t2) +{ + vectorcurr(t2.length()+19,0); + vectornext(t2.length()+19,0); + + for(int i = t1.length()-1;i>=0;i--) + { + for(int j =t2.length()-1;j>=0;j--) + { + int ans =0; + if(t1[i]==t2[j]) + { + ans=1+next[j+1]; + } + else + { + ans=max(next[j],curr[j+1]); + } + curr[j]=ans; + } + next=curr; + } + return curr[0]; +} + + int longestPalindromeSubseq(string s) { + string str=s; + reverse(s.begin(),s.end()); + return tabulation(s,str); + } +}; diff --git a/c++/merge-sort.cpp b/c++/merge-sort.cpp new file mode 100644 index 0000000..6f543bf --- /dev/null +++ b/c++/merge-sort.cpp @@ -0,0 +1,104 @@ +// C++ program for Merge Sort +#include +using namespace std; + +// Merges two subarrays of array[]. +// First subarray is arr[begin..mid] +// Second subarray is arr[mid+1..end] +void merge(int array[], int const left, int const mid, + int const right) +{ + int const subArrayOne = mid - left + 1; + int const subArrayTwo = right - mid; + + // Create temp arrays + auto *leftArray = new int[subArrayOne], + *rightArray = new int[subArrayTwo]; + + // Copy data to temp arrays leftArray[] and rightArray[] + for (auto i = 0; i < subArrayOne; i++) + leftArray[i] = array[left + i]; + for (auto j = 0; j < subArrayTwo; j++) + rightArray[j] = array[mid + 1 + j]; + + auto indexOfSubArrayOne = 0, indexOfSubArrayTwo = 0; + int indexOfMergedArray = left; + + // Merge the temp arrays back into array[left..right] + while (indexOfSubArrayOne < subArrayOne + && indexOfSubArrayTwo < subArrayTwo) { + if (leftArray[indexOfSubArrayOne] + <= rightArray[indexOfSubArrayTwo]) { + array[indexOfMergedArray] + = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + } + else { + array[indexOfMergedArray] + = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + } + indexOfMergedArray++; + } + + // Copy the remaining elements of + // left[], if there are any + while (indexOfSubArrayOne < subArrayOne) { + array[indexOfMergedArray] + = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + indexOfMergedArray++; + } + + // Copy the remaining elements of + // right[], if there are any + while (indexOfSubArrayTwo < subArrayTwo) { + array[indexOfMergedArray] + = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + indexOfMergedArray++; + } + delete[] leftArray; + delete[] rightArray; +} + +// begin is for left index and end is right index +// of the sub-array of arr to be sorted +void mergeSort(int array[], int const begin, int const end) +{ + if (begin >= end) + return; + + int mid = begin + (end - begin) / 2; + mergeSort(array, begin, mid); + mergeSort(array, mid + 1, end); + merge(array, begin, mid, end); +} + +// UTILITY FUNCTIONS +// Function to print an array +void printArray(int A[], int size) +{ + for (int i = 0; i < size; i++) + cout << A[i] << " "; + cout << endl; +} + +// Driver code +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + int arr_size = sizeof(arr) / sizeof(arr[0]); + + cout << "Given array is \n"; + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + cout << "\nSorted array is \n"; + printArray(arr, arr_size); + return 0; +} + +// This code is contributed by Mayank Tyagi +// This code was revised by Joshua Estes diff --git a/c++/word-ladder.cpp b/c++/word-ladder.cpp new file mode 100644 index 0000000..ff698d1 --- /dev/null +++ b/c++/word-ladder.cpp @@ -0,0 +1,37 @@ +class Solution { + public int ladderLength(String beginWord, String endWord, List wordList) { + Set set = new HashSet<>(wordList); + if(!set.contains(endWord)) return 0; + + Queue queue = new LinkedList<>(); + queue.add(beginWord); + + Set visited = new HashSet<>(); + queue.add(beginWord); + + int changes = 1; + + while(!queue.isEmpty()){ + int size = queue.size(); + for(int i = 0; i < size; i++){ + String word = queue.poll(); + if(word.equals(endWord)) return changes; + + for(int j = 0; j < word.length(); j++){ + for(int k = 'a'; k <= 'z'; k++){ + char arr[] = word.toCharArray(); + arr[j] = (char) k; + + String str = new String(arr); + if(set.contains(str) && !visited.contains(str)){ + queue.add(str); + visited.add(str); + } + } + } + } + ++changes; + } + return 0; + } +}