LeetCode Weekly Contest 484 Discussion and Solution
Priyanshu Chaurasiya
19 days ago
Priyanshu Chaurasiya
19 days ago

Join the conversation by signing in below!
| Problem | Difficulty | Points | Status |
|---|---|---|---|
| 1 | Easy | 3 | Solved |
| 2 | Medium | 4 | Solved |
| 3 | Medium | 4 | Solved |
| 4 | Hard | 6 | Unsolved |
class Solution {
public int residuePrefixes(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0;
for(int i = 0; i < n; i++){
char ch = s.charAt(i);
set.add(ch);
if((i + 1) % 3 == set.size()) ans++;
}
return ans;
}
}class Solution {
public int centeredSubarrays(int[] nums) {
int n = nums.length;
int ans = 0;
for(int i = 0; i < n; i++){
Set<Integer> set = new HashSet<>();
int sum = 0;
for(int j = i; j < n; j++){
sum += nums[j];
set.add(nums[j]);
if(set.contains(sum)) ans++;
}
}
return ans;
}
}class Solution {
public long countPairs(String[] words) {
Map<String, Integer> map = new HashMap<>();
long ans = 0;
for(String word : words){
StringBuilder sb = new StringBuilder();
int baseVal = (int) word.charAt(0);
for(int i = 0; i < word.length(); i++){
int currVal = (int) word.charAt(i);
int relativeVal = (currVal - baseVal + 26) % 26;
sb.append(Integer.toString(relativeVal));
}
String s = sb.toString();
if(map.containsKey(s)) ans += map.get(s);
map.put(s, map.getOrDefault(s, 0) + 1);
}
return ans;
}
}No comments yet, Start the conversation!