Open In App

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 occurrencesCount of letter with minimum occurrences in string). 

Examples:



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:

Below is the implementation of the above approach.




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to decrypt the strings
void find(string string1, string string2)
{
 
    // Size of the strings
    int M = string1.length(),
        N = string2.length();
 
    // Last characters of the strings
    char c1 = string1[M - 1],
         c2 = string2[N - 1];
 
    // Arrays to store the frequencies
    vector<int> f1(26, 0), f2(26, 0);
 
    // Calculate the frequency of characters
    // of both the strings
    for (int i = 0; i < M - 1; i++)
        f1[string1[i] - 'a']++;
 
    for (int i = 0; i < N - 1; i++)
        f2[string2[i] - 'a']++;
 
    // Variables to store the maximum and
    // minimum occurring character.
    int ma1 = 0, mi1 = M, ma2 = 0, mi2 = N;
 
    for (int i = 0; i < 26; i++) {
        ma1 = max(ma1, f1[i]);
        if (f1[i] > 0)
            mi1 = min(mi1, f1[i]);
 
        ma2 = max(ma2, f2[i]);
        if (f2[i] > 0)
            mi2 = min(mi2, f2[i]);
    }
 
    // Print the result
    cout << ma1 - mi1 << " ";
    if (c1 == 's')
        cout << "South ";
    else
        cout << "North ";
    cout << ma2 - mi2;
    if (c2 == 'e')
        cout << " East ";
    else
        cout << " West ";
}
 
// Driver Code
int main()
{
 
    string string1 = "babbeddcs",
           string2 = "aeeaecacw";
 
    find(string1, string2);
 
    return 0;
}




// Java code for the above approach
import java.io.*;
class GFG
{
   
// Function to decrypt the strings
static void find(String string1, String string2)
{
 
    // Size of the strings
    int M = string1.length();
    int N = string2.length();
 
    // Last characters of the strings
    char c1 = string1.charAt(M - 1);
    char c2 = string2.charAt(N - 1);
 
    // Arrays to store the frequencies
    int []f1 = new int[26];
    int []f2 = new int[26];
 
    // Calculate the frequency of characters
    // of both the strings
    for (int i = 0; i < M - 1; i++)
        f1[string1.charAt(i) - 'a']++;
 
    for (int i = 0; i < N - 1; i++)
        f2[string2.charAt(i) - 'a']++;
 
    // Variables to store the maximum and
    // minimum occurring character.
    int ma1 = 0, mi1 = M, ma2 = 0, mi2 = N;
 
    for (int i = 0; i < 26; i++) {
        ma1 = Math.max(ma1, f1[i]);
        if (f1[i] > 0)
            mi1 = Math.min(mi1, f1[i]);
 
        ma2 = Math.max(ma2, f2[i]);
        if (f2[i] > 0)
            mi2 = Math.min(mi2, f2[i]);
    }
 
    // Print the result
   System.out.print(ma1 - mi1 + " ");
    if (c1 == 's')
       System.out.print("South ");
    else
        System.out.print( "North ");
  System.out.print(ma2 - mi2);
    if (c2 == 'e')
        System.out.print( " East ");
    else
        System.out.print( " West ");
}
 
// Driver Code
    public static void main (String[] args) {
         String string1 = "babbeddcs";
         String string2 = "aeeaecacw";
 
       find(string1, string2);
       
    }
}
 
// This code is contributed by Potta Lokesh




# Python3 program for the above approach
 
# Function to decrypt the strings
def find(string1, string2):
 
    # Size of the strings
    M = len(string1)
    N = len(string2)
 
    # Last characters of the strings
    c1 = string1[M - 1]
    c2 = string2[N - 1]
 
    # Arrays to store the frequencies
    f1 = [0 for _ in range(26)]
    f2 = [0 for _ in range(26)]
 
    # Calculate the frequency of characters
    # of both the strings
    for i in range(0, M - 1):
        f1[ord(string1[i]) - ord('a')] += 1
 
    for i in range(0, N - 1):
        f2[ord(string2[i]) - ord('a')] += 1
 
    # Variables to store the maximum and
    # minimum occurring character.
    ma1 = 0
    mi1 = M
    ma2 = 0
    mi2 = N
 
    for i in range(0, 26):
        ma1 = max(ma1, f1[i])
        if (f1[i] > 0):
            mi1 = min(mi1, f1[i])
 
        ma2 = max(ma2, f2[i])
        if (f2[i] > 0):
            mi2 = min(mi2, f2[i])
 
    # Print the result
    print(ma1 - mi1, end = " ")
    if (c1 == 's'):
        print("South", end = " ")
    else:
        print("North", end = " ")
 
    print(ma2 - mi2, end = "")
    if (c2 == 'e'):
        print(" East ", end = "")
    else:
        print(" West ")
 
# Driver Code
if __name__ == "__main__":
 
    string1 = "babbeddcs"
    string2 = "aeeaecacw"
 
    find(string1, string2)
 
# This code is contributed by rakeshsahni




// C# Program to implement
// the above approach
using System;
class GFG
{
// Function to decrypt the strings
static void find(string string1, string string2)
{
 
    // Size of the strings
    int M = string1.Length;
    int N = string2.Length;
 
    // Last characters of the strings
    char c1 = string1[M - 1];
    char c2 = string2[N - 1];
 
    // Arrays to store the frequencies
    int []f1 = new int[26];
    int []f2 = new int[26];
    for(int i = 0; i < 26; i++) {
        f1[i] = 0;
        f2[i] = 0;
    }
 
    // Calculate the frequency of characters
    // of both the strings
    for (int i = 0; i < M - 1; i++)
        f1[string1[i] - 'a']++;
 
    for (int i = 0; i < N - 1; i++)
        f2[string2[i] - 'a']++;
 
    // Variables to store the maximum and
    // minimum occurring character.
    int ma1 = 0, mi1 = M, ma2 = 0, mi2 = N;
 
    for (int i = 0; i < 26; i++) {
        ma1 = Math.Max(ma1, f1[i]);
        if (f1[i] > 0)
            mi1 = Math.Min(mi1, f1[i]);
 
        ma2 = Math.Max(ma2, f2[i]);
        if (f2[i] > 0)
            mi2 = Math.Min(mi2, f2[i]);
    }
 
    // Print the result
    Console.Write(ma1 - mi1 + " ");
    if (c1 == 's')
        Console.Write("South ");
    else
        Console.Write("North ");
    Console.Write(ma2 - mi2);
    if (c2 == 'e')
        Console.Write(" East ");
    else
        Console.Write(" West ");
}
 
// Driver code
public static void Main() {
     
    string string1 = "babbeddcs";
    string string2 = "aeeaecacw";
 
    find(string1, string2);
}
}
// This code is contributed by Samim Hossain Mondal.




<script>
// Javascript program for the above approach
 
// Function to decrypt the strings
function find(string1, string2)
{
 
    // Size of the strings
    let M = string1.length;
    let N = string2.length;
 
    // Last characters of the strings
    let c1 = string1[M - 1];
    let c2 = string2[N - 1];
 
    // Arrays to store the frequencies
    let f1 = [], f2 = [];
    for(let i = 0; i < 26; i++) {
        f1[i] = 0;
        f2[i] = 0;
    }
     
    // Calculate the frequency of characters
    // of both the strings
    for (let i = 0; i < M - 1; i++)
        f1[string1.charCodeAt(i) - 97]++;
 
    for (let i = 0; i < N - 1; i++)
        f2[string2.charCodeAt(i) - 97]++;
 
    // Variables to store the maximum and
    // minimum occurring character.
    let ma1 = 0, mi1 = M, ma2 = 0, mi2 = N;
 
    for (let i = 0; i < 26; i++) {
        ma1 = Math.max(ma1, f1[i]);
        if (f1[i] > 0)
            mi1 = Math.min(mi1, f1[i]);
 
        ma2 = Math.max(ma2, f2[i]);
        if (f2[i] > 0)
            mi2 = Math.min(mi2, f2[i]);
    }
 
    // Print the result
    document.write(ma1 - mi1 + " ");
    if (c1 == 's')
        document.write("South ");
    else
        document.write("North ");
    document.write(ma2 - mi2);
    if (c2 == 'e')
        document.write(" East ");
    else
        document.write(" West ");
}
 
// Driver Code
let string1 = "babbeddcs";
let string2 = "aeeaecacw";
 
find(string1, string2);
 
// This code is contributed by Samim Hossain Mondal.
</script>

Output: 
2 South 1 West

 

Time Complexity: O(max(M, N))
Auxiliary Space: O(1)


Article Tags :