문제풀이 2021 02 01
2021.02.01 연습
- 사용언어 : java
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
HashSet<Character> set = new HashSet<>();
int count = 0;
boolean check = true;
for (char c : allowed.toCharArray()) {
set.add(c);
}
for (String s : words) {
for (char sc : s.toCharArray()) {
if (!set.contains(sc)) {
check = false;
}
}
if (check) {count++;}
check = true;
}
return count;
}
}
Runtime: 12 ms, faster than 46.03% of Java online submissions for Count the Number of Consistent Strings. Memory Usage: 39.8 MB, less than 57.73% of Java online submissions for Count the Number of Consistent Strings.
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int sum = 0;
for (int i =0; i < arr.length; i++) {
int subsum = 0;
for (int j = i; j < arr.length ; j++) {
subsum += arr[j];
if ( (j-i+1)%2 == 1 ) {
sum += subsum;
}
}
}
return sum;
}
}
Runtime: 1 ms, faster than 67.43% of Java online submissions for Sum of All Odd Length Subarrays. Memory Usage: 36.8 MB, less than 32.83% of Java online submissions for Sum of All Odd Length Subarrays.