给定一个大小为N的字符串数组arr[] ,任务是打印给定数组中存在的所有不同字符串。
例子:
Input: arr[] = { “Geeks”, “For”, “Geeks”, “Code”, “Coder” }
Output: Coder Code Geeks For
Explanation: Since all the strings in the array are distinct, the required output is Coder Code Geeks For.
Input: arr[] = { “Good”, “God”, “Good”, “God”, “god” }
Output: god Good God
朴素的方法:解决这个问题最简单的方法是根据字符串的字典顺序对数组进行排序。遍历数组并检查数组的当前字符串是否等于先前遍历的字符串。如果发现为假,则打印当前字符串。
时间复杂度: O(N * M * log(N)),其中 M 是最长字符串的长度。
辅助空间: O(1)
高效的方法:为了优化上述方法,想法是使用Hashing。请按照以下步骤解决问题:
- 初始化一个 Set,比如DistString ,以存储来自给定数组的不同字符串。
- 遍历数组并将数组元素插入到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 < N; 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 << String << " ";
}
}
// 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 < N; 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
Python3
# 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 < N; 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
输出:
Coder Code Geeks For
时间复杂度: O(N * M),其中 M 是最长字符串的长度。
辅助空间: O(N * M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live