Skip to content

Instantly share code, notes, and snippets.

View riyafa's full-sized avatar

Riyafa Abdul Hameed riyafa

View GitHub Profile
@riyafa
riyafa / echo.bal
Created May 19, 2020 07:42
Simple WebSocket echo server written in Ballerina language
import ballerina/http;
service websocket on new http:Listener(15300) {
resource function onText(http:WebSocketCaller caller, string text,
boolean finalFrame) {
checkpanic caller->pushText(text);
}
}
@riyafa
riyafa / MiddleOfLinkedList.java
Created September 3, 2020 00:28
[Leetcode] 876. Middle of the Linked List
class MiddleOfLinkedList {
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public class HammingDistance {
public int hammingDistance(int x, int y) {
int res = x ^ y;
int count = 0;
while (res > 0) {
res = res & (res - 1);
count++;
}
return count;
}
@riyafa
riyafa / LongestDuplicateSubstring.java
Last active September 8, 2020 14:00
Solution for LeetCode Longest Duplicate Substring
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
public class LongestDuplicateSubstring {
private static final int PRIME = java.math.BigInteger.probablePrime(15, ThreadLocalRandom.current()).intValue();
public String longestDupSubstring(String S) {
@riyafa
riyafa / MoveZeros.java
Created September 8, 2020 14:01
Solution for LeetCode Move Zeroes
public class MoveZeros {
public void moveZeroes(int[] nums) {
int i = 0;
int j = 0;
while(i < nums.length && j < nums.length) {
if(nums[i] != 0) {
i++;
} else if(j <= i || nums[j] == 0) {
j++;
} else {
@riyafa
riyafa / BullsAndCows.java
Created October 9, 2020 03:10
Solution for LeetCode Bulls and Cows
public class BullsAndCows {
public String getHint(String secret, String guess) {
int n = secret.length();
int[] map = new int[10];
int a = 0;
int b = 0;
for (int i = 0; i < n; i++) {
int s = secret.charAt(i) - '0';
int g = guess.charAt(i) - '0';
if (s == g) {
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
int num = nums[i];
if(map.containsKey(num)) {
return new int[]{i, map.get(num)};
} else {
map.put(target - num, i);
}
class SearchInRotatedArray {
public int search(int[] nums, int target) {
return search(nums, 0, nums.length - 1, target);
}
private int search(int[] nums, int start, int end, int target) {
if (start > end) return -1;
int midIdx = start + (end - start) / 2;
int mid = nums[midIdx];
if (target == mid) return midIdx;
class SubarrayProductLessThanK {
public int numSubarrayProductLessThanK(int[] nums, int k) {
if(k <= 1) {
return 0;
}
int count = 0;
int prod = 1;
for(int i =0, j =0; j < nums.length; j++) {
prod *= nums[j];
while(prod >= k) {
@riyafa
riyafa / ArrangingCoins.java
Last active March 15, 2022 07:32
Mathematical solution for 441. Arranging Coins: https://youtu.be/H-Tdu8qJ_uk
class ArrangingCoins {
public int arrangeCoins(int n) {
double result = (-1.0 + Math.sqrt(1.0+8.0*n))/2.0;
return (int)Math.floor(result);
}
}