最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 1763. Longest Nice Substring

2023-04-09 10:15 作者:您是打尖兒還是住店呢  | 我要投稿

A string?s?is?nice?if, for every letter of the alphabet that?s?contains, it appears?both?in uppercase and lowercase. For example,?"abABB"?is nice because?'A'?and?'a'?appear, and?'B'?and?'b'?appear. However,?"abA"?is not because?'b'?appears, but?'B'?does not.

Given a string?s, return?the longest?substring?of?s?that is?nice. If there are multiple, return the substring of the?earliest?occurrence. If there are none, return an empty string.

?

Example 1:

Input: s = "YazaAay"

Output: "aAa"

Explanation: "aAa" is a nice string because 'A/a' is the only letter of the alphabet in s, and both 'A' and 'a' appear. "aAa" is the longest nice substring.

Example 2:

Input: s = "Bb"

Output: "Bb"

Explanation: "Bb" is a nice string because both 'B' and 'b' appear. The whole string is a substring.

Example 3:

Input: s = "c"

Output: ""

Explanation: There are no nice substrings.

?

Constraints:

  • 1 <= s.length <= 100

  • s?consists of uppercase and lowercase English letters.‘

  • 題目不難,就是寫的code長了一點,先是要判斷是否是nice string,用數(shù)組最快了,

  • 然后遍歷整個字符串,條件是當j-i+1沒在hashmap中,因為如果在了,說明前面已經(jīng)有這么長的字符串滿足要求了,所以就不需要判斷的,如果是nice string,就把長度跟左右端的index放到map中,然后遍歷map,找到最長的,返回即可(如果map.size>1的情況下),map如果是空的就返回空字符串即可。

Runtime:?13 ms, faster than?30.49%?of?Java?online submissions for?Longest Nice Substring.

Memory Usage:?42.6 MB, less than?30.32%?of?Java?online submissions for?Longest Nice Substring.


LeetCode 1763. Longest Nice Substring的評論 (共 條)

分享到微博請遵守國家法律
东乡族自治县| 灌云县| 邹平县| 平和县| 泌阳县| 南澳县| 嘉禾县| 潮安县| 丰城市| 西丰县| 富锦市| 巨鹿县| 灵台县| 水城县| 卢氏县| 福安市| 大石桥市| 中卫市| 石阡县| 赤壁市| 修水县| 汤原县| 公安县| 新绛县| 龙陵县| 汝阳县| 广灵县| 大渡口区| 永安市| 合水县| 宜昌市| 武安市| 石阡县| 临洮县| 阳原县| 日照市| 平山县| 缙云县| 渝中区| 修文县| 承德县|