Decrypt Map Coordinates from given pair of strings based on given rules
Last Updated :
27 Dec, 2021
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).
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:
- Initialize the variables c1 and c2 as the last characters of the strings string1[] and string2[].
- Initialize the vectors f1[26] and f2[26] 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.
C++14
// 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
// 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
# 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#
// 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.
JavaScript
<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>
Time Complexity: O(max(M, N))
Auxiliary Space: O(1)
Similar Reads
Decrypt a string according to given rules Given encrypted string str, the task is to decrypt the given string when the encryption rules are as follows: Start with the first character of the original string.In every odd step, append the next character to it.In every even step, prepend the next character to the encrypted string so far.For exa
15+ min read
Generate a String from given Strings P and Q based on the given conditions Given two strings P and Q, the task is to generate a string S satisfying the following conditions: Find S such that P is rearranged and Q is a substring in it.All the characters before Q in S should be smaller than or equal to the first character in Q and in lexicographic order.The rest of the chara
7 min read
Decrypt message from given code by replacing all * with prefix values of encoded string Given a string str of length of N that is in the encoded form with alphabets and * . The task is to find the string from which it was generated. The required string can be generated from the encoded string by replacing all the * with the prefix values of the encoded string. Examples: Input: str = ab
4 min read
Check if possible to move from given coordinate to desired coordinate Given two coordinates (x, y) and (a, b). Find if it is possible to reach (x, y) from (a, b). Only possible moves from any coordinate (i, j) are (i-j, j)(i, i-j)(i+j, j)(i, i+j) Given x, y, a, b can be negative. Examples: Input : (x, y) = (1, 1) and (a, b) = (2, 3). Output : Yes. (1, 1) -> (2, 1)
7 min read
Reconstruct original string from resultant string based on given encoding technique A binary string S of length N is constructed from a string P of N characters and an integer X. The choice of the ith character of S is as follows: If the character Pi-X exists and is equal to 1, then Si is 1if the character Pi+X exists and is equal to 1, then Si is 1if both of the aforementioned con
10 min read
Check if String T can be made Substring of S by replacing given characters Given two strings S and T and a 2D array replace[][], where replace[i] = {oldChar, newChar} represents that the character oldChar of T is replaced with newChar. The task is to find if it is possible to make string T a substring of S by replacing characters according to the replace array. Note: Each
9 min read
Find the string among given strings represented using given encryption pattern Given an array of strings arr[] of size N and an encrypted string str, the task is to find the correct string from the given array of strings whose encryption will give str where str is encrypted using the following rules: The starting characters form an integer representing the number of uppercase
8 min read
Minimizing Steps to Form Anagrams from Given Strings Given two strings s1 and s2. You have the flexibility to add any letter to either the string s1 or s2 in just one action. Find out the least number of steps needed to transform two given words, s1, and s2, into anagrams of each other. The length of both strings can be different and it contains only
7 min read
Check if String can be made Palindrome by replacing characters in given pairs Given a string str and K pair of characters, the task is to check if string str can be made Palindrome, by replacing one character of each pair with the other. Examples: Input: str = "geeks", K = 2, pairs = [["g", "s"], ["k", "e"]]Output: TrueExplanation: Swap 's' of "geeks" with 'g' using pair ['g'
9 min read
Check if there is any common character in two given strings Given two strings. The task is to check that is there any common character in between two strings. Examples: Input: s1 = "geeksforgeeks", s2 = "geeks" Output: Yes Input: s1 = "geeks", s2 = "for" Output: No Approach: Traverse the 1st string and map the characters of the string with its frequency, in
8 min read