<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-container {
display: none;
position: absolute;
width: 150px;
border: 1px solid #000;
}
.show {
display: block;
}
</style>
<script>
const toggle = (e) => {
document.getElementById('req')
.classList.toggle('show');
e.stopPropagation();
};
const bodyHandle = (e) => {
if (document.getElementById('req')
.classList.contains('show')) {
document.getElementById('req')
.classList.toggle('show');
}
};
</script>
</head>
<div onclick="bodyHandle(event)"
style="width: 100vw;
height: 100vh;
background-color: lightgreen;">
<div class="dropdown" onclick="toggle(event)"
style="background-color: skyblue;">
<button id="button">Click</button>
<div id="req" class="dropdown-container"
style="background-color: lightyellow;">
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
</div>
</div>
</div>
</body>
</html>
Author: Shiva Charan Devabhaktuni
Shifting Letters – Java O(n)
class Solution {
public String shiftingLetters(String S, int[] shifts) {
int n = shifts.length;
int prev = 0;
char[] arr = S.toCharArray();
for (int i = n - 1; i >= 0; i--) {
prev += shifts[i] % 26;
arr[i] = getNextChar(arr[i], prev);
}
return new String(arr);
}
public char getNextChar(char a, int diff) {
return (char)((((a - 'a') + diff) % 26) + 'a');
}
}
Drag Drop ES6
The following code demonstrates drag drop functionality in ES6.
<script>
const onDrop = (e) => {
e.preventDefault();
e.target.appendChild(document.getElementById(e.dataTransfer.getData('target')));
};
const onDragOver = (e) => e.preventDefault();
const onDragStart = (e) => e.dataTransfer.setData('target', e.target.id);
</script>
<div id="dest" ondrop="onDrop(event)" ondragover="onDragOver(event)" style="height: 200px; width: 200px; border: 1px solid #000; padding: 10px; box-sizing: border-box;"></div>
<img id="source" draggable="true" ondragstart="onDragStart(event)" src="http://via.placeholder.com/180x180" />
Loader ES6
The following is the implementation of a simple Javascript (ES6) loader.
<script>
const load = () => {
let width = 0;
const id = setInterval(() => {
if (width === 100)
clearInterval(id);
else
document.getElementById('bar').style.width = `${++width}%`;
}, 10);
};
</script>
<div id="loader" style="height: 100px; width: 600px; border: 1px solid #000;">
<div id="bar" style="background-color: green; width: 0; height: 100px;" class="">
</div>
</div>
<button onclick="load()">Load</button>
Add Events ES6
In the example below, we create a button and attach a click event to it.
const button = document.createElement('button');
button.innerHTML = 'Click';
document.body.appendChild(button);
const test = (val) => {
console.log('clicked ' + val);
};
button.addEventListener('click', test.bind(this, 'test'));
Debounce (ES6)
Sometimes, we would like to wait a certain minimum time period before successive method calls. For example in events like onscroll(), onresize(), this can go a long way in terms of optimizing the page performance as a whole.
The wrapping of the methods using a Debounce method is a simple way to achieve the desired result.
const debounce = (fn, interval) => { let timeout; return (...rest) => { if (timeout) clearTimeout(timeout); timeout = setTimeout(() => { fn.apply(this, rest); }, interval); }; } const test = (inp) => { console.log(inp); }; const db = debounce(test, 1000); const cb = () => { db('test'); }; const button = document.createElement('button'); button.innerHTML = 'fire'; document.body.appendChild(button); button.addEventListener('click', cb);
Minimum Window Substring – Java
The brute force method would be calculate all the substrings, which would be inefficient.
Runtime Complexity O(m + n)
Space Complexity O(m+n)
class Solution { public String minWindow(String s, String t) { int tLen = t.length(); int sLen = s.length(); if (tLen > sLen) return ""; int[] pattern = new int[256]; int[] given = new int[256]; for (int i = 0; i < tLen; i++) pattern[t.charAt(i)]++; int mstart = 0, size = 0, start = 0, min = sLen + 1; for (int j = 0; j < s.length(); j++) { char curr = s.charAt(j); if (given[curr] < pattern[curr]) size++; given[curr]++; if (size == tLen) { while (given[s.charAt(start)] > pattern[s.charAt(start)]) { given[s.charAt(start)]--; start++; } int len = j - start + 1; if (len < min) { min = len; mstart = start; } } } if (size < tLen) return ""; return s.substring(mstart, mstart + min); } }
UTF-8 Validation – Java
Hint UTF-8 ranges between 1 to 4 bytes (8-bits).
Runtime: O(n)
class Solution { public boolean validUtf8(int[] data) { int n = data.length; int skip = 0b10000000; int check = 0; for (int currByte: data) { if (check > 0) { if ((currByte & skip) == skip) check--; else return false; } else { check = getHeadType(currByte); if (check < 0) return false; } } return check == 0; } public int getHeadType(int num) { if ((num & 0b11110000) == 0b11110000 && (num & 0b00001000) != 0b00001000) return 3; if ((num & 0b11100000) == 0b11100000 && (num & 0b00010000) != 0b00010000) return 2; if ((num & 0b11000000) == 0b11000000 && (num & 0b00100000) != 0b00100000) return 1; if ((num & 0b10000000) == 0b10000000) return -1; //error return 0; } }
K Empty Slots – Java
Time O(nlogn)
Space O(n)
class Solution { public int kEmptySlots(int[] flowers, int k) { if (flowers.length == 1 && k == 0) return 1; TreeSet < Integer > set = new TreeSet < Integer > (); for (int i = 0; i < flowers.length; i++) { int curr = flowers[i]; Integer higher = set.higher(curr); if (higher != null && higher - curr == k + 1) { return i + 1; } Integer lower = set.lower(curr); if (lower != null && curr - lower == k + 1) { return i + 1; } set.add(curr); } return -1; } }
Top K Frequent Elements – Java
Runtime O(nlogn)
class Solution { public List<Integer> topKFrequent(int[] nums, int k) { HashMap<Integer, Integer> times = new HashMap<Integer, Integer>(); for(int i=0; i<nums.length; i++) { if(times.get(nums[i])!=null) { times.put(nums[i], times.get(nums[i])+1); } else { times.put(nums[i], 1); } } List<Integer> list = new ArrayList<Integer>(times.keySet()); list.sort((a,b)->times.get(b)-times.get(a)); return list.subList(0,k); } }