Re: [閒聊] 每日LeetCode
https://leetcode.com/problems/largest-substring-between-two-equal-characters/description
1624. Largest Substring Between Two Equal Characters
給你一個字串s,求出一個最長子字串,他的左右兩邊需存在一個相同的字元,如果不存
在相同字元返回 -1。
思路:
1.找到每個字元的最左和最右出現的位置,並取寬度最大的返回。
Java Code:
--------------------------------------------
class Solution {
public int maxLengthBetweenEqualCharacters(String s) {
int[] first = new int[26];
int[] last = new int[26];
for (int i = 0; i < s.length(); i++) {
last[s.charAt(i) - 'a'] = i;
}
for (int i = s.length() - 1; i >= 0; i--) {
first[s.charAt(i) - 'a'] = i;
}
int res = -1;
for (int i = 0; i < 26; i++) {
res = Math.max(res, last[i] - first[i] - 1);
}
return res;
}
}
--------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1704003003.A.F95.html
推
12/31 14:14,
1年前
, 1F
12/31 14:14, 1F
→
12/31 14:16,
1年前
, 2F
12/31 14:16, 2F
→
12/31 14:17,
1年前
, 3F
12/31 14:17, 3F
討論串 (同標題文章)
完整討論串 (本文為第 590 之 719 篇):