Saturday, May 21, 2022

Two Sum

 


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TwoSum {

@Test
public void test() {

int[] array = new int[]{2, 7, 11, 15};
int target = 9;
int[] result = new int[]{0, 1};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));

array = new int[]{3, 2, 4};
target = 6;
result = new int[]{1, 2};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));

array = new int[]{3, 3};
target = 6;
result = new int[]{0, 1};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));
}

public int[] twoSum(int[] array, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
return new int[]{map.get(array[i]), i};
}
map.put(target - array[i], i);
}
return null;
}
}

Valid Anagram



import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class ValidAnagram {

@Test
public void test() {
String s = "anagram";
String t = "nagaram";
Assertions.assertTrue(isAnagram(s, t));
Assertions.assertTrue(isAnagramWithKey(s, t));

String a = "rat";
String b = "cat";
Assertions.assertFalse(isAnagram(a, b));
Assertions.assertFalse(isAnagramWithKey(a, b));

}

public boolean isAnagram(String s, String t) {
int[] array = new int[128];
for (char c : s.toCharArray()) {
array[c]++;
}
for (char c : t.toCharArray()) {
array[c]--;
}
for (int a : array) {
if (a != 0) {
return false;
}
}
return true;
}

public boolean isAnagramWithKey(String s, String t) {
return key(s).equals(key(t));
}

public String key(String s) {
int[] array = new int[128];
for (char c : s.toCharArray()) {
array[c]++;
}
return Arrays.toString(array);
}
}

Contains Duplicate

 

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ContainsDuplicate {

@Test
public void test() {
int[] a = {1, 2, 3, 1};
assertTrue(containsDuplicateWithSet(a));
assertTrue(containsDuplicateWithSort(a));
assertTrue(containsDuplicateWithStream(a));

int[] b = {1, 2, 3, 4};
assertFalse(containsDuplicateWithSet(b));
assertFalse(containsDuplicateWithSort(b));
assertFalse(containsDuplicateWithStream(b));

int[] c = {1, 1, 1, 3, 3, 4, 3, 2, 4, 2};
assertTrue(containsDuplicateWithSet(c));
assertTrue(containsDuplicateWithSort(c));
assertTrue(containsDuplicateWithStream(c));
}

public boolean containsDuplicateWithSet(int[] array) {
Set<Integer> set = new HashSet<>();
for (int num : array) {
if (!set.add(num)) {
return true;
}
}
return false;
}

public boolean containsDuplicateWithSort(int[] array) {
Arrays.sort(array);
for (int i = 1; i < array.length; i++) {
if (array[i - 1] == array[i]) {
return true;
}
}
return false;
}

public boolean containsDuplicateWithStream(int[] array){
return Arrays.stream(array).distinct().count()!=array.length;
}
}