diff --git a/.gitignore b/.gitignore index b8bd026..06e7ff6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,52 @@ +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# Prerequisites +*.d + # Compiled Object files *.slo *.lo @@ -15,6 +64,7 @@ # Fortran module files *.mod +*.smod # Compiled Static libraries *.lai @@ -25,4 +75,4 @@ # Executables *.exe *.out -*.app +*.app \ No newline at end of file diff --git a/Ch 1.Arrays And Strings/1.Is Unique/1. Is_unique.cpp b/Ch 1.Arrays And Strings/1.Is Unique/1. Is_unique.cpp deleted file mode 100644 index 6118cc1..0000000 --- a/Ch 1.Arrays And Strings/1.Is Unique/1. Is_unique.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include -#include // for sort() - -using namespace std; - -bool isUniqueChars(const string &str){ - if (str.length() > 128){ - return false; - } - vector char_set(128); - for (int i = 0; i < str.length(); i++){ - int val = str[i]; - if (char_set[val]){ - return false; - } - char_set[val] = true; - } - return true; -} - -bool isUniqueChars_bitvector(const string &str) { - //Reduce space usage by a factor of 8 using bitvector. - //Each boolean otherwise occupies a size of 8 bits. - bitset<256> bits(0); - for(int i = 0; i < str.length(); i++) { - int val = str[i]; - if(bits.test(val) > 0) { - return false; - } - bits.set(val); - } - return true; -} -bool isUniqueChars_noDS( string str) { - - sort(str.begin(), str.end()); // O(nlogn) sort from - - bool noRepeat = true; - for ( int i = 0 ; i < str.size() - 1; i++){ - if ( str[i] == str[i+1] ){ - noRepeat = false; - break; - } - } - - return noRepeat; -} - -int main(){ - vector words = {"abcde", "hello", "apple", "kite", "padle"}; - for (auto word : words) - { - cout << word << string(": ") << boolalpha << isUniqueChars(word) < -#include -#include -#include -using namespace std; -bool arePermutation(string str1,string str2) -{ - // Get lengths of both strings - int n1 = str1.length(); - int n2 = str2.length(); - - // If length of both strings is not same, then they - // cannot be anagram - if (n1 != n2) - return false; - - // Sort both strings - sort(str1.begin(), str1.end()); - sort(str2.begin(), str2.end()); - // Compare sorted strings - for (int i = 0; i < n1; i++) - if (str1[i] != str2[i]) - return false; - - return true; -} - -bool arePermutation_2(const string &str1, const string &str2) { - if(str1.length() != str2.length()) - return false; - int count[256]={0}; - for(int i = 0; i < str1.length(); i++) { - int val = str1[i]; - count[val]++; - } - for(int i = 0; i < str2.length(); i++) { - int val = str2[i]; - count[val]--; - if(count[val]<0) - return false; - } - return true; -} -int main() { -// Test Method 1 - Using sort - cout << "Method 1 - Using sort" << endl; - string str1 = "testest"; - string str2 = "estxest"; - if(arePermutation(str1, str2)) - cout << str1 <<" and " << str2 << " are permutation of each other" << endl; - else - cout << str1 <<" and " << str2 << " are not permutation of each other" << endl; - str1 = "hello"; - str2 = "oellh"; - if(arePermutation(str1, str2)) - cout << str1 <<" and " << str2 << " are permutation of each other" << endl; - else - cout << str1 <<" and " << str2 << " are not permutation of each other" << endl; - -//Test Method 2 - Using character count - cout << "Method 2 - Using character count" << endl; - str1 = "testest"; - str2 = "estxest"; - if(arePermutation_2(str1, str2)) - cout << str1 <<" and " << str2 << " are permutation of each other" << endl; - else - cout << str1 <<" and " << str2 << " are not permutation of each other" << endl; - str1 = "hello"; - str2 = "oellh"; - if(arePermutation_2(str1, str2)) - cout << str1 <<" and " << str2 << " are permutation of each other" << endl; - else - cout << str1 <<" and " << str2 << " are not permutation of each other" << endl; - return 0; -} diff --git a/Ch 1.Arrays And Strings/3.Palindrome Permutation/Palindrome Permutation.cpp b/Ch 1.Arrays And Strings/3.Palindrome Permutation/Palindrome Permutation.cpp deleted file mode 100644 index f9a5511..0000000 --- a/Ch 1.Arrays And Strings/3.Palindrome Permutation/Palindrome Permutation.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include - -using namespace std; - -int getCharNumber(const char & c){ - int a = (int) 'a'; - int z = (int) 'z'; - int A = (int) 'A'; - int Z = (int) 'Z'; - int val = (int) c; - if(a <= val && val <= z){ - return val - 'a'; - } - else if(A <= val && val <= Z){ - return val - 'A'; - } - return -1; -} - - -vector buildCharFrequencyTable(string phrase){ - vector table(getCharNumber('z') - getCharNumber('a') + 1, 0); - for(char &c : phrase){ - int x = getCharNumber(c); - if(x != -1){ - table[x]++; - } - } - return table; -} - - -bool checkMaxOneOdd(vector &table) -{ - bool foundOdd = false; - for (auto count : table) - { - if (count % 2 == 1) - { - if (foundOdd) - { - return false; - } - foundOdd = true; - } - } - return true; -} - -bool isPermutationOfPalindrome(const string &phrase) -{ - vector table = buildCharFrequencyTable(phrase); - return checkMaxOneOdd(table); -} - -int main(int argc, const char *argv[]) -{ - string pali = "Rats live on no evil star"; - string isPermutation = isPermutationOfPalindrome(pali) ? "yes" : "no"; - cout << isPermutation << endl; - return 0; -} diff --git a/Ch 1.Arrays And Strings/3.URLify/URLify.cpp b/Ch 1.Arrays And Strings/3.URLify/URLify.cpp deleted file mode 100644 index 98f67b8..0000000 --- a/Ch 1.Arrays And Strings/3.URLify/URLify.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Cracking the coding interview Edition 6 - * Problem 1.3 URLify --> Replace all the spaces in a string with '%20'. - * Assumption : We have enough space to accomodate addition chars - * Preferebly in place - */ - -#include -#include - -/* - * Function : urlify - * Args : string long enough to accomodate extra chars + true len - * Return : void (in place transformation of string) - */ - -void urlify(char *str, int len) -{ - int numOfSpaces = 0; - int i = 0, j = 0; - for ( i = 0; i < len; ++i ) { - if (str[i] == ' ') { - ++numOfSpaces; - } - } - - int extendedLen = len + 2 * numOfSpaces; - i = extendedLen - 1; - for( j = len - 1; j >= 0; --j ) { - if ( str[j] != ' ' ) { - str[i--] = str[j]; - } else { - str[i--] = '0'; - str[i--] = '2'; - str[i--] = '%'; - } - } -} - -int main() -{ - char str[] = "Mr John Smith "; //String with extended length ( true length + 2* spaces) - std::cout << "Actual string : " << str << std::endl; - urlify(str, 13); //Length of "Mr John Smith" = 13 - std::cout << "URLified string : " << str << std::endl; - return 0; -} diff --git a/Ch 1.Arrays And Strings/5. One Away/5.One Away.cpp b/Ch 1.Arrays And Strings/5. One Away/5.One Away.cpp deleted file mode 100644 index 7ca6f1e..0000000 --- a/Ch 1.Arrays And Strings/5. One Away/5.One Away.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -using namespace std; - -bool isOneAway(string s1, string s2){ - string a,b; - a = s1.length() >= s2.length() ? s1 : s2; - b = s1.length() < s2.length() ? s1 : s2; - int len1, len2; - len1 = a.length(); - len2 = b.length(); - if(abs(len1-len2)>1) - return false; - - bool flag = false; - for(int i=0,j=0;i possible edit is replace. - * If there are more than one mismatch, return false - * - * 2. Case when One string is bigger than another - * Smaller string ------------> Bigger String - * insert - * delete - * smaller string <----------- Bigger String - * - * Idea is check if there are more than one mismatch discounting the already - * difference in the string. Therefore for first mismatch we do not move the pointer - * pointing to smaller string, and then expect it to match from next char of bigger - * string. - */ - - - -#include -#include -#include - - -bool oneEditAway( const std::string & str1, const std::string & str2 ) -{ - if ( std::abs( int(str1.length()) - int(str2.length())) > 1 ) { - return false; - } - - int len1 = str1.length(); - int len2 = str2.length(); - std::string smaller = len1 < len2 ? str1 : str2; - std::string bigger = len1 < len2 ? str2 : str1; - - unsigned int i = 0, j = 0; - bool mismatchDone = false; - while ( i < smaller.length() && j < bigger.length() ) - { - if ( smaller[i] != bigger[j] ) { - if (mismatchDone) { - return false; - } - mismatchDone = true; - if ( len1 == len2 ) { - ++i; //case of replace - } - } else { - ++i; //move short pointer if its a match, dont move it in case of first mismatch - } - ++j; //always move long string pointer. - } - return true; -} - - -void translate( bool result, const std::string str1, const std::string str2 ) -{ - if (result == true ) { - std::cout << str1 << " and " << str2 << " are one edit away\n"; - } else { - std::cout << str1 << " and " << str2 << " are not one edit away\n"; - } -} - -int main() -{ - translate ( oneEditAway("pale", "ple"), "pale", "ple" ); - translate ( oneEditAway("pales", "pale"), "pales", "pale" ); - translate ( oneEditAway("pale", "pales"), "pale", "pales" ); - translate ( oneEditAway("pale", "bale"), "pale", "bale" ); - translate ( oneEditAway("pale", "bake"), "pale", "bake" ); - return 0; - -} diff --git a/Ch 1.Arrays And Strings/6.String_Compression/6.string_compression.cpp b/Ch 1.Arrays And Strings/6.String_Compression/6.string_compression.cpp deleted file mode 100644 index b9d70e6..0000000 --- a/Ch 1.Arrays And Strings/6.String_Compression/6.string_compression.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Cracking the coding interview edition 6 - * Problem 1-6 Implement a method to perform basic string compression. - * Example string aabcccccaaa should be compressed to a2b1c5a3, - * however if compressed string is bigger than original string, return original string - */ - -#include -#include - - -std::string compress(std::string str) -{ - size_t original_length = str.length(); - if (original_length < 2) { - return str; - } - std::string out{""}; - int count = 1; - for( size_t i = 1; i < original_length; ++i ) { - if (str[i-1] == str[i]) { - ++count; - } else { - out += str[i-1]; - out += std::to_string(count); - count = 1; - } - if (out.length() >= original_length) { - return str; - } - } - out += str[original_length-1]; - out += std::to_string(count); - if (out.length() >= original_length) { - return str; - } - return out; -} - -int main() -{ - std::string str, out; - std::cout << "Enter a string:\n"; - std::cin >> str; - out = compress(str); - if (str.compare(out)) { - std::cout << str << " can be compressed to " << out << std::endl; - } else { - std::cout << str << " can not be compressed\n"; - } - return 0; -} diff --git a/Ch 1.Arrays And Strings/Q1_01_Is_Unique/isUnique.cpp b/Ch 1.Arrays And Strings/Q1_01_Is_Unique/isUnique.cpp new file mode 100644 index 0000000..9814b97 --- /dev/null +++ b/Ch 1.Arrays And Strings/Q1_01_Is_Unique/isUnique.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include // for sort() + + +bool isUniqueChars(const std::string &str){ + if (str.length() > 128){ + return false; + } + std::vector charSet(128); + for (int i = 0; i < str.length(); i++){ + int val = str[i]; + if (charSet[val]){ + return false; + } + charSet[val] = true; + } + return true; +} + +bool isUniqueCharsBitVector(const std::string &str) { + //Reduce space usage by a factor of 8 using bitvector. + //Each boolean otherwise occupies a size of 8 bits. + std::bitset<256> bits(0); + for(int i = 0; i < str.length(); i++) { + int val = str[i]; + if(bits.test(val) > 0) { + return false; + } + bits.set(val); + } + return true; +} +bool isUniqueCharsNoDataStructure( std::string str) { + + std::sort(str.begin(), str.end()); // O(nlogn) sort from + + bool noRepeat = true; + for ( int i = 0 ; i < str.size() - 1; i++){ + if ( str[i] == str[i+1] ){ + noRepeat = false; + break; + } + } + + return noRepeat; +} + +int main(){ + std::vector words = {"abcde", "hello", "apple", "kite", "padle"}; + for (auto word : words) + { + std::cout< +#include +#include +#include + +bool arePermutation(std::string str1, std::string str2) +{ + // Get lengths of both strings + int n1 = str1.length(); + int n2 = str2.length(); + + // If length of both strings is not same, then they + // cannot be anagram + if (n1 != n2){ + return false; + } + + // Sort both strings + sort(str1.begin(), str1.end()); + sort(str2.begin(), str2.end()); + // Compare sorted strings + for (int i = 0; i < n1; i++){ + if (str1[i] != str2[i]){ + return false; + } + } + + return true; +} + +bool arePermutation2(const std::string &str1, const std::string &str2) { + if(str1.length() != str2.length()){ + return false; + } + int count[256]={0}; + for(int i = 0; i < str1.length(); i++) { + int val = str1[i]; + count[val]++; + } + for(int i = 0; i < str2.length(); i++) { + int val = str2[i]; + count[val]--; + if(count[val]<0) + return false; + } + return true; +} +int main() { + // Test Method 1 - Using sort + std::cout<<"Method 1 - Using sort"<< std::endl; + std::string str1 = "testest"; + std::string str2 = "estxest"; + + std::cout< Replace all the spaces in a string with '%20'. + * Assumption : We have enough space to accomodate addition chars + * Preferebly in place + */ + +#include +#include + +/* + * Function : urlify + * Args : string long enough to accomodate extra chars + true len + * Return : void (in place transformation of string) + */ + +void urlify(std::string &str, const int &len) +{ + int numOfSpaces = 0; + int i = 0, j = 0; + for ( i = 0; i < len; i++ ) { + if (str[i] == ' ') { + numOfSpaces++; + } + } + + int extendedLen = len + 2 * numOfSpaces; + i = extendedLen - 1; + for( j = len - 1; j >= 0; j-- ) { + if ( str[j] != ' ' ) { + str[i--] = str[j]; + } else { + str[i--] = '0'; + str[i--] = '2'; + str[i--] = '%'; + } + } +} + +int main() +{ + std::string str = "Mr John Smith "; //String with extended length ( true length + 2* spaces) + std::cout<<"Actual string : "< - +#include /* * Helper routine to return an frequency Table index @@ -21,35 +21,37 @@ int getCharIndex( char c ) { - int idx = -1; - if ( c >= 'a' && c <= 'z' ) + int idx = -1; + if ( c >= 'a' && c <= 'z' ) { - idx = c - 'a'; + idx = c - 'a'; } - else if ( c >= 'A' && c <= 'Z' ) + else if ( c >= 'A' && c <= 'Z' ) { - idx = c - 'A'; + idx = c - 'A'; } - return idx; + return idx; } /* * Function : countFrequency * Args : input string, an array of int - * Return : Void, array of int will populate each letter's frequency in string. + * Return : vector of int will populate each letter's frequency in string. */ -void countFrequency( const std::string & str, int *frequency ) +std::vector countFrequency( const std::string & str) { - int idx; - for (const char & c : str) + std::vector frequency(26,0); + int idx; + for (const char & c : str) { - idx = getCharIndex(c); - if ( idx != -1 ) + idx = getCharIndex(c); + if ( idx != -1 ) { - ++frequency[idx]; + frequency[idx]++; } } + return frequency; } @@ -62,22 +64,21 @@ void countFrequency( const std::string & str, int *frequency ) bool isPermutationOfPallindrome1( const std::string & str ) { - int frequency[ 26 ] = { 0 }; - countFrequency( str, frequency ); + std::vector frequency = countFrequency(str); /* * We will check here that letter frequencies are all even or all even except one odd. */ - bool oddAppeared = false; - std::cout << std::endl; - for ( int i = 0 ; i < 26; ++i ) { - if ( frequency[i] % 2 && oddAppeared ) { - return false; - } else if ( frequency[i] % 2 && !oddAppeared ) { - oddAppeared = true; - } + bool oddAppeared = false; + std::cout << std::endl; + for ( int i = 0 ; i < 26; ++i ) { + if ( frequency[i] % 2 && oddAppeared ) { + return false; + } else if ( frequency[i] % 2 && !oddAppeared ) { + oddAppeared = true; } - return true; + } + return true; } @@ -90,24 +91,24 @@ bool isPermutationOfPallindrome1( const std::string & str ) bool isPermutationOfPallindrome2( const std::string & str ) { - int oddCount = 0; - int frequency[26] = { 0 }; - int idx = 0; - for ( const char & c : str ) + int oddCount = 0; + int frequency[26] = { 0 }; + int idx = 0; + for ( const char & c : str ) { - idx = getCharIndex(c); - if ( idx != -1 ) + idx = getCharIndex(c); + if ( idx != -1 ) { - ++frequency[idx]; - if ( frequency[idx] % 2 ) + frequency[idx]++; + if ( frequency[idx] % 2 ) { - ++oddCount; + ++oddCount; } else { - --oddCount; - } + oddCount--; + } } } - return (oddCount <= 1); + return (oddCount <= 1); } /* @@ -126,18 +127,18 @@ bool isPermutationOfPallindrome2( const std::string & str ) int toggle( int bitVector, int index ) { - if ( index < 0 ) - return bitVector; - - int mask = 1 << index; - //if bit is not set - if ( (bitVector & mask ) == 0 ) + if ( index < 0 ){ + return bitVector; + } + int mask = 1 << index; + //if bit is not set + if ( (bitVector & mask ) == 0 ) { - bitVector |= mask; + bitVector |= mask; } else { //if bit is set - bitVector &= ~mask; - } - return bitVector; + bitVector &= ~mask; + } + return bitVector; } /* diff --git a/Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp b/Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp new file mode 100644 index 0000000..5bc5aba --- /dev/null +++ b/Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp @@ -0,0 +1,82 @@ +/* + * Problem: There are three possible edits that can be performed on a string. + * 1. Insert a char. + * 2. Delete a char. + * 3. Replace a char. + * + * Given two strings, determine if they are one or 0 edit away. + * + * Approach : + * 1. Case when strings are of some length --> possible edit is replace. + * If there are more than one mismatch, return false + * + * 2. Case when One string is bigger than another + * Smaller string ------------> Bigger String + * insert + * delete + * smaller string <----------- Bigger String + * + * Idea is check if there are more than one mismatch discounting the already + * difference in the string. Therefore for first mismatch we do not move the pointer + * pointing to smaller string, and then expect it to match from next char of bigger + * string. + */ + + + +#include +#include +#include + + +bool oneEditAway( const std::string & str1, const std::string & str2 ) +{ + if ( std::abs( int(str1.length()) - int(str2.length())) > 1 ) { + return false; + } + + int len1 = str1.length(); + int len2 = str2.length(); + std::string smaller = len1 < len2 ? str1 : str2; + std::string bigger = len1 < len2 ? str2 : str1; + + unsigned int i = 0, j = 0; + bool mismatchDone = false; + while ( i < smaller.length() && j < bigger.length() ) + { + if ( smaller[i] != bigger[j] ) { + if (mismatchDone) { + return false; + } + mismatchDone = true; + if ( len1 == len2 ) { + ++i; //case of replace + } + } else { + ++i; //move short pointer if its a match, dont move it in case of first mismatch + } + ++j; //always move long string pointer. + } + return true; +} + + +void translate( bool result, const std::string str1, const std::string str2 ) +{ + if (result == true ) { + std::cout << str1 << " and " << str2 << " are one edit away\n"; + } else { + std::cout << str1 << " and " << str2 << " are not one edit away\n"; + } +} + +int main() +{ + translate ( oneEditAway("pale", "ple"), "pale", "ple" ); + translate ( oneEditAway("pales", "pale"), "pales", "pale" ); + translate ( oneEditAway("pale", "pales"), "pale", "pales" ); + translate ( oneEditAway("pale", "bale"), "pale", "bale" ); + translate ( oneEditAway("pale", "bake"), "pale", "bake" ); + return 0; + +} diff --git a/Ch 1.Arrays And Strings/Q1_06_String_Compression/stringCompression.cpp b/Ch 1.Arrays And Strings/Q1_06_String_Compression/stringCompression.cpp new file mode 100644 index 0000000..daf2f35 --- /dev/null +++ b/Ch 1.Arrays And Strings/Q1_06_String_Compression/stringCompression.cpp @@ -0,0 +1,53 @@ +/* + * Cracking the coding interview edition 6 + * Problem 1-6 Implement a method to perform basic string compression. + * Example string aabcccccaaa should be compressed to a2b1c5a3, + * however if compressed string is bigger than original string, return original string + */ + +#include +#include + + +std::string compress(std::string str) +{ + size_t original_length = str.length(); + if (original_length < 2) { + return str; + } + std::string out{""}; + int count = 1; + for( size_t i = 1; i < original_length; ++i ) { + if (str[i-1] == str[i]) { + ++count; + } else { + out += str[i-1]; + out += std::to_string(count); + count = 1; + } + + if (out.length() >= original_length) { + return str; + } + } + out += str[original_length-1]; + out += std::to_string(count); + if (out.length() >= original_length) { + return str; + } + return out; +} + +int main() +{ + std::string str, out; + std::cout << "Enter a string:\n"; + std::cin >> str; + out = compress(str); + if (str.compare(out)) { + std::cout << str << " can be compressed to " << out << std::endl; + } else { + std::cout << str << " can not be compressed\n"; + } + return 0; +} diff --git a/Ch 1.Arrays And Strings/7.Rotate_matrix/7.rotate_matrix.cpp b/Ch 1.Arrays And Strings/Q1_07_Rotate_Matrix/7.rotate_matrix.cpp similarity index 100% rename from Ch 1.Arrays And Strings/7.Rotate_matrix/7.rotate_matrix.cpp rename to Ch 1.Arrays And Strings/Q1_07_Rotate_Matrix/7.rotate_matrix.cpp diff --git a/Ch 1.Arrays And Strings/8.Zero_matrix/8.zero_matrix.cpp b/Ch 1.Arrays And Strings/Q1_08_Zero_Matrix/8.zero_matrix.cpp similarity index 100% rename from Ch 1.Arrays And Strings/8.Zero_matrix/8.zero_matrix.cpp rename to Ch 1.Arrays And Strings/Q1_08_Zero_Matrix/8.zero_matrix.cpp diff --git a/Ch 1.Arrays And Strings/9.String_rotation/9.string_rotation.cpp b/Ch 1.Arrays And Strings/Q1_09_String_Rotation/9.string_rotation.cpp similarity index 100% rename from Ch 1.Arrays And Strings/9.String_rotation/9.string_rotation.cpp rename to Ch 1.Arrays And Strings/Q1_09_String_Rotation/9.string_rotation.cpp