Skip to content

Commit b880456

Browse files
committed
new question added
1 parent f4f699c commit b880456

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ start and end of array is chosen(specifically where array is split again and aga
105105
- In case of string arr size should be measured using strlen and not using sizeof as sizeof also includes \0
106106
- 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.
107107
- Store as many things as required to solve the algo in hash table as it is a structure
108+
- It is always good to free memory assigned to hashTable after the end of the program
109+
- Sometimes hashtable value can be decremented and not incremented to solve an algo for eg. finding anagram
108110

109111
# Topic0: Programming Questions
110112

@@ -264,7 +266,8 @@ start and end of array is chosen(specifically where array is split again and aga
264266
- [Reverse a given string](/strings/question5.c)
265267
- [Check whether given string is palindrome or not](/strings/question6.c)
266268
- [Find the first non-repeating character in a given string](/strings/question7.c)
267-
- [](/strings/question8.c)
269+
- [Run length encoding](/strings/question8.c)
270+
- [Check whether given two strings are anagrams of each other](/strings/question9.c)
268271

269272

270273
## Some important concepts to solve algos better
@@ -337,6 +340,11 @@ trees having random number of children not necessary equal
337340
are at same level but do not have same parent
338341
- O(1) means time complexity or space complexity is not dependent on the input size
339342
- One string is rotation of the other, if one of the rotations of one string matches the other one.
343+
- Run length encoding means, traversing through the given character array and displaying which character
344+
is repeating how many times along with the character as output.
345+
Eg: SSMMMAAARRT => S2M3A3R2T1
346+
- Two strings are anagrams if they have same no of characters and they are composed of the same letters.
347+
Even if there are repetitions, they are going to be same.
340348

341349
# C Programming - Topic1: Introduction
342350

nextquestions.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ TODO:
5757
- question19 method2 to be done later after strings (revisit the code for it given as pdf)
5858
- method1 of question21 and question22 complete
5959
- do binary search method for strings question1
60+
- question 8 strings to be done (see from the sol pdf)

strings/a.exe

-5 Bytes
Binary file not shown.

strings/question8.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Run length encoding
3+
4+
Run length encoding means, traversing through the given character array and displaying which character
5+
is repeating how many times along with the character as output.
6+
Eg: SSMMMAAARRT => S2M3A3R2T1
7+
8+
Applications: File compression
9+
10+
METHOD:
11+
Just traverse and maintain a counter to solve the algo
12+
Time complexity: O(n)
13+
Space complexity: O(1)
14+
*/
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
19+
void runLengthEncoding(char *arr, int size){
20+
int i,j=0;
21+
// char *finalStr = (char *)malloc(sizeof(char)*(size*2 + 1));
22+
int counter = 1;
23+
printf("%s\n", arr);
24+
for(i=0;i<size;i++){
25+
// finalStr[j++] = arr[i];
26+
if(arr[i] == arr[i+1]){
27+
counter++;
28+
}else{
29+
printf("%c%d", arr[i],counter);
30+
counter=1;
31+
}
32+
}
33+
}
34+
35+
int main(){
36+
char arr[] = "SSMMMAAARRT";
37+
int length = strlen(arr);
38+
39+
runLengthEncoding(arr,length);
40+
return 0;
41+
}

strings/question9.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Check whether given two strings are anagrams of each other
3+
4+
Two strings are anagrams if they have same no of characters and they are composed of the same letters.
5+
Even if there are repetitions, they are going to be same.
6+
7+
METHOD1:
8+
sort and scan
9+
Time complexity: O(nlogn)
10+
Space complexity: O(1) //for inplace sorting
11+
12+
METHOD2:
13+
use hash table and increment count for first string and decrement for second. In the end all count
14+
values should be zero.
15+
Time complexity: O(n)
16+
Space complexity: O(1) //256 not dependent on input
17+
*/
18+
//sorting method not done as it is very basic
19+
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <string.h>
23+
#define size 256
24+
25+
void checkAnagram(char *str1, char *str2, int l1, int l2){
26+
int hash[size] = {0};
27+
int i;
28+
for(i=0;i<l1;i++){
29+
hash[str1[i]]++;
30+
}
31+
for(i=0;i<l2;i++){
32+
hash[str2[i]]--;
33+
}
34+
for(i=0;i<size;i++){
35+
if(hash[i] > 0){
36+
printf("two strings are NOT anagrams\n");
37+
return;
38+
}
39+
}
40+
printf("two strings are anagrams of each other\n");
41+
}
42+
43+
int main(){
44+
char str1[] = "heater";
45+
char str2[] = "reheaa";
46+
int l1 = strlen(str1);
47+
int l2 = strlen(str2);
48+
if(l1 != l2){
49+
printf("two strings not NOT anagrams\n");
50+
return 0;
51+
}
52+
checkAnagram(str1,str2, l1, l2);
53+
return 0;
54+
}

0 commit comments

Comments
 (0)