作者:小新亚笔 | 来源:互联网 | 2023-09-25 13:19
打印给定数组中所有不同的字符串原文:https://www.
打印给定数组中所有不同的字符串
原文:https://www . geesforgeks . org/print-all-distinct-strings-from-a-给定数组/
给定一个大小为 N 的字符串数组 arr[] ,任务是打印给定数组中所有不同的字符串。
示例:
输入: arr[] = {“极客”、“For”、“极客”、“代码”、“编码器”}
输出:编码器代码极客 For
解释:由于数组中的所有字符串都是不同的,所以需要的输出是编码器代码极客 For 。
输入: arr[] = {“好”“神”“好”“神”“神”}
T3】输出:神好神好
天真方法:解决这个问题最简单的方法是根据字符串的字典顺序对数组进行排序。遍历数组并检查数组的当前字符串是否等于之前遍历的字符串。如果发现为假,则打印当前字符串。
时间复杂度: O(N * M * log(N)),其中 M 为最长字符串的长度。
辅助空间: O(1)
高效方法:要优化上述方法,思路是使用哈希。按照以下步骤解决问题:
- 初始化一个设置,比如说 DistString ,来存储给定数组中的不同字符串。
- 遍历数组并将数组元素插入字符串。
- 最后,打印 DistString 中的所有字符串。
下面是上述方法的实现。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the distinct strings
// from the given array
void findDisStr(vector& arr, int N)
{
// Stores distinct strings
// from the given array
unordered_set DistString;
// Traverse the array
for (int i = 0; i // If current string not
// present into the set
if (!DistString.count(arr[i])) {
// Insert current string
// into the set
DistString.insert(arr[i]);
}
}
// Traverse the set DistString
for (auto String : DistString) {
// Print distinct string
cout < }
}
// Driver Code
int main()
{
vector arr = { "Geeks", "For", "Geeks",
"Code", "Coder" };
// Stores length of the array
int N = arr.size();
findDisStr(arr, N);
return 0;
}
Java 语言(一种计算机语言,尤用于创建网站)
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to find the distinct strings
// from the given array
static void findDisStr(List arr, int N)
{
// Stores distinct strings
// from the given array
Set DistString = new HashSet();
// Traverse the array
for(int i = 0; i {
// If current string not
// present into the set
if (!DistString.contains(arr.get(i)))
{
// Insert current string
// into the set
DistString.add(arr.get(i));
}
}
// Traverse the set DistString
for(String string : DistString)
{
// Print distinct string
System.out.print(string + " ");
}
}
// Driver code
public static void main(String[] args)
{
List arr = Arrays.asList(new String[]{
"Geeks", "For", "Geeks", "Code", "Coder" });
// Stores length of the array
int N = arr.size();
findDisStr(arr, N);
}
}
// This code is contributed by jithin
Python 3
# Python3 program to implement
# the above approach
# Function to find the distinct
# strings from the given array
def findDisStr(arr, N):
# Stores distinct strings
# from the given array
DistString = set()
# Traverse the array
for i in range(N):
# If current string not
# present into the set
if (arr[i] not in DistString):
# Insert current string
# into the set
DistString.add(arr[i])
# Traverse the set DistString
for string in DistString:
# Print distinct string
print(string, end = " ")
# Driver Code
if __name__ == "__main__":
arr = [ "Geeks", "For", "Geeks",
"Code", "Coder" ]
# Stores length of the array
N = len(arr)
findDisStr(arr, N)
# This code is contributed by chitranayal
C
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the distinct strings
// from the given array
static void findDisStr(List arr, int N)
{
// Stores distinct strings
// from the given array
HashSet DistString = new HashSet();
// Traverse the array
for(int i = 0; i {
// If current string not
// present into the set
if (!DistString.Contains(arr[i]))
{
// Insert current string
// into the set
DistString.Add(arr[i]);
}
}
// Traverse the set DistString
foreach(string a in DistString)
{
Console.Write(a +" ");
}
}
// Driver code
public static void Main(String[] args)
{
List arr = new List(new []{
"Geeks", "For", "Geeks", "Code", "Coder" });
// Stores length of the array
int N = arr.Count;
findDisStr(arr, N);
}
}
// This code is contributed by jana_sayantan
java 描述语言
Output:
Coder Code Geeks For
时间复杂度: O(N * M),其中 M 是最长字符串的长度。
辅助空间: O(N * M)