From b880456b7493454a3f5381ef78c474e9d1bfc887 Mon Sep 17 00:00:00 2001 From: arorarahul Date: Wed, 24 May 2017 14:58:01 +0530 Subject: [PATCH] new question added --- README.md | 10 +++++++- nextquestions.md | 1 + strings/a.exe | Bin 30975 -> 30970 bytes strings/question8.c | 41 +++++++++++++++++++++++++++++++++ strings/question9.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 strings/question8.c create mode 100644 strings/question9.c diff --git a/README.md b/README.md index 726b8bb..b2e1b82 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,8 @@ start and end of array is chosen(specifically where array is split again and aga - In case of string arr size should be measured using strlen and not using sizeof as sizeof also includes \0 - Sometimes a string can be merged with itself or the other to solve some algos. For eg: if one string is rotation of the other, concatenating one with itself can give a string where second string will be a substring in this string. - Store as many things as required to solve the algo in hash table as it is a structure +- It is always good to free memory assigned to hashTable after the end of the program +- Sometimes hashtable value can be decremented and not incremented to solve an algo for eg. finding anagram # Topic0: Programming Questions @@ -264,7 +266,8 @@ start and end of array is chosen(specifically where array is split again and aga - [Reverse a given string](/strings/question5.c) - [Check whether given string is palindrome or not](/strings/question6.c) - [Find the first non-repeating character in a given string](/strings/question7.c) -- [](/strings/question8.c) +- [Run length encoding](/strings/question8.c) +- [Check whether given two strings are anagrams of each other](/strings/question9.c) ## Some important concepts to solve algos better @@ -337,6 +340,11 @@ trees having random number of children not necessary equal are at same level but do not have same parent - O(1) means time complexity or space complexity is not dependent on the input size - One string is rotation of the other, if one of the rotations of one string matches the other one. +- Run length encoding means, traversing through the given character array and displaying which character +is repeating how many times along with the character as output. +Eg: SSMMMAAARRT => S2M3A3R2T1 +- Two strings are anagrams if they have same no of characters and they are composed of the same letters. +Even if there are repetitions, they are going to be same. # C Programming - Topic1: Introduction diff --git a/nextquestions.md b/nextquestions.md index 80ab94d..f20669b 100644 --- a/nextquestions.md +++ b/nextquestions.md @@ -57,3 +57,4 @@ TODO: - question19 method2 to be done later after strings (revisit the code for it given as pdf) - method1 of question21 and question22 complete - do binary search method for strings question1 +- question 8 strings to be done (see from the sol pdf) \ No newline at end of file diff --git a/strings/a.exe b/strings/a.exe index 2af04225699afb0b783a9ce73ff450a32168e4e1..d5d8a6063d413f6d131c1a023c1a1b3774a9b11c 100644 GIT binary patch delta 5436 zcmaKw3s6+o9mdaDc3B?FLxBWA!zBv_g3)cs{lD`*_oDX>{m&iR^Rg;-P46ouA-9*&-W!MLTGmEORoNlz8l6pt-9OM=6&+-c zs@Kv6_KNy3x`>@sC(>m0nR-6`hUqj#QT>^OTu`SGB+nUErI|%Hv+bH>x{mF~??rZ8 zla)LKP5YSIQ>`VWKg?((UFtN?>=;CR!v3py&@~t3{Zb2fVVb(zWZ=a~A~SOq3g*eg zPj`ZjyTp3V>EL{G+_NeSCHM0Z?h)2c0%rYc{(#WKDnh!3JU26No0;ZuLb~%fZ}iR& z3F`~Zp-0(BXe-^uR%;XInxH~^|Imow3uscD877GG^RTY-9A*aKT=pC7e5z)bw8?ao zeWh)2?S9A?erc$ur@ZT{ing&dHR{6?;PA7 zjd`mTDc7V6_5DWG94t4z|Dxvoiue4|)@*l*Hj;bGqQxQaWEawW36^|d z;@w{F72HWdbXceA@hDBvOChSW7YaVssnxvPi0ceAzr$VDsP;40l60EE4lSvmX7=Nf z#WN<~^LpoSO-Xd@`e-$g@VE$9EGy2@(>(TkP8Kz=zMK?V#m?m9YL1=odPCUR>=ZUL zH;nFM>AAV|G_&T;qdL}`n@=0rC%GmZiV1lss>ze=vAkNkgZ1T=!sK?|LKLSiokuUT z)k||JVLO-Fc{7$)ph@M+il~wGEz6=c?9#G)x`&15&%o+<|H^+H z2iSaLDgB(a8BHko*r=x+>>FbiJ!<6-uC9$uI=g=iAuH;er2z#O= zS<`^Vg|qo(32b}G8ub6|;u)&bT>1brSE^ZbX~fL+m=nJE#N)=-{&TxOv1ZLQ9lN9Kp+($#;Bks7(dAFtTx@>$m8+C z^v4IwM%j~ViXtz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+PS+WLT;B-;?h$MDc>7f3byi$P+&dP^2><7S$x-QBDPMhr;6OED-*& z$oGNM$9P?~(optp@6I>DQjrCBuQ zcJLh-Q`*N}AZmf^0pTT(C)5sujh-XE$PZRzKZUo_P z8p(=(u#O7>ZTDG)YyKF8uQ$G2(mozwv>`EWtq{^MOK;HFFT#9>;Lro2hIP;b{*8g? z1kKcF-)0Egdzm1kg8xzwE&NFPa}CHD!A~uBR3o4+_9?>Z0|yS9`Ed|lq+GlO;t^~= z1qp5||JJ$;Qj-<_LG@3N3xd^mASCd?>$jR@kMWHacKF;0BEDJp_x&EhQDMMJ3h_aZ z1%hVtEt*b{iI9drwN{J0iCoVNk9sde&X9-)_XiLi?oxV(K>CHbegGmqG*>~+37Sdx z3$j%3JUf=y$+{3S`~TAswP2PDv$2qd2eceS%+Mx~c*G|i;X6Uvpp`zKo(17cChfK( zAjRU7@n;~nkdHS&<_W|99pqeyTw#fuj>OeBHr16hwr;UiH#I)G)#4i7R2Jz{w|*^; QZrt2r4ou7ywar8S3yS6HK>z>% diff --git a/strings/question8.c b/strings/question8.c new file mode 100644 index 0000000..6c6d67a --- /dev/null +++ b/strings/question8.c @@ -0,0 +1,41 @@ +/* +Run length encoding + +Run length encoding means, traversing through the given character array and displaying which character +is repeating how many times along with the character as output. +Eg: SSMMMAAARRT => S2M3A3R2T1 + +Applications: File compression + +METHOD: +Just traverse and maintain a counter to solve the algo +Time complexity: O(n) +Space complexity: O(1) +*/ +#include +#include +#include + +void runLengthEncoding(char *arr, int size){ + int i,j=0; + // char *finalStr = (char *)malloc(sizeof(char)*(size*2 + 1)); + int counter = 1; + printf("%s\n", arr); + for(i=0;i +#include +#include +#define size 256 + +void checkAnagram(char *str1, char *str2, int l1, int l2){ + int hash[size] = {0}; + int i; + for(i=0;i 0){ + printf("two strings are NOT anagrams\n"); + return; + } + } + printf("two strings are anagrams of each other\n"); +} + +int main(){ + char str1[] = "heater"; + char str2[] = "reheaa"; + int l1 = strlen(str1); + int l2 = strlen(str2); + if(l1 != l2){ + printf("two strings not NOT anagrams\n"); + return 0; + } + checkAnagram(str1,str2, l1, l2); + return 0; +}