image_2022-12-13_01-43-00.png
12 KB
#N205. Isomorphic Strings
problem link
#solution
problem link
#solution
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> map = new HashMap<>();
Map<Character, Character> map1 = new HashMap<>();
for(int i=0; i<s.length(); i++){
if(map.containsKey(s.charAt(i)) && map.get(s.charAt(i))!=t.charAt(i)
|| map1.containsKey(t.charAt(i)) && map1.get(t.charAt(i))!=s.charAt(i)){
return false;
}else if(!map.containsKey(s.charAt(i))){
map.put(s.charAt(i), t.charAt(i));
map1.put(t.charAt(i), s.charAt(i));
}
}
return true;
}
}⚡3👍1
image_2022-12-15_00-24-17.png
29.5 KB
Runtime 13 ms Beats 95.50%#N703. Kth Largest Element in a Stream
Memory 46.2 MB Beats 91.49%
problem link
#solution
class KthLargest {
int k;
PriorityQueue<Integer> pq;
public KthLargest(int k, int[] nums) {
pq=new PriorityQueue<>();
for(int n: nums) pq.offer(n);
this.k=k;
while(pq.size()>k) pq.poll();
}
public int add(int val) {
pq.offer(val);
if(pq.size()>k) pq.poll();
return pq.peek();
}
}⚡3👍1