Skip to content

Instantly share code, notes, and snippets.

@isabolic
Last active January 3, 2018 08:04
Show Gist options
  • Select an option

  • Save isabolic/ca73fb77e09a313d4f1a2c27577e5863 to your computer and use it in GitHub Desktop.

Select an option

Save isabolic/ca73fb77e09a313d4f1a2c27577e5863 to your computer and use it in GitHub Desktop.
G964.ts
/*
Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.
#Example 1: a1 = ["arp", "live", "strong"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns ["arp", "live", "strong"]
#Example 2: a1 = ["tarp", "mice", "bull"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns []
Notes:
Arrays are written in "general" notation. See "Your Test Cases" for examples in your language.
In Shell bash a1 and a2 are strings. The return is a string where words are separated by commas.
Beware: r must be without duplicates.
*/
export class G964 {
public static inArray(a1: string[], a2: string[]): string[] {
let ret: string[] = [];
a1.forEach((str1: string) => {
a2.filter((str2:string) => {
if (str2.indexOf(str1) > -1 && ret.indexOf(str1) === -1){
ret.push(str1);
}
})
});
ret.sort();
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment