https://github.com/prasanthlouis/C-equivalent-to-Cracking-the-Coding-Interview
There's nothing too complex on the arrays and strings problems. Most of them are common problems you've probably seen before.
Like I said before, I'll make a post for each section unless there's a question that needs to be explained in detail. These were kinda easy, so I didn't really feel the need to dedicate a whole post for each question.
Remember you can always check my code out on Github.
Things that intrigued me were (Or what I did differently are):
Question 1: The unique characters question. - Pretty straight forward, but after doing it the only difference I saw in my code was that I used 0s and 1s instead of boolean values in the solution provided. Meh. Not much of a difference.
Question 2: Reverse a string- Again, nothing complex. An easy second semester of college question.
Question 3: Permutation: Originally I thought of a simple search for each letter and whether the other word contains that letter. The algorithm presented in the book was pretty nice.
First of all, they checked if the two strings length was equal. If not they aren't a palindrome. (I didn't really think about this case)
Second they sorted the words by the characters present in them, and from there on out, it was just matching the letters in both the arrays. This was way more simpler than what I initially thought.
Question 4: Replacement with %20. My initial attempt made me copy the letters into another array and adding a %20 where ever needed. After reading the algorithm, I realized that you didn't need to copy the array, instead move to the end of the array, and work backwards while shifting. Again nothing hard here.
Question 5: The repeated aabbccc question. I'll admit, I attacked this head on and got the output. But when I went back to read the algorithm, I realized for big strings, my code wasn't efficient.
If its one thing I learned by now its this:
IF YOU CAN AVOID COPYING STRINGS, DO IT. INSTEAD, MANIPULATE THE ORIGINAL STRING OR CREATE A NEW STRING AND UPDATE IT BY INDEX INSTEAD OF CONCATENATION.
I then realized why in question 4 they wanted to modify the original array rather than copying it into a new array. It wasn't explicitly said at the time, but looking back on it I realized I could've done it better.
I liked how we first had to find the length of the array and then we attack it. Seemed interesting to me. But again, no problems in implementation.
Question 6: Matrix rotation. This again, I attacked it and got it right. On reading the algorithm, I realized again, the faster way is to update by INDEX and not COPYING STRINGS.
(Facepalm! Oh well. This is how you learn and I won't forget it anymore.)
So I implemented my own copying algorithm and did it.
This was probably the hardest question of this session, or at least the most time consuming.
Question 7: I liked the algorithm they used here. Instead of finding the elements that were 0, just store the indexes of the row and column of the element that was zero and put the element to zero if the index was marked. Again I used 0s and 1s instead of boolean values.
Question 8:Substring. I was stumped on this question. But when I saw the solution they provided, it was really easy to implement.
Waterbottle
terbottlewa
If you write the original string twice, it'll be easy to determing whether the second string is a rotated substring of the first.
Waterbottlewaterbottle
terbottlewa
And that's it! Session one is done!
Nothing to difficult. I used bold text whenever I thought was necessary.