1180. 统计只含单一字母的子串
给你一个字符串
S
,返回只含 单一字母 的子串个数。示例 1:
输入: "aaaba" 输出: 8 解释: 只含单一字母的子串分别是 "aaa", "aa", "a", "b"。 "aaa" 出现 1 次。 "aa" 出现 2 次。 "a" 出现 4 次。 "b" 出现 1 次。 所以答案是 1 + 2 + 4 + 1 = 8。
示例 2:
输入: "aaaaaaaaaa" 输出: 55
提示:
1 <= S.length <= 1000
S[i]
仅由小写英文字母组成。思路:脑筋急转弯
public int countLetters(String S) {
int res = 0;
int len = S.length();
int[] f = new int [len + 1];
f[1] = 1;
for (int i = 2;i <= len;i++) {
f[i] = f[i - 1] + i;
}
int cnt = 1;
char[] chs = S.toCharArray();
for (int i = 0;i < chs.length;i++) {
if (i + 1 < chs.length && chs[i] == chs[i + 1]) {
cnt++;
} else {
res += f[cnt];
cnt = 1;
}
}
return res;
}
1181. 前后拼接
给你一个「短语」列表
phrases
,请你帮忙按规则生成拼接后的「新短语」列表。「短语」(phrase)是仅由小写英文字母和空格组成的字符串。「短语」的开头和结尾都不会出现空格,「短语」中的空格不会连续出现。
「前后拼接」(Before and After puzzles)是合并两个「短语」形成「新短语」的方法。我们规定拼接时,第一个短语的最后一个单词 和 第二个短语的第一个单词 必须相同。
返回每两个「短语」
phrases[i]
和phrases[j]
(i != j
)进行「前后拼接」得到的「新短语」。注意,两个「短语」拼接时的顺序也很重要,我们需要同时考虑这两个「短语」。另外,同一个「短语」可以多次参与拼接,但「新短语」不能再参与拼接。
请你按字典序排列并返回「新短语」列表,列表中的字符串应该是 不重复的 。
示例 1:
输入:phrases = ["writing code","code rocks"] 输出:["writing code rocks"]
示例 2:
输入:phrases = ["mission statement", "a quick bite to eat", "a chip off the old block", "chocolate bar", "mission impossible", "a man on a mission", "block party", "eat my words", "bar of soap"] 输出:["a chip off the old block party", "a man on a mission impossible", "a man on a mission statement", "a quick bite to eat my words", "chocolate bar of soap"]
示例 3:
输入:phrases = ["a","b","a"] 输出:["a"]
提示:
1 <= phrases.length <= 100
1 <= phrases[i].length <= 100
思路:没啥好说的,第一个单词作为Key,将字符串存入HashMap,暴力拼接,TreeSet排序加去重
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.Map.Entry;
class Help{
String headString;
String string;
String laString;
int index;
//ArrayList<String> strs;
public Help(String headString,String laString,String string,int index) {
// TODO Auto-generated constructor stub
this.headString = headString;
this.laString =laString;
this.string = string;
this.index = index;
}
}
class Solution {
public List<String> beforeAndAfterPuzzles(String[] phrases) {
TreeSet<String> resSet = new TreeSet<String>();
HashMap<String,ArrayList<Help>> map = new HashMap<String, ArrayList<Help>>();
int index = 0;
for (String string : phrases) {
String[] strs = string.split(" ");
ArrayList<Help> helpList = map.getOrDefault(strs[0], new ArrayList<>());
Help help = new Help(strs[0], strs[strs.length - 1],string,index++);
helpList.add(help);
map.put(strs[0], helpList);
}
for (Entry<String, ArrayList<Help>> entry : map.entrySet()) {
ArrayList<Help> helpList =entry.getValue();
for (Help help : helpList) {
ArrayList<Help> helpList2 = map.get(help.laString);
if (helpList2 == null || helpList2.size() == 0)
continue;
for (Help help2 : helpList2) {
if (help.index == help2.index)
continue;
String[] strs2 = help2.string.split(" ");
StringBuilder tempString = new StringBuilder(help.string);
for(int i = 1;i < strs2.length;i++) {
tempString.append(" " + strs2[i]);
}
//tempString.deleteCharAt(tempString.length() - 1);
resSet.add(tempString.toString());
}
}
}
List<String> res = new ArrayList<String>();
for (String string : resSet) {
res.add(string);
}
return res;
}
}
1182. 与目标颜色间的最短距离
给你一个数组
colors
,里面有1
、2
、3
三种颜色。我们需要在
colors
上进行一些查询操作queries
,其中每个待查项都由两个整数i
和c
组成。现在请你帮忙设计一个算法,查找从索引
i
到具有目标颜色c
的元素之间的最短距离。如果不存在解决方案,请返回
-1
。示例 1:
输入:colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]] 输出:[3,0,3] 解释: 距离索引 1 最近的颜色 3 位于索引 4(距离为 3)。 距离索引 2 最近的颜色 2 就是它自己(距离为 0)。 距离索引 6 最近的颜色 1 位于索引 3(距离为 3)。
示例 2:
输入:colors = [1,2], queries = [[0,3]] 输出:[-1] 解释:colors 中没有颜色 3。
提示:
1 <= colors.length <= 5*10^4
1 <= colors[i] <= 3
1 <= queries.length <= 5*10^4
queries[i].length == 2
0 <= queries[i][0] < colors.length
1 <= queries[i][1] <= 3
思路:一开始想用二分查找,时间爆了。后面改成存储离该点最近的左边和右边的改颜色的索引。
public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
int len = colors.length;
int[][] nearLeft = new int[3][len];
int[][] nearRight = new int[3][len];
nearLeft[0][0] = nearLeft[1][0] = nearLeft[2][0] = -1;
nearRight[0][len - 1] = nearRight[1][len - 1] = nearRight[2][len - 1] = -1;
for (int index = 0;index < len;index++) {
if (index > 0) {
for (int i = 0;i < 3;i++) {
nearLeft[i][index] = nearLeft[i][index - 1];
nearRight[i][len - index - 1] = nearRight[i][len - index];
}
}
nearLeft[colors[index] - 1][index] = index;
nearRight[colors[len - 1 - index] - 1][len - 1 - index] = len - 1 - index;
}
List<Integer> res = new ArrayList<Integer>();
for (int[] q : queries) {
int c = q[1] - 1,p = q[0];
int absLeft = Math.abs(p - nearLeft[c][p]);
int absRight = Math.abs(p - nearRight[c][p]);
if (nearLeft[c][p] == -1 && nearRight[c][p] == -1)
res.add(-1);
else if (nearLeft[c][p] != -1 && nearRight[c][p] != -1)
res.add(Math.min(absLeft,absRight));
else
res.add(Math.abs(1 + nearLeft[c][p] + nearRight[c][p] - p));
}
return res;
}