Decrypt Map Coordinates from given pair of strings based on given rules
Given a pair of lowercase strings string1 and string2 of size M and N, the task is to decrypt these strings according to the following rules. The last character of encrypted string denotes the direction latitude string(only two [n-North, s-South]) longitude string(other two [e-East, w-West]). Except for the last character the string denotes an integer value irrespective of whether it is a latitude string or longitude string. The Integer part of the coordinate can be decoded as (Count of letter with maximum occurrences – Count of letter with minimum occurrences in string).
Input: string1 = “babbeddcs”, string2 = “aeeaecacw”
Output: 2 South 1 West
Explanation: In the string1, the last character is s, so south, the most frequent character is b with frequency 3 and the least are a, e and c with 1. Similarly, for the other string i.e, string2.
Input: string1 = “ddcs”, string2 = “aeew”
Output: 1 South 1 West
Approach: The idea to solve this problem is to count the maximum and minimum frequency of characters of each string and check the last character. Follow the steps below to solve this problem:
- Initialize the variables c1 and c2 as the last characters of the strings string1 and string2.
- Initialize the vectors f1 and f2 with 0 to store the frequencies.
- Traverse the strings string1 and string2 and store the frequency of all characters of the string in vectors f1 and f2.
- Initialize the variables ma1, mi1, ma2, and mi2 to store the maximum and minimum frequency occurring characters from both the strings string1 and string2.
- Traverse the vectors f1 and f2 and store the values of ma1, mi1, ma2, and mi2.
- After performing the above steps, print the result from the above computations.
Below is the implementation of the above approach.
2 South 1 West
Time Complexity: O(max(M, N))
Auxiliary Space: O(1)
Please Login to comment...