Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected RansomNote() {}
private static final String NO = "No";

/** checkMagazineCompute. */
@SuppressWarnings("java:S7467")
public static boolean checkMagazineCompute(List<String> magazine, List<String> note) {
Map<String, Integer> dictionary = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand All @@ -19,6 +20,8 @@
public class SherlockAndAnagrams {
private SherlockAndAnagrams() {}

private static final Pattern EMPTY_REGEX = Pattern.compile("");

/** factorial(). */
public static BigInteger factorial(int number) {
BigInteger result = BigInteger.ONE;
Expand All @@ -42,7 +45,7 @@ public static int sherlockAndAnagrams(String sWord) {
// Add substrings to a candidate list.
// two strings are anagrams if sorted strings are the same.
String anagramCandidate =
Arrays.stream(substr.split("")).sorted().collect(Collectors.joining());
Arrays.stream(EMPTY_REGEX.split(substr)).sorted().collect(Collectors.joining());

// Append candidates to dictionary by "sorted string" key
if (candidates.containsKey(anagramCandidate)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* TwoStrings.
Expand All @@ -20,19 +20,10 @@ private TwoStrings() {}
/** twoStringsCompute. */
public static boolean twoStringsCompute(String s1, String s2) {

Set<String> dictionary = new HashSet<>();
Set<String> dictionary =
s1.chars().mapToObj(c -> String.valueOf((char) c)).collect(Collectors.toSet());

for (char occurrence : s1.toCharArray()) {
dictionary.add("%c".formatted(occurrence));
}

for (char occurrence : s2.toCharArray()) {
if (dictionary.contains("%c".formatted(occurrence))) {
return true;
}
}

return false;
return s2.chars().mapToObj(c -> String.valueOf((char) c)).anyMatch(dictionary::contains);
}

public static String twoStrings(String s1, String s2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static Integer problem0005(Integer bottom, Integer top) {
minimumPrimeFactorsCollector =
mixOccurrences(minimumPrimeFactorsCollector, primeFactorsCount);

log = String.format("%s", minimumPrimeFactorsCollector.toString());
log = String.valueOf(minimumPrimeFactorsCollector);
Log.info(log);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ae.projecteuler;

import ae.projecteuler.helper.NumberToWord;
import java.util.regex.Pattern;
import util.Log;

/**
Expand All @@ -12,6 +13,8 @@ public class Problem0017 {

private Problem0017() {}

private static final Pattern NON_ALPHANUMERIC = Pattern.compile("[^A-Za-z0-9]");

/** Problem 0017. */
public static Integer problem0017(int init, int last) throws Exception {

Expand All @@ -21,7 +24,7 @@ public static Integer problem0017(int init, int last) throws Exception {
for (int i = init; i <= last; i++) {
String word = NumberToWord.numberToWord(i);

String replaced = word.replaceAll("[^A-Za-z0-9]", "");
String replaced = NON_ALPHANUMERIC.matcher(word).replaceAll("");

acum += replaced.length();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public static Integer problem0018(Integer[][] triangle) {
BinaryNode<Integer> tree = builder.buildBinaryNodeTreeWithWeight(triangle);
Integer[] leaves = builder.getLeaves();

log = String.format("Triangle: %s", tree);
log = "Triangle: " + tree;
Log.debug(log);
log = String.format("Leaves count: %d, %s", leaves.length, leaves);
log = "Leaves count: " + leaves.length + ", " + Arrays.toString(leaves);
Log.debug(log);

Arrays.sort(leaves);

Integer result = leaves[leaves.length - 1]; // maximum is last element

log = String.format("Problem 00018 solved: %d", result);
log = "Problem 00018 solved: " + result;
Log.debug(log);

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,27 @@ public static String numberToWord(int value) throws Exception {
return dictionary.get(Integer.toString(dec));
}

return String.format(
"%s-%s", dictionary.get(Integer.toString(dec)), dictionary.get(Integer.toString(unit)));
return dictionary.get(Integer.toString(dec)) + "-" + dictionary.get(Integer.toString(unit));

} else if (value <= 999) {
// 100 to 999
int rest = (int) Math.floor((double) value % 100);
int cent = (int) Math.floor((double) value / 100);

if (rest == 0) {
return String.format("%s %s", dictionary.get(Integer.toString(cent)), NumberToWord.CENTS);
return dictionary.get(Integer.toString(cent)) + " " + NumberToWord.CENTS;
}

return String.format(
"%s %s and %s",
dictionary.get(Integer.toString(cent)), NumberToWord.CENTS, numberToWord(rest));
return dictionary.get(Integer.toString(cent))
+ " "
+ NumberToWord.CENTS
+ " and "
+ numberToWord(rest);
} else if (value == 1000) {
// up to 1000
int mills = (int) Math.floor((double) value / 1000);

return String.format("%s %s", dictionary.get(Integer.toString(mills)), NumberToWord.MILLS);
return dictionary.get(Integer.toString(mills)) + " " + NumberToWord.MILLS;
}

throw new IllegalArgumentException("Invalid value");
Expand Down
Loading