Skip to content

Instantly share code, notes, and snippets.

View ozkansari's full-sized avatar
🏠
Working from home

Ozkan Sari ozkansari

🏠
Working from home
View GitHub Profile
@ozkansari
ozkansari / CountingConsecutiveElements.java
Last active April 13, 2020 06:30
Given an integer array arr, count element x such that x + 1 is also in arr. If there're duplicates in arr, count them seperately. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3289/
import java.util.*;
import java.util.stream.*;
class CountingConsecutiveElements {
public int countElements(int[] arr) {
Map<Integer, Integer> numberCounts = new HashMap<>();
for(int num : arr) {
Integer wrappedNum = Integer.valueOf(num);
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.*;
class GroupAnagrams2 {
public List<List<String>> groupAnagrams(String[] wordsArray) {
Map<String,List<String>> anagramWords = new HashMap<>();
for(String word : wordsArray) {
String key = calculateKey(word);
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GroupAnagrams {
public static void main(String[] args) {
GroupAnagrams problem = new GroupAnagrams();
List<List<String>> result1 = problem.groupAnagrams(new String[] { "eat", "tea", "tan", "ate", "nat", "bat" });
@ozkansari
ozkansari / MoveZeroesOptimal.java
Created April 12, 2020 18:34
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3286/
import java.util.Arrays;
import java.util.stream.Collectors;
public class MoveZeroesOptimal {
public static void main(String[] args) {
MoveZeroesOptimal problem = new MoveZeroesOptimal();
int[] result1 = problem.calculate(new int[] { 0, 1, 0, 3, 12 });
System.out.println(
"Expected: [1,3,12,0,0], " + "Result: " + Arrays.stream(result1).boxed().collect(Collectors.toList()));
@ozkansari
ozkansari / MoveZeroes.java
Created April 12, 2020 18:22
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3286/
import java.util.Arrays;
import java.util.stream.Collectors;
public class MoveZeroes {
public static void main(String[] args) {
MoveZeroes problem = new MoveZeroes();
int[] result1 = problem.calculate(new int[] { 0, 1, 0, 3, 12 });
System.out.println(
"Expected: [1,3,12,0,0], " + "Result: " + Arrays.stream(result1).boxed().collect(Collectors.toList()));
@ozkansari
ozkansari / MaxSubArray.java
Created April 12, 2020 17:21
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3285/
public class MaxSubArray {
public static void main(String[] args) {
MaxSubArray problem = new MaxSubArray();
int result1 = problem.calculate(new int[] { -2,1,-3,4,-1,2,1,-5,4 });
System.out.println("Expected: 6, Result: " + result1);
}
public int calculate(int[] nums) {
@ozkansari
ozkansari / HappyNumber.java
Created April 12, 2020 16:34
Write an algorithm to determine if a number n is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Th…
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class HappyNumber {
public static void main(String[] args) {
HappyNumber problem = new HappyNumber();
boolean result1 = problem.isHappy(19);
@ozkansari
ozkansari / LinkedListMiddleNodeFastPointer.java
Created April 12, 2020 16:03
Middle of the Linked List : Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class LinkedListMiddleNodeFastPointer {
public ListNode middleNode(ListNode head) {
@ozkansari
ozkansari / LinkedListMiddleNode.java
Created April 12, 2020 14:46
Middle of the Linked List : Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class LinkedListMiddleNode {
@ozkansari
ozkansari / BinaryTreeDiameterCalculatorWithRecursion.java
Created April 12, 2020 13:59
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3293/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/