-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4f699c
commit b880456
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
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<size;i++){ | ||
// finalStr[j++] = arr[i]; | ||
if(arr[i] == arr[i+1]){ | ||
counter++; | ||
}else{ | ||
printf("%c%d", arr[i],counter); | ||
counter=1; | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
char arr[] = "SSMMMAAARRT"; | ||
int length = strlen(arr); | ||
|
||
runLengthEncoding(arr,length); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Check whether given two strings are anagrams of each other | ||
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. | ||
METHOD1: | ||
sort and scan | ||
Time complexity: O(nlogn) | ||
Space complexity: O(1) //for inplace sorting | ||
METHOD2: | ||
use hash table and increment count for first string and decrement for second. In the end all count | ||
values should be zero. | ||
Time complexity: O(n) | ||
Space complexity: O(1) //256 not dependent on input | ||
*/ | ||
//sorting method not done as it is very basic | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#define size 256 | ||
|
||
void checkAnagram(char *str1, char *str2, int l1, int l2){ | ||
int hash[size] = {0}; | ||
int i; | ||
for(i=0;i<l1;i++){ | ||
hash[str1[i]]++; | ||
} | ||
for(i=0;i<l2;i++){ | ||
hash[str2[i]]--; | ||
} | ||
for(i=0;i<size;i++){ | ||
if(hash[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; | ||
} |