Skip to content

Commit

Permalink
new question added
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed May 24, 2017
1 parent f4f699c commit b880456
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions nextquestions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Binary file modified strings/a.exe
Binary file not shown.
41 changes: 41 additions & 0 deletions strings/question8.c
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;
}
54 changes: 54 additions & 0 deletions strings/question9.c
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;
}

0 comments on commit b880456

Please sign in to comment.