考虑到字符串数组ARR,任务是去除那些较早字符串的字谜的字符串,然后打印排序后剩余的阵列。
例子:
Input: arr[] = { “geeks”, “keegs”, “code”, “doce” }, N = 4
Output: [“code”, “geeks”]
Explanation:
“geeks” and “keegs” are anagrams, so we remove “keegs”.
Similarly, “code” and “doce” are anagrams, so we remove “doce”.
Input : arr[] = {“tea”, “ate”, “anagram”, “eat”, “gramaan”}, N = 5
Output : [“anagram”, “tea”]
Explanation: “ate” and “eat” are anagram of “tea”.
“gramaan” is an anagram of “anagram” hence, array becomes [“anagram”, “tea”].
方法:
方法:为了检查给定的两个字符串是否不是字谜,我们可以简单地对两个字符串进行排序并进行比较。另外,要检查字符串是否出现,我们可以使用哈希图。
- 创建一个辅助数组以保留生成的字符串,并创建一个哈希图以保留到目前为止我们发现的字符串的标记。
- 然后遍历数组的给定字符串,对当前字符串排序,并在哈希图中检查该字符串。
- 如果在哈希图中找不到当前字符串,则将arr [i]推入结果数组,然后将排序后的字符串插入哈希图中。
- 最后,对结果数组进行排序并打印每个字符串。
下面是上述方法的实现。
C++
// C++ implementation to remove
// all the anagram strings
#include
using namespace std;
// Function to remove the anagram string
void removeAnagrams(string arr[], int N)
{
// vector to store the final result
vector ans;
// data structure to keep a mark
// of the previously occured string
unordered_set found;
for (int i = 0; i < N; i++) {
string word = arr[i];
// Sort the characters
// of the current string
sort(begin(word), end(word));
// Check if current string is not
// present inside the hashmap
// Then push it in the resultant vector
// and insert it in the hashmap
if (found.find(word) == found.end()) {
ans.push_back(arr[i]);
found.insert(word);
}
}
// Sort the resultant vector of strings
sort(begin(ans), end(ans));
// Print the required array
for (int i = 0; i < ans.size(); ++i) {
cout << ans[i] << " ";
}
}
// Driver code
int main()
{
string arr[]
= { "geeks", "keegs",
"code", "doce" };
int N = 4;
removeAnagrams(arr, N);
return 0;
}
Java
// Java implementation to remove
// all the anagram Strings
import java.util.*;
class GFG{
// Function to remove the anagram String
static void removeAnagrams(String arr[], int N)
{
// vector to store the final result
Vector ans = new Vector();
// data structure to keep a mark
// of the previously occured String
HashSet found = new HashSet ();
for (int i = 0; i < N; i++) {
String word = arr[i];
// Sort the characters
// of the current String
word = sort(word);
// Check if current String is not
// present inside the hashmap
// Then push it in the resultant vector
// and insert it in the hashmap
if (!found.contains(word)) {
ans.add(arr[i]);
found.add(word);
}
}
// Sort the resultant vector of Strings
Collections.sort(ans);
// Print the required array
for (int i = 0; i < ans.size(); ++i) {
System.out.print(ans.get(i)+ " ");
}
}
static String sort(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
// Driver code
public static void main(String[] args)
{
String arr[]
= { "geeks", "keegs",
"code", "doce" };
int N = 4;
removeAnagrams(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to remove
# all the anagram strings
# Function to remove the anagram string
def removeAnagrams(arr, N):
# vector to store the final result
ans = []
# data structure to keep a mark
# of the previously occured string
found = dict()
for i in range(N):
word = arr[i]
# Sort the characters
# of the current string
word = " ".join(sorted(word))
# Check if current is not
# present inside the hashmap
# Then push it in the resultant vector
# and insert it in the hashmap
if (word not in found):
ans.append(arr[i])
found[word] = 1
# Sort the resultant vector of strings
ans = sorted(ans)
# Print the required array
for i in range(len(ans)):
print(ans[i], end=" ")
# Driver code
if __name__ == '__main__':
arr=["geeks", "keegs","code", "doce"]
N = 4
removeAnagrams(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# implementation to remove
// all the anagram Strings
using System;
using System.Collections.Generic;
class GFG{
// Function to remove the anagram String
static void removeAnagrams(String []arr, int N)
{
// vector to store the readonly result
List ans = new List();
// data structure to keep a mark
// of the previously occured String
HashSet found = new HashSet ();
for (int i = 0; i < N; i++) {
String word = arr[i];
// Sort the characters
// of the current String
word = sort(word);
// Check if current String is not
// present inside the hashmap
// Then push it in the resultant vector
// and insert it in the hashmap
if (!found.Contains(word)) {
ans.Add(arr[i]);
found.Add(word);
}
}
// Sort the resultant vector of Strings
ans.Sort();
// Print the required array
for (int i = 0; i < ans.Count; ++i) {
Console.Write(ans[i]+ " ");
}
}
static String sort(String inputString)
{
// convert input string to char array
char []tempArray = inputString.ToCharArray();
// sort tempArray
Array.Sort(tempArray);
// return new sorted string
return String.Join("",tempArray);
}
// Driver code
public static void Main(String[] args)
{
String []arr
= { "geeks", "keegs",
"code", "doce" };
int N = 4;
removeAnagrams(arr, N);
}
}
// This code is contributed by 29AjayKumar
输出:
code geeks