Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are a given string in a single line. Assume “words” are all sequences of Latin letters.
- For example in the input string "java..?|basics/*-+=javabasics" we have 3 words: "java", "basics" and "javabasics".
- Write a program to find in the input string all unique sets of 3 “words” {a, b, c}, such that a|b = c.
- Assume that "a|b" means to concatenate the “word” b after a. We call these “words” {a, b, c} cognate words.
- All “words” must be case sensitive! Don't repeat the cognate words in the output.
- Print at the console all cognate words {a, b, c} found in the input sequence in format "a|b=c" (without any spaces),
- each at a separate line. The order of the output lines is not important. Print "No" in case no cognate words exist
- among the input sequence of characters. */
- "use strict";
- function findUniqueSetsOf3Words(args) {
- args = args[0].split(/[^a-z]+/i).filter(Boolean);
- var cognateWords = [];
- if (args && args.length > 2) {
- var i, j, k;
- for (i = 0; i < args.length; i += 1) {
- for (j = 0; j < args.length; j += 1) {
- for (k = 0; k < args.length; k += 1) {
- if (j != i && k != j && k != i) {
- if (args[i].concat(args[j]) === args[k] && cognateWords.indexOf(args[k]) < 0) {
- console.log("%s|%s=%s", args[i], args[j], args[k]);
- cognateWords.push(args[k]);
- }
- }
- }
- }
- }
- }
- if (!cognateWords.length) {
- console.log('No');
- }
- }
- findUniqueSetsOf3Words(["java..?|basics/*-+=javabasics"]);
- findUniqueSetsOf3Words(["Hi, Hi, Hihi"]);
- findUniqueSetsOf3Words(["Uni(lo,.ve=I love SoftUni (Soft)"]);
- findUniqueSetsOf3Words(["a a aa a"]);
- findUniqueSetsOf3Words(["x a ab b aba a hello+java a b aaaaa"]);
- findUniqueSetsOf3Words(["aa bb bbaa"]);
- findUniqueSetsOf3Words(["ho hoho"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement