首页 > 其他分享 >[LeetCode] 2053. Kth Distinct String in an Array

[LeetCode] 2053. Kth Distinct String in an Array

时间:2024-08-05 14:29:04浏览次数:8  
标签:2053 String Distinct arr distinct str 字符串 string

A distinct string is a string that is present only once in an array.

Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".

Note that the strings are considered in the order in which they appear in the array.

Example 1:
Input: arr = ["d","b","c","b","c","a"], k = 2
Output: "a"
Explanation:
The only distinct strings in arr are "d" and "a".
"d" appears 1st, so it is the 1st distinct string.
"a" appears 2nd, so it is the 2nd distinct string.
Since k == 2, "a" is returned.

Example 2:
Input: arr = ["aaa","aa","a"], k = 1
Output: "aaa"
Explanation:
All strings in arr are distinct, so the 1st string "aaa" is returned.

Example 3:
Input: arr = ["a","b","a"], k = 3
Output: ""
Explanation:
The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".

Constraints:
1 <= k <= arr.length <= 1000
1 <= arr[i].length <= 5
arr[i] consists of lowercase English letters.

数组中第 K 个独一无二的字符串。

独一无二的字符串 指的是在一个数组中只出现过 一次 的字符串。 给你一个字符串数组 arr 和一个整数 k ,请你返回 arr 中第 k 个 独一无二的字符串 。如果 少于 k 个独一无二的字符串,那么返回 空字符串 "" 。 注意,按照字符串在原数组中的 顺序 找到第 k 个独一无二字符串。

思路

需要遍历两遍 input 数组。第一遍遍历,用 hashmap 记录每个不同字符的出现次数。第二次遍历,挑出所有只出现过一次的字符串,放在一个 list 中。遍历结束后,如果 list 的长度不足 k,则返回空字符串;反之则返回第 k 个字符串。

复杂度

时间O(n)
空间O(n)

代码

Java实现

class Solution {
    public String kthDistinct(String[] arr, int k) {
        HashMap<String, Integer> map = new HashMap<>();
        for (String str : arr) {
            map.put(str, map.getOrDefault(str, 0) + 1);
        }

        List<String> res = new ArrayList<>();
        for (String str : arr) {
            if (map.get(str) == 1) {
                res.add(str);
            }
        }
        if (res.size() < k) {
            return "";
        }
        return res.get(k - 1);
    }
}

标签:2053,String,Distinct,arr,distinct,str,字符串,string
From: https://www.cnblogs.com/cnoodle/p/18343153

相关文章

  • String类
    String类一.字符串常量池在Java(以及许多其他编程语言中),字符串常量值是指那些在程序中直接以字符串字面量形式给出的值。这些值被双引号("")包围,并且一旦在代码中定义,就不能被改变(尽管你可以将字符串变量指向另一个字符串常量或字符串对象的引用)。字符串常量值在编译时会被存储在......
  • EFCore执行自定义SQL时格式化错误:Input string was not in a correct format.
      记录一下EFCore执行自定义SQL报System.FormatException异常的问题,这个异常可能是“Inputstringwasnotinacorrectformat.”,也可能是其它格式化异常,比如:System.ArgumentException:“Formatoftheinitializationstringdoesnotconformtospecificationstartingat......
  • C++ //练习 15.31 已知s1、s2、s3和s4都是string,判断下面的表达式分别创建了什么样的
    C++Primer(第5版)练习15.31练习15.31已知s1、s2、s3和s4都是string,判断下面的表达式分别创建了什么样的对象:(a)Query(s1)|Query(s2)&~Query(s3);(b)Query(s1)|(Query(s2)&~Query(s3));(c)(Query(s1)&(Query(s2))|(Query(s3)&Query(s4)));......
  • 嵌入式学习day9(string函数族)
    一丶strcpy和strncpy1.strcpy    #include<string.h>    char*strcpy(char*dest,constchar*src);    功能:实现字符串复制    参数:char*dest:目标字符串首地址    constchar*src:原字符串首地址    返回值:目标字符串首地......
  • QString成员函数一览表
    QString类是Qt框架中的一个核心类,用于处理Unicode字符串。它提供了大量的成员函数,用于字符串的创建、操作、查询和转换。以下是QString类的一些主要成员函数,按照功能分类:构造和赋值-QString():构造一个空字符串。-QString(constchar*):从ASCII字符串构造。-Q......
  • PgStatement的executeCachedSql(String sql, int flags, String @Nullable [] column
    方法代码如下:privatebooleanexecuteCachedSql(Stringsql,intflags,String@Nullable[]columnNames)throwsSQLException{//第一部分PreferQueryModepreferQueryMode=connection.getPreferQueryMode();booleanshouldUseParameterized=false;......
  • 一、MyString类的实现
    包括默认构造、有参构造、拷贝构造、拷贝赋值运算符、移动构造、移动赋值运算符及析构函数。classMyString{   MyString(constchar*buffer)//有参构造   {       length=strlen(buffer);       m_char=newchar[length+1];     ......
  • 24-7-31String类,StringBuffer类,StringBuilder类的详解与比较
    24-7-31String类,StringBuffer类,StringBuilder类的详解与比较文章目录24-7-31String类,StringBuffer类,StringBuilder类的详解与比较StringString的结构String的方法String对象的两种创建方法String的其他方法String练习StringExercise01StringExercise02StringExer......
  • 5.String类型的常见命令
    String类型,字符串类型,是redis中最简单的存储类型。根据字符串的格式不同分为三类:1.string普通字符串。2.int整型类型,可以自增,自减操作。3.float浮点类型,可以自增,自减操作。字符串最大空间不能超过512m。String类型数据的常见操作命令setkeyvalue添加或修改key-v......
  • 题解:CF559B Equivalent Strings
    CF559BEquivalentStrings题解题目描述吐槽一下,题目翻译有歧义。思路分析你会发现,当你需要判断字符串\(a,b\)是否等价时,如果长度为偶数,需要继续判断字符串\(a\)拆分的字串。所用知识s.substr(i,j)//在字符串s中,从位置i开始截取长度为j的字串参考代码#include<bits......