Given a magazine with famous quotes, a note needs to be created using words from magazine only. Make sure words are case sensitive.
package org.wesome.dsalgo;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class NoteFromMagazine {
public static boolean noteFromMagazine(List<String> magazine, List<String> note) {
if (Objects.isNull(magazine) || Objects.isNull(note)) {
return false;
}
HashMap<String, Integer> magazineHm = new HashMap<>();
HashMap<String, Integer> noteHm = new HashMap<>();
boolean result = true;
for (int i = 0; i < magazine.size(); i++) {
if (magazineHm.containsKey(magazine.get(i))) {
magazineHm.put(magazine.get(i), magazineHm.get(magazine.get(i)) + 1);
} else magazineHm.put(magazine.get(i), 1);
}
for (int i = 0; i < note.size(); i++) {
if (noteHm.containsKey(note.get(i))) {
noteHm.put(note.get(i), noteHm.get(note.get(i)) + 1);
} else noteHm.put(note.get(i), 1);
}
for (String s : noteHm.keySet()) {
if (noteHm.get(s) <= magazineHm.getOrDefault(s, 0)) {
System.out.println(s + " is required in Notes " + noteHm.get(s) + " times, and is present in magazine in " + magazineHm.get(s) + " times");
} else {
result = false;
}
}
return result;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
public class NoteFromMagazineTest {
@Test
void noteFromMagazineTest1() {
List<String> magazine = Arrays.asList("your", "time", "is", "limited,", "so", "don't", "waste", "it", "living", "someone", "else's", "life");
List<String> note = Arrays.asList("don't", "waste", "your", "time");
boolean noteFromMagazine = NoteFromMagazine.noteFromMagazine(magazine, note);
Assertions.assertTrue(noteFromMagazine);
}
@Test
void noteFromMagazineTest2() {
List<String> magazine = Arrays.asList("Houston,", "we", "have", "a", "problem");
List<String> note = Arrays.asList("i", "have", "a", "problem");
boolean noteFromMagazine = NoteFromMagazine.noteFromMagazine(magazine, note);
Assertions.assertFalse(noteFromMagazine);
}
@Test
void noteFromMagazineTest3() {
List<String> magazine = Arrays.asList("It", "is", "not", "our", "abilities", "that", "show", "what", "we", "truly", "are…", "it", "is", "our", "choices");
List<String> note = Arrays.asList("it", "is", "your", "choices");
boolean noteFromMagazine = NoteFromMagazine.noteFromMagazine(magazine, note);
Assertions.assertFalse(noteFromMagazine);
}
@Test
void noteFromMagazineTest4() {
List<String> magazine = Arrays.asList("Oh", "yes,", "the", "past", "can", "hurt.", "But", "you", "can", "either", "run", "from", "it,", "or", "learn", "from", "it.");
List<String> note = Arrays.asList("either", "learn", "or", "run");
boolean noteFromMagazine = NoteFromMagazine.noteFromMagazine(magazine, note);
Assertions.assertTrue(noteFromMagazine);
}
@Test
void noteFromMagazineTest5() {
List<String> magazine = Arrays.asList("I", "believe", "whatever", "doesn't", "kill", "you" + ",", "simply", "makes", "you" + "stranger.");
List<String> note = Arrays.asList("I", "make", "you", "believe");
boolean noteFromMagazine = NoteFromMagazine.noteFromMagazine(magazine, note);
Assertions.assertFalse(noteFromMagazine);
}
}
plugins {
id 'java'
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}