Tomas
BAN USERpublic class FindSmartSubstring {
public static void main(String[] args) {
// TODO Auto-generated method stub
String subString = "Featuring stylish rooms and moornings for recreation boats,"
+ " Room Mate Aitana is a designer hotel built in 2013 on an island in the IJ River in Amsterdam.";
System.out.print(findSmartString(subString,30));
}
static String findSmartString(String s,int length) {
String[] arr = s.split(" ");
StringBuilder sb = new StringBuilder();
int total = 0;
for(String str:arr) {
total = total + str.length();
if(total > length)
break;
sb.append(str+" ");
}
return sb.toString();
}
}
public class FindSmartSubstring {
public static void main(String[] args) {
// TODO Auto-generated method stub
String subString = "Featuring stylish rooms and moornings for recreation boats,"
+ " Room Mate Aitana is a designer hotel built in 2013 on an island in the IJ River in Amsterdam.";
System.out.print(findSmartString(subString,30));
}
static String findSmartString(String s,int length) {
String[] arr = s.split(" ");
StringBuilder sb = new StringBuilder();
int total = 0;
for(String str:arr) {
total = total + str.length();
if(total > length)
break;
sb.append(str+" ");
}
return sb.toString();
}
}
public static void main(String[] args) {
List<String> query = new ArrayList<String>(Arrays.asList("good","bad"));
Map<Integer,Integer> result = new HashMap<Integer,Integer>();
Map<Integer,List<String>> reviews = new HashMap<Integer, List<String>>();
reviews.put(1, new ArrayList<String>(Arrays.asList("good", "great", "awesome", "good")));
reviews.put(2, new ArrayList<String>(Arrays.asList("nasty", "bad", "disgusting")));
reviews.put(3, new ArrayList<String>(Arrays.asList("moderate", "good", "bad")));
for(Map.Entry<Integer, List<String>> entry: reviews.entrySet()) {
List<String> description = new ArrayList<String>(entry.getValue());
description.retainAll(query);
result.put(entry.getKey(),description.size());
}
Map<Integer,Integer> sortMap = sortByValue(result);
for(Map.Entry<Integer,Integer> rating : sortMap.entrySet()) {
System.out.println("hotel: " + rating.getKey() + ", rating: " + rating.getValue());
}
}
static HashMap<Integer,Integer> sortByValue(Map<Integer, Integer> unsortedMap){
LinkedHashMap<Integer,Integer> sortedMap = new LinkedHashMap<>();
unsortedMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
return (HashMap<Integer, Integer>) sortedMap;
}
static final String[] words = {"aaa", "bbb", "ccc", "booking", "alpha", "beta", "gamma"};
private static Map<String,Integer> wordList = new HashMap<>();
public static void main(String[] args) {
// TODO Auto-generated method stub
String w = "booking";
int k = 2;
//creating dictionary with word position
for(int i = 0;i<words.length;i++) {
wordList.put(words[i], i);
}
grep(w,k);
}
static void grep(String w,int k) {
int location = wordList.get(w) == null ? -1 : wordList.get(w);
if(location == -1 || k > location)
return;
int start = location - k;
for(int i = start;i<=location;i++) {
System.out.print(words[i]+" ");
}
}
We can solve it in the simplest way
static final String[] words = {"aaa", "bbb", "ccc", "booking", "alpha", "beta", "gamma"};
private static Map<String,Integer> wordList = new HashMap<>();
public static void main(String[] args) {
// TODO Auto-generated method stub
String w = "booking";
int k = 2;
//creating dictionary with word position
for(int i = 0;i<words.length;i++) {
wordList.put(words[i], i);
}
grep(w,k);
}
static void grep(String w,int k) {
int location = wordList.get(w) == null ? -1 : wordList.get(w);
if(location == -1 || k > location)
return;
int start = location - k;
for(int i = start;i<=location;i++) {
System.out.print(words[i]+" ");
}
}
- Tomas March 21, 2021