|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +/** |
| 13 | + * 1258. Synonymous Sentences |
| 14 | + * |
| 15 | + * Given a list of pairs of equivalent words synonyms and a sentence text, Return all possible synonymous sentences sorted lexicographically. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: |
| 19 | + * synonyms = [["happy","joy"],["sad","sorrow"],["joy","cheerful"]], |
| 20 | + * text = "I am happy today but was sad yesterday" |
| 21 | + * Output: |
| 22 | + * ["I am cheerful today but was sad yesterday", |
| 23 | + * "I am cheerful today but was sorrow yesterday", |
| 24 | + * "I am happy today but was sad yesterday", |
| 25 | + * "I am happy today but was sorrow yesterday", |
| 26 | + * "I am joy today but was sad yesterday", |
| 27 | + * "I am joy today but was sorrow yesterday"] |
| 28 | + * |
| 29 | + * Constraints: |
| 30 | + * 0 <= synonyms.length <= 10 |
| 31 | + * synonyms[i].length == 2 |
| 32 | + * synonyms[0] != synonyms[1] |
| 33 | + * All words consist of at most 10 English letters only. |
| 34 | + * text is a single space separated sentence of at most 10 words. |
| 35 | + * */ |
| 36 | +public class _1258 { |
| 37 | + public static class Solution1 { |
| 38 | + public List<String> generateSentences(List<List<String>> synonyms, String text) { |
| 39 | + String[] words = text.split(" "); |
| 40 | + Map<String, Set<String>> map = buildSynonymDict(words, synonyms); |
| 41 | + Set<String> result = new HashSet<>(); |
| 42 | + result.add(text); |
| 43 | + for (int i = 0; i < words.length; i++) { |
| 44 | + List<String> list = new ArrayList<>(); |
| 45 | + for (String next : result) { |
| 46 | + list.add(next); |
| 47 | + list.addAll(findAllSynonymForThisWord(next, i, map)); |
| 48 | + } |
| 49 | + result.addAll(list); |
| 50 | + } |
| 51 | + List<String> list = new ArrayList<>(); |
| 52 | + list.addAll(result); |
| 53 | + Collections.sort(list); |
| 54 | + return list; |
| 55 | + } |
| 56 | + |
| 57 | + private List<String> findAllSynonymForThisWord(String sentence, int i, Map<String, Set<String>> map) { |
| 58 | + String[] words = sentence.split(" "); |
| 59 | + List<String> list = new ArrayList<>(); |
| 60 | + Set<String> synonyms = map.get(words[i]); |
| 61 | + for (String s : synonyms) { |
| 62 | + words[i] = s; |
| 63 | + list.add(formWord(words)); |
| 64 | + } |
| 65 | + return list; |
| 66 | + } |
| 67 | + |
| 68 | + private String formWord(String[] words) { |
| 69 | + StringBuilder sb = new StringBuilder(); |
| 70 | + for (String word : words) { |
| 71 | + sb.append(word); |
| 72 | + sb.append(" "); |
| 73 | + } |
| 74 | + return sb.substring(0, sb.length() - 1); |
| 75 | + } |
| 76 | + |
| 77 | + private Map<String, Set<String>> buildSynonymDict(String[] words, List<List<String>> synonyms) { |
| 78 | + Map<String, Set<String>> map = new HashMap<>(); |
| 79 | + for (String key : words) { |
| 80 | + if (!map.containsKey(key)) { |
| 81 | + map.put(key, new HashSet<>()); |
| 82 | + } |
| 83 | + map.get(key).add(key); |
| 84 | + } |
| 85 | + for (List<String> list : synonyms) { |
| 86 | + for (String key : map.keySet()) { |
| 87 | + if (map.get(key).contains(list.get(0))) { |
| 88 | + map.get(key).add(list.get(1)); |
| 89 | + } else if (map.get(key).contains(list.get(1))) { |
| 90 | + map.get(key).add(list.get(0)); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return map; |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | +} |
0 commit comments