给定一个字符串str和一个大小为N的字符串arr[]数组,任务是从 arr[] 打印一个字符串,该字符串具有与 str 匹配的最大字符数。
例子:
Input: str = “vikas”, N = 3, arr[] = [“preeti”, “khusbu”, “katherina”]
Output: “katherina”
Explanation:
Number of similar characters between Str and each string in D[ ] are,
“preeti” = 1
“khusbu” = 2
“katherina” = 3
Hence, “katherina” has maximum matching characters.
Input: str = “gfg”, N = 3, arr[] = [“goal”, “fog”, “abc”]
Output: “fog”
Explanation:
Number of similar characters between Str and each string in D[ ] are,
“goal” = 1
“fog” = 2
“abc” = 0
Hence, “fog” has maximum matching characters.
方法:思想是考虑数组 arr[] 的每个字符串,并将其每个字符与给定的字符串str 进行比较。跟踪最大匹配字符和相应的字符串。另外,请确保从每个字符串删除重复项。以下是步骤:
- 创建一个变量maxVal和一个变量val ,分别跟踪匹配字符和对应字符串的最大数量。
- 遍历数组arr[]并从每个字符串删除重复项。此外,从Str 中删除重复项。
- 对于 arr[] 的每个字符串,将其与给定的字符串str进行比较,并计算匹配字符的数量。
- 使用最大匹配字符数更新maxVal并使用相应的字符串更新val 。
- 最后,打印完上述操作后val的值。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.util.*;
import java.io.*;
public class GFG {
// Function that print string which
// has maximum similar characters
private static void
maxMatchingChar(ArrayList list,
int n, char ch[])
{
String val = "";
int maxVal = Integer.MIN_VALUE;
for (String s : list) {
// Count matching characters
int matchingchar = matchingChar(
s.toLowerCase(), ch);
// Update maxVal if needed
if (matchingchar > maxVal) {
maxVal = matchingchar;
val = s;
}
}
System.out.print(val + " ");
}
// Function that returns the count
// of number of matching characters
private static int matchingChar(
String s, char[] ch)
{
int freq = 0, c = ch.length;
// Traverse the character array
for (int i = 0; i < c; i++) {
// If character matches
// then increment the count
if (s.contains(
String.valueOf(ch[i]))) {
freq++;
}
}
return freq;
}
// Function to remove duplicate
// characters
private static char[] removeDuplicate(String str)
{
// To keep unique character only
HashSet set
= new HashSet();
int c = str.length();
// Inserting character in hashset
for (int i = 0; i < c; i++) {
set.add(str.charAt(i));
}
char arr[] = new char[set.size()];
int index = 0;
// Update string with unique characters
for (char s : set) {
arr[index] = s;
index++;
}
// Return the char array
return arr;
}
// Driver Code
public static void main(
String[] args) throws Exception
{
int n = 3;
String str = "Vikas";
String D[] = { "preeti", "khusbu", "katherina" };
// Removing duplicate and
// convert to lowercase
char ch[]
= removeDuplicate(str.toLowerCase());
ArrayList list
= new ArrayList();
// Insert each string in the list
for (int i = 0; i < n; i++) {
list.add(D[i]);
}
// Function Call
maxMatchingChar(list, n, ch);
}
}
Python3
# Python3 program for the above approach
import sys
# Function that print string which
# has maximum similar characters
def maxMatchingChar(list, n, ch):
val = ""
maxVal = -sys.maxsize - 1
for s in list:
# Count matching characters
matchingchar = matchingChar(s.lower(), ch)
# Update maxVal if needed
if (matchingchar > maxVal):
maxVal = matchingchar
val = s
print(val, end = " ")
# Function that returns the count
# of number of matching characters
def matchingChar(s, ch):
freq = 0
c = len(ch)
# Traverse the character array
for i in range(c):
# If character matches
# then increment the count
if ch[i] in s:
freq += 1
return freq
# Driver Code
n = 3
str = "Vikas"
D = [ "preeti", "khusbu", "katherina" ]
# Remove duplicate characters and
# convert to lowercase
ch = list(set(str.lower()))
List = []
# Insert each string in the list
for i in range(n):
List.append(D[i])
# Function call
maxMatchingChar(List, n, ch)
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that print string which
// has maximum similar characters
private static void maxMatchingChar(List list,
int n, char []ch)
{
String val = "";
int maxVal = int.MinValue;
foreach(String s in list)
{
// Count matching characters
int matchingchar = matchingChar(
s.ToLower(), ch);
// Update maxVal if needed
if (matchingchar > maxVal)
{
maxVal = matchingchar;
val = s;
}
}
Console.Write(val + " ");
}
// Function that returns the count
// of number of matching characters
private static int matchingChar(String s, char[] ch)
{
int freq = 0, c = ch.Length;
// Traverse the character array
for(int i = 0; i < c; i++)
{
// If character matches
// then increment the count
if (s.Contains(String.Join("", ch[i])))
{
freq++;
}
}
return freq;
}
// Function to remove duplicate
// characters
private static char[] removeDuplicate(String str)
{
// To keep unique character only
HashSet set = new HashSet();
int c = str.Length;
// Inserting character in hashset
for(int i = 0; i < c; i++)
{
set.Add(str[i]);
}
char []arr = new char[set.Count];
int index = 0;
// Update string with unique characters
foreach(char s in set)
{
arr[index] = s;
index++;
}
// Return the char array
return arr;
}
// Driver Code
public static void Main(String[] args)
{
int n = 3;
String str = "Vikas";
String []D = { "preeti", "khusbu",
"katherina" };
// Removing duplicate and
// convert to lowercase
char []ch = removeDuplicate(str.ToLower());
List list = new List();
// Insert each string in the list
for(int i = 0; i < n; i++)
{
list.Add(D[i]);
}
// Function call
maxMatchingChar(list, n, ch);
}
}
// This code is contributed by Rohit_ranjan
katherina
时间复杂度: O(N*M) 其中 M 是字符串的最大长度。
辅助空间: O(M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。