热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

打印给定数组中所有不同的字符串

打印给定数组中所有不同的字符串原文: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)


推荐阅读
  • 本文介绍了Java中Hashtable的clear()方法,该方法用于清除和移除指定Hashtable中的所有键。通过示例程序演示了clear()方法的使用。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • MPLS VP恩 后门链路shamlink实验及配置步骤
    本文介绍了MPLS VP恩 后门链路shamlink的实验步骤及配置过程,包括拓扑、CE1、PE1、P1、P2、PE2和CE2的配置。详细讲解了shamlink实验的目的和操作步骤,帮助读者理解和实践该技术。 ... [详细]
author-avatar
小新亚笔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有