-
Rotating an array counter clockwise around a given index →
Given an array, how do you rotate it counter clockwise around a given input index position? i.e. If the input array was
[1, 2, 3, 4, 5, 6, 7, 8]
, the result of rotation around index3
(that has the element 4) in the counter clockwise direction would be[5, 6, 7, 8, 1, 2, 3, 4]
. The sequence of transformations the array goes through as each of the elements until the given index rotates would be:1 2 3 4 5 6 7 8 --> input array 2 3 4 5 6 7 8 1 --> 1 has rotated away to the end of the array 3 4 5 6 7 8 1 2 --> 2 has rotated away following 1 4 5 6 7 8 1 2 3 --> now, it's 3's turn 5 6 7 8 1 2 3 4 --> the element at index 3 (i.e. 4) around which we wanted to rotate has also completed.
We can use the algorithm we developed to reverse a sub-array, in order to achieve this rotation. The idea is simple:
- First, reverse the sub-array consisting of elements from the beginning of the array until the pivot (around which we want to rotate the array) in place
- Now, rotate the remaining sub-array alone (i.e. from the pivot until the end of the array
- Complete it by reversing the entire array once
1 2 3 4 5 6 7 8 --> input array 4 3 2 1 5 6 7 8 --> step-1 first sub-array reversed 4 3 2 1 8 7 6 5 --> step-2 second sub-array reversed 5 6 7 8 1 2 3 4 --> step-3 rotation of the entire array
An implementation would look like:
-
Checking if a string is an isogram →
A string is an isogram if no character in it appears more than once.
Let us write a simple algorithm to identify if a given input string is an isogram. For simplicity let us consider only strings that have small characters and no spaces or special characters i.e. - only characters from a to z.
In our previous post, we learnt how to keep track of characters that appear in a string as we traverse the input string. We use the same idea here, just this time we simplify it to be 26 characters (a to z) instead of 256.
- We traverse the input string and maintain a counter for each of the occurences in the string. Note that we subtract ‘a’ from the ASCII value as our strings contain only a to z and we want our index
0
to correspond to a. The intention of this simplification was precisely to learn how to modify our algorithm for such specific use cases. - We traverse the counter array (which is essentially a mapping
seen: character -> count
to see if any of the characters appeared more than once (i.e. their count mapping is greater than 1).
- We traverse the input string and maintain a counter for each of the occurences in the string. Note that we subtract ‘a’ from the ASCII value as our strings contain only a to z and we want our index
-
Checking if two strings are isomorphic →
Today, let us see how we can figure out if two given input strings are isomorphic using a time-efficient algorithm.
Given two strings s1 and s2 if there is a unique mapping from each of the characters of one to the other, then they are isomorphic
For example,
anna
andnaan
are isomorphic with the mappinga -> n
andn -> a
. Similarlyaab
andxxy
are isomorphic with the mappinga -> x
andb -> y
, whileaab
andxyz
are not isomorphic. However,anna
andnaa
are not isomorphic as there is no direct mapping for the lasta
ofanna
(a hint that the strings should be of equal length for them to be isomorphic).An approach is to traverse one of the strings and mark the occurence of the character in the string. The nice thing about character strings is that there are only 256 ASCII characters and hence we can have a character array of size 256 to mark the occurence of characters in a string. Along with marking the occurence, we need to store the mapping of this character to the corresponding (array index) character of the second string.
- During the very first encounter of a character, say the first
a
(index 0) inanna
, we check if the corresponding index in the other stringnaan
has already been visited. - We visit it (i.e. remember it in some algorithmic way).
- We store the mapping of this visit (i.e.
a -> n
). - If we encounter a character again, say the last
a
inanna
, we check for the mapping to see if the corresponding index in the second string is as per our expectations (i.e. obeys the mapping).
The way we maintain the mapping is through an array
map
and we also maintain whether we already visited a particular character using an arrayvisited
. Since we need two additional arrays of size 256 each, there is some cost we pay for this space. However, we traverse the input array only once and so it would be O(n) time algorithm. These four steps are noted in the code fragment below for easier reference. - During the very first encounter of a character, say the first
-
Reversing a sub-array of an array in place →
In an earlier post, we saw how to reverse a given input array. But, it reversed the entire array. What if we want to pick a fragment of the input array and reverse only that?
Let us assume we are given the
start
andend
indices - the window that we want to reverse. It is very similar to our previous approach, except that we need to be careful computing the indices to iterate on.There is an interesting story about computation of mid-point given two window boundaries. At first glance, it would seem tempting to write it as
(start + end) / 2
but it is safer (and more correct) to write it asstart + ((end - start) / 2)
. -
Checking for string rotations by a fixed length →
In yesterday’s post we saw how to check if two given strings are rotations of each other. Today, we will add one other restriction to the problem and try to solve it. What if the algorithm needs to check if the two input strings are rotations of each other by a fixed length
n
which is also provided as an input to the function? For example,anna
andnaan
are rotations of each other by2
as we need to make two rotations (each rotation moves only 1 character) to get from one string to the other and similarly one cannot be obtained from the other by rotation of just 1.We use the same algorithm as before, but this time we will be careful to store the index position (when we search for one of the strings in the concatenation) and match it against the input
n
.Again, we have to check if the lengths of the input strings are the same. Without this check, we migth deduce that
ello
can be obtained fromhello
by rotating through1
character position.