Posts

Showing posts with the label Moderate

Word Transform

/*** 18.10 Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary. EXAMPLE input: DAMP, LIKE output: DAMP->LAMP->LIMP->LIME->LIKE ***/ #include <iostream> #include <string> #include <vector> using namespace std; bool isword(string s) {        string dic[] = { "damp" , "lamp" , "limp" , "lime" , "like" , "lake" };        int sz = sizeof (dic) / sizeof (string);        int i = 0;        while (i<sz)        {               if (dic[i] == s)               {            ...

BST to Linked List

/**** 17.13 the data structure BiNode could be used to represent both a binary tree (where node 1 is the left node and node 2 is the right node) or a doubly linked list(where node1 is the previous node and node 2 is the next node) Implement a method to convert a binary search tree(implemented with BiNode) into a doubly linked list. The value should be kept in order and the operation should be performed in place complexity O(N^2) N is the number of nodes *****/ #include <iostream> using namespace std; struct BiNode {        BiNode *node1, *node2;        int val; }; BiNode * getleftTail(BiNode *left) {        BiNode *tail;        tail = left;        while (tail->node2 != nullptr )        {               tail = tail->node2;  ...

Pair Sum

/*** 17.12 Design an algorithm to find all pairs of integers within an array which sum to a specified value. ***/ #include <iostream> #include <map> #include <vector> using namespace std; vector<vector< int >> pairofgivenSum( int *a, int sz, int sum) {        map< int , int > mymap;        map< int , int > :: iterator it;        vector<vector< int >> pairs;        for ( int i = 0; i < sz; i++) {               it = mymap.find(sum - a[i]);               if (it != mymap.end()) {                      vector< int > v;           ...

Frequency Word

/*** 17.9 Design a method to find the frequency of occurrences of any given word in a book. IDEA: map a book in a hash table or a map. key is word and value is the frequency ***/ #include <iostream> #include <fstream> #include <map> #include <string> using namespace std; map<string, int > mapbook( char *book) {        ifstream mybook(book);        string str;        map<string, int > dic;        map<string, int > :: iterator it;        while (mybook.good()) {               mybook>>str;               it = dic.find(str);               if (it != dic.end()) {        ...

Largest Sum

/*** You are given an array of integers (both positive and negative). Find the contigous sequence with the largest sum. Return the sum. EXAMPLE: Input: 2, -8, 3, -2, 4, -10 Ouput: 5 (3, -2, 4) ***/ #include <iostream> #include <algorithm> using namespace std; int getMax( int a, int b, int c) {        return max(a, max(b, c)); } int middleSum( int *a, int begin, int end) {        int midSum;        int mid = (begin + end) / 2;        int left = 0, right = 0;        midSum = a[mid] + a[mid+1];        for ( int i = begin; i<mid; i++) {               if (left < 0) left = a[i];               else left += a[i];        } ...