Last active
May 15, 2019 03:09
-
-
Save johntheo/594d847d6cfd66fc3c18b481987a6ea6 to your computer and use it in GitHub Desktop.
Luxoft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println("==== TASK 1 ===="); | |
Concatenation concatenation = new Concatenation(); | |
String word = "whataniceday"; | |
List<String> tokens = Arrays.asList("a", "what", "an", "nice", "day"); | |
System.out.println("WORD: '" + word + | |
"'\tTOKENS: " + tokens + "\tRESULT: " + | |
concatenation.validate(word, tokens)); | |
word = "dawhaty"; | |
tokens = Arrays.asList("a", "what", "an", "nice", "day"); | |
System.out.println("WORD: '" + word + | |
"'\tTOKENS: " + tokens + "\tRESULT: " + | |
concatenation.validate(word, tokens)); | |
word = "abc"; | |
tokens = Arrays.asList("a", "ab", "bc"); | |
System.out.println("WORD: '" + word + | |
"'\tTOKENS: " + tokens + "\tRESULT: " + | |
concatenation.validate(word, tokens)); | |
System.out.println("==== TASK 2 ===="); | |
Intervals merged = new Intervals(); | |
List<Interval> intervals = Arrays.asList( | |
new Interval(2,6), | |
new Interval(8,10), | |
new Interval(1,3), | |
new Interval(15,18), | |
new Interval(18,21)); | |
System.out.println("INTERVALS: \t"+ intervals); | |
System.out.println("MERGED: \t"+ merged.merge(intervals)); | |
intervals = Arrays.asList( | |
new Interval(2,6), | |
new Interval(8,10), | |
new Interval(1,3), | |
new Interval(15,18), | |
new Interval(18,21), | |
new Interval(5,15)); | |
System.out.println("INTERVALS: \t"+ intervals); | |
System.out.println("MERGED: \t"+ merged.merge(intervals)); | |
} | |
} | |
class Concatenation { | |
public boolean validate(String str, List<String> tokens) { | |
String possibleWords = String.join("|", tokens); | |
//Generate a regex string for string matching | |
String regex = "^(" + possibleWords + ")+$"; | |
return str.matches(regex); | |
} | |
} | |
class Interval{ | |
int start; | |
int end; | |
public Interval(int start, int end){ | |
this.start = start; | |
this.end = end; | |
} | |
@Override | |
public String toString() { | |
return "[" + start + ", " + end + ']'; | |
} | |
} | |
class Intervals { | |
public List<Interval> merge(List<Interval> intervals){ | |
Collections.sort(intervals, new IntervalComparator()); | |
LinkedList<Interval> result = new LinkedList<>(); | |
for (Interval interval : intervals) { | |
// if the list is empty or didn overlap the last one | |
if (result.isEmpty() || result.getLast().end < interval.start) { | |
result.add(interval); | |
} | |
else {//if overlap -> merge | |
result.getLast().end = Math.max(result.getLast().end, interval.end); | |
} | |
} | |
return result; | |
} | |
private class IntervalComparator implements Comparator<Interval> { | |
@Override | |
public int compare(Interval a, Interval b) { | |
return a.start < b.start ? -1 : a.start == b.start ? 0 : 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment