问题是: 给定仅由小写字母组成的字符串数组A,返回列表中所有字符串(包括重复项)中显示的所有字符的列表。例如,如果某个字符在所有字符串中出现3次,但没有出现4次,那么您需要在最终答案中将该字符包含3次。
您可以按任何顺序返回答案。
示例1:
输入:[“ bella”,“ label”,“ roller”] 输出:[“ e”,“ l”,“ l”]
示例2:
输入:[“ cool”,“ lock”,“ cook”] 输出:[“ c”,“ o”]
注意:
1 <=长度<= 100
1 <= A [i]。长度<= 100
A [i] [j]是小写字母
这是我的代码:
class Solution {
public List<String> commonChars(String[] A) {
List<String> charArr = new ArrayList<>();
for(int i = 1; i<A.length ; i++){
A[0].replaceAll("[^" + A[i] + "]", "");
}
for(int i=0; i<A[0].length(); i++){
charArr.add(Character.toString(A[0].charAt(i)));
}
return charArr;
}
}
我得到的结果
输入: [“ bella”,“ label”,“ roller”]
输出: [“ b”,“ e”,“ l”,“ l”,“ a”]
预期: [“ e”,“ l”,“ l”]
显然字符没有被删除,有人可以帮助我解决这个问题吗?
答案 0 :(得分:0)
以下是我如何解决此问题的伪代码:
Place the letters from the first string of the array into a list
for each letter in the list{
count the occurrences of the letter
for each string in the array{
count the occurrences of the letter
if the letter occurs in the list more often than in the string{
decrease occurrence of letter in list to match letter in string
}
}
}
希望这会有所帮助!
答案 1 :(得分:0)
不确定java,但这是python中的1个内衬
list(functools.reduce(lambda a,b: a&b, [Counter(c) for c in A]).elements())
>>> from collections import Counter
>>> import functools
>>> A = ["bella","label","roller"]
>>> list(functools.reduce(lambda a,b: a&b, [Counter(c) for c in A]).elements())
['e', 'l', 'l']
>>> A = ["cool","lock","cook"]
>>> list(functools.reduce(lambda a,b: a&b, [Counter(c) for c in A]).elements())
['c', 'o']
>>>
答案 2 :(得分:0)
我用Manaar的想法解决了这个问题, 谢谢大家的建议和评论!
$(document).ready(function(){
var media = $('video');
function checkmedia(){
// Get current browser top and bottom
var scrollTop = $(window).scrollTop();
var scrollBottom = $(window).scrollTop() + $(window).height();
media.each(function(index, el) {
var height = parseInt($(this).height());
var visiblechunk = height/1.2;
var vidpostop = $(this).offset().top;
var vidposbottom = $(this).height() + vidpostop;
/* Only play when a certain portion of the video is visible */
vidpostop = vidpostop+visiblechunk;
vidposbottom = vidposbottom - visiblechunk;
if(scrollTop < vidposbottom && scrollBottom > vidpostop){
if($(this).hasClass('autoplayed')){
}else{
$(this).get(0).play();
$(this).addClass('autoplayed');
}
}else{
$(this).get(0).pause();
$(this).removeClass('autoplayed');
}
});
}
$(window).scroll(function(){
checkmedia();
})
})
答案 3 :(得分:0)
略有改动的版本:
public List<String> commonChars01(String[] A) {
int[][] lowerCase = new int[26][A.length];
for(int i = 0 ; i< A.length ; i++){
for(int j = 0; j < A[i].length(); j++) {
lowerCase[A[i].charAt(j) -'a'][i] ++;
}
}
List<String> list = new ArrayList<>();
for (int i = 0; i < 26; i++) {
// all the elements of the array should be equal
long count = IntStream.of(lowerCase[i])
.boxed().distinct().count();
// [0, 0, 0] should fail, hence sum should be greater than or equal to 3.
int sum = IntStream.of(lowerCase[i])
.boxed().reduce(0, (a,b) -> a + b);
if ( sum >= 3 && count == 1) {
int timesRepeated = lowerCase[i][0];
while (timesRepeated != 0) {
list.add(Character.toString((char)(i + 'a')));
timesRepeated--;
}
}
}
return list;
}