From fc4d1a718a4689bb77be1189e5333d924b31a7db Mon Sep 17 00:00:00 2001 From: Mahyar Date: Tue, 6 Nov 2018 21:47:18 -0500 Subject: [PATCH 1/5] folders renamed according to java repo and some code cleanups for the first 4 questions --- .../1.Is Unique/1. Is_unique.cpp | 70 -------------- .../1.Check_Permutation.cpp | 75 --------------- .../Palindrome Permutation.cpp | 64 ------------- Ch 1.Arrays And Strings/3.URLify/URLify.cpp | 47 --------- .../Q1_01_Is_Unique/isUnique.cpp | 69 ++++++++++++++ .../checkPermutation.cpp | 71 ++++++++++++++ .../Q1_03_URLify/URLify.cpp | 47 +++++++++ .../palindromePermutation.cpp} | 95 ++++++++++--------- .../5.One Away.cpp | 0 .../B.cpp} | 0 .../6.string_compression.cpp | 0 .../7.rotate_matrix.cpp | 0 .../8.zero_matrix.cpp | 0 .../9.string_rotation.cpp | 0 14 files changed, 235 insertions(+), 303 deletions(-) delete mode 100644 Ch 1.Arrays And Strings/1.Is Unique/1. Is_unique.cpp delete mode 100644 Ch 1.Arrays And Strings/2.Check Permutation/1.Check_Permutation.cpp delete mode 100644 Ch 1.Arrays And Strings/3.Palindrome Permutation/Palindrome Permutation.cpp delete mode 100644 Ch 1.Arrays And Strings/3.URLify/URLify.cpp create mode 100644 Ch 1.Arrays And Strings/Q1_01_Is_Unique/isUnique.cpp create mode 100644 Ch 1.Arrays And Strings/Q1_02_Check_Permutation/checkPermutation.cpp create mode 100644 Ch 1.Arrays And Strings/Q1_03_URLify/URLify.cpp rename Ch 1.Arrays And Strings/{4.Palindrome_Permutation/4-pallindrome-permutations.cpp => Q1_04_Palindrome_Permutation/palindromePermutation.cpp} (75%) rename Ch 1.Arrays And Strings/{5. One Away => Q1_05_One_Away}/5.One Away.cpp (100%) rename Ch 1.Arrays And Strings/{5.One_Away/5-one-edit-away.cpp => Q1_05_One_Away/B.cpp} (100%) rename Ch 1.Arrays And Strings/{6.String_Compression => Q1_06_String_Compression}/6.string_compression.cpp (100%) rename Ch 1.Arrays And Strings/{7.Rotate_matrix => Q1_07_Rotate_Matrix}/7.rotate_matrix.cpp (100%) rename Ch 1.Arrays And Strings/{8.Zero_matrix => Q1_08_Zero_Matrix}/8.zero_matrix.cpp (100%) rename Ch 1.Arrays And Strings/{9.String_rotation => Q1_09_String_Rotation}/9.string_rotation.cpp (100%) 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/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/5. One Away/5.One Away.cpp b/Ch 1.Arrays And Strings/Q1_05_One_Away/5.One Away.cpp similarity index 100% rename from Ch 1.Arrays And Strings/5. One Away/5.One Away.cpp rename to Ch 1.Arrays And Strings/Q1_05_One_Away/5.One Away.cpp diff --git a/Ch 1.Arrays And Strings/5.One_Away/5-one-edit-away.cpp b/Ch 1.Arrays And Strings/Q1_05_One_Away/B.cpp similarity index 100% rename from Ch 1.Arrays And Strings/5.One_Away/5-one-edit-away.cpp rename to Ch 1.Arrays And Strings/Q1_05_One_Away/B.cpp diff --git a/Ch 1.Arrays And Strings/6.String_Compression/6.string_compression.cpp b/Ch 1.Arrays And Strings/Q1_06_String_Compression/6.string_compression.cpp similarity index 100% rename from Ch 1.Arrays And Strings/6.String_Compression/6.string_compression.cpp rename to Ch 1.Arrays And Strings/Q1_06_String_Compression/6.string_compression.cpp 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 From f15b07ae4db82f8e6cd327a276eeb511fd7aaa40 Mon Sep 17 00:00:00 2001 From: Mahyar Date: Tue, 6 Nov 2018 23:21:13 -0500 Subject: [PATCH 2/5] the redundant file was deleted as well as indenting was modified --- .../Q1_05_One_Away/5.One Away.cpp | 41 --------- .../Q1_05_One_Away/oneAway | Bin 0 -> 14504 bytes .../Q1_05_One_Away/oneAway.cpp | 82 ++++++++++++++++++ .../Q1_05_One_Away/{B.cpp => oneAway.cpp~} | 0 4 files changed, 82 insertions(+), 41 deletions(-) delete mode 100644 Ch 1.Arrays And Strings/Q1_05_One_Away/5.One Away.cpp create mode 100755 Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway create mode 100644 Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp rename Ch 1.Arrays And Strings/Q1_05_One_Away/{B.cpp => oneAway.cpp~} (100%) diff --git a/Ch 1.Arrays And Strings/Q1_05_One_Away/5.One Away.cpp b/Ch 1.Arrays And Strings/Q1_05_One_Away/5.One Away.cpp deleted file mode 100644 index 7ca6f1e..0000000 --- a/Ch 1.Arrays And Strings/Q1_05_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;iy|f3{ErxB|s?}uan7;jONeG9Sp98 zM&qy;aU>6mQv0Y_e~Q&AvR0L9Ux`METH8jgKL4t`r)VN52>zt6O5V56IXk&CnbBfj z4=c0J-uv78?6dE==bn4+;W}^S!VHH)aB+(B1#x*fN>ZW~I|_6Kq(l^p(fBpQR51#? zOUjhKL=k9J8PKkoW^1_{mSzsWygFIco(w#0cfz){Q-@JC~S^cxmE-hS^hyB;&{uYYm#O#hWP@BU*x;V0sc zuNy|Btdoq&#H~4Hs+Rhf+BZ_Drlyml!Tc2XTM5u*b* zSuifcR&8MVbQ+6uglX0_HaD4J%in65rZ6vDV=fKWg~C>_wXDG(4hO@+tXOujSsQHC z;$;_?H8eK`m-%ZNf=ZWKv#QE!2v-EEtir-Ty}#A8TKypl0&kTcKi;KK%&PH+Ljkin zY_$gcjTOtxWd)|UYOcBTqD58KT+GcY8)gtMv zywJNwGFEY@Icx$SoL$irvXtDcYil!Gg011^CVxZ7T5GN;P~bEzqz$>O(7WWKz|xDV zW+72%I$elg=K$8Tl{IAn9#aNBEI-~Dhf{I9mZ3IpBX#Sdm_naC;Ht+HkZnj@CL_-s@|6ScZPzzh>tD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KHGEuZ`Ae+v_sy1 zVQT0OlRYi9lkIPTj35*r72bDc%KMW_JM#L+ps?HXehyRlje?EIzpgX=agzPezK?z% zQ9 z_8*4rd0&d@rQcw`2YP$^ynn^?x1_X{* z|G%B&d~^FuKY$Ir;;=pM+wai-uZeNwn+o$xk3vQcBsM{|{lC%AcBssgYQ@(fX&@Mx9ExMy8 tgPn~(?l<>~?oWVXJ-Ha literal 0 HcmV?d00001 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_05_One_Away/B.cpp b/Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp~ similarity index 100% rename from Ch 1.Arrays And Strings/Q1_05_One_Away/B.cpp rename to Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp~ From 4351424e62144906071513a2531d942d717ba3da Mon Sep 17 00:00:00 2001 From: Mahyar Date: Tue, 6 Nov 2018 23:29:24 -0500 Subject: [PATCH 3/5] binary file and Emacs backup file are deleted --- .../Q1_05_One_Away/oneAway | Bin 14504 -> 0 bytes .../Q1_05_One_Away/oneAway.cpp~ | 82 ------------------ 2 files changed, 82 deletions(-) delete mode 100755 Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway delete mode 100644 Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp~ diff --git a/Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway b/Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway deleted file mode 100755 index 9a26b13b9d035a2949d2cfb5c56479d2b3b2c447..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14504 zcmcIr4Rlo1wLX){kN{y4B2*q!xmxX$hl~jj2>y|f3{ErxB|s?}uan7;jONeG9Sp98 zM&qy;aU>6mQv0Y_e~Q&AvR0L9Ux`METH8jgKL4t`r)VN52>zt6O5V56IXk&CnbBfj z4=c0J-uv78?6dE==bn4+;W}^S!VHH)aB+(B1#x*fN>ZW~I|_6Kq(l^p(fBpQR51#? zOUjhKL=k9J8PKkoW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REArmJ8c`&Y;hhu(C;Rd9#=35+)^Qyyf`Yc#d4$RtcvssTjjm{kok2`g5nl<6D zt=n|CZbVPMT|zLJ@<{3&X+D$N?45yj-4z?jgr1{?fjpldA+ zmhi|?=Rws|*9;b?5AjRSB!HUbW%LB=^;4+67EYLhcN}UAwgrMMmWh5~FiU6Z0i}F! z{fYn#(XhWU6o6f`MGZniqr`&&FFbfuHsbN8p2`2nFTE$xQWDETS$JQ7Y0~R~#A>J{ z-@`BM8L>U@A22l}YWN4shlElH&TemMM@&ykK+`)&VkrlU4}#6yKJP~`WqT}tWa0fR z&5A{+&=vsOcj*dK{{IVD4#~c=J@3D*fE-KGA$#86V9NV;fXG6*wwKGm&>k7L&-)-u z&(vU!pK(mDgr4@?nCJZxrtR9E;-@${ek~UC9!oi8d)`-J%KLuYKHGEuZ`Ae+v_sy1 zVQT0OlRYi9lkIPTj35*r72bDc%KMW_JM#L+ps?HXehyRlje?EIzpgX=agzPezK?z% zQ9 z_8*4rd0&d@rQcw`2YP$^ynn^?x1_X{* z|G%B&d~^FuKY$Ir;;=pM+wai-uZeNwn+o$xk3vQcBsM{|{lC%AcBssgYQ@(fX&@Mx9ExMy8 tgPn~(?l<>~?oWVXJ-Ha 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~ deleted file mode 100644 index 69a3d54..0000000 --- a/Ch 1.Arrays And Strings/Q1_05_One_Away/oneAway.cpp~ +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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; - -} From f5275d237243b777382f352f8fc181f21436a78b Mon Sep 17 00:00:00 2001 From: Mahyar Date: Tue, 6 Nov 2018 23:33:13 -0500 Subject: [PATCH 4/5] emacs files added to gitignore --- .gitignore | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) 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 From 1f9f5deb5a65cf092883cec234e0525f387bbf70 Mon Sep 17 00:00:00 2001 From: Mahyar Date: Wed, 7 Nov 2018 00:06:31 -0500 Subject: [PATCH 5/5] changed the name of Q06 and its indenting --- .../6.string_compression.cpp | 52 ------------------ .../stringCompression.cpp | 53 +++++++++++++++++++ 2 files changed, 53 insertions(+), 52 deletions(-) delete mode 100644 Ch 1.Arrays And Strings/Q1_06_String_Compression/6.string_compression.cpp create mode 100644 Ch 1.Arrays And Strings/Q1_06_String_Compression/stringCompression.cpp diff --git a/Ch 1.Arrays And Strings/Q1_06_String_Compression/6.string_compression.cpp b/Ch 1.Arrays And Strings/Q1_06_String_Compression/6.string_compression.cpp deleted file mode 100644 index b9d70e6..0000000 --- a/Ch 1.Arrays And Strings/Q1_06_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_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; +}