根据给定的规则从给定的字符串对中解密地图坐标
给定一对大小为M和N的小写字符串string1[]和string2[] ,任务是根据以下规则解密这些字符串。加密字符串的最后一个字符表示方向纬度字符串(只有两个[ n-North,s-South ])经度字符串(其他两个[ e-East,w-West ])。除了最后一个字符,该字符串表示一个整数值,无论它是纬度字符串还是经度字符串。坐标的整数部分可以解码为(出现次数最多的字母计数 -字符串中出现次数最少的字母计数)。
例子:
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
方法:解决这个问题的思路是统计每个字符串的字符的最大和最小频率,并检查最后一个字符。请按照以下步骤解决此问题:
- 将变量c1和c2初始化为字符串string1[]和string2[]的最后一个字符。
- 用0初始化向量f1[26]和f2[26]以存储频率。
- 遍历字符串string1[]和string2[] ,将字符串中所有字符的出现频率存储在向量f1[]和f2[]中。
- 初始化变量ma1、mi1、ma2和mi2以存储字符串string1[]和string2[]中出现频率最高和最低的字符。
- 遍历向量f1[]和f2[]并存储ma1、mi1、ma2和mi2 的值。
- 执行上述步骤后,打印上述计算的结果。
下面是上述方法的实现。
C++14
// C++ program for the above approach
#include
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 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
2 South 1 West
时间复杂度: O(max(M, N))
辅助空间: O(1)