-1

Condition.

There are uppercase alaphabet items

items = [ "FUCAMVLNDI", "KXRMGDPOHU", "GPMKEUFLQJ", 
"YHBURQPOFM", "VKURNBXTIH", "IGLOTVNPEY", "SLCGRJOPYB",
"HUDGIFNTPM", "EPHLCQZFOR", "TYDIAVUQZW", "KTYBZPXSHG",
"CWXMLTFDJI", "ERCJSWPFYH", "TQSHIKRCMJ", "JHBOYCFWAK",
"NPVAMTLXBQ", "BIWAUNPSOT", "TQZARXMSLH", "JLYDFHWMTN",
"XODHNVFCWM", "EWZFKXRIVM", "KVEWZRXIMA"
  1. There are given string which is 10 length, uppercase alphabet, like BIWAUNPSOT each called item above
  2. Each Alphabet in item is a meta data ( or property ), not just a alphabet.
  3. So i want to find a item in items using regex which presents condition of the most matching a given meta data of each item
    • Intersection of two string
    • If YHBURQPOFM matches IGLOTVNPEY. Y, O, P Three conditions are satisfied
  4. In order to know the most satisfied condition, I thought i need to use { } operator because it's countable so that i can find the most matching for each item with some logic. but it's hard to make regex to me. something wrong.
  5. I would find item of items using match method like
    • items.toString().match(item_regex);
    • My problem is item_regex
    • But is there another good way to find the most matching item? not using regex?

What i did

for ( let i = 0; i < items.length; i ++){
  // make RegExp using each item
  const regex = new RegExp('\\b[' + items[i] + ']{' + increase + '}\\w+', 'g');
  const matchedList = items.toString().match(regex)
  if ( matchedList === null ) {
     // the previous one was the most matched condition
     // So I thought that ( increase - 1 ) is the most matched condition 
  } else {
     increase++;
     i=0;
  }
}

The example of regex what i thought is

\b[item]{increase}\w+

It might be something like

\b[YHBURQPOFM]{7}\w+

But never mind. I think this is totally wrong

So My Question is

  1. What is the best regex which meet the condition i mentioned

    • I need 2~n items with the most matching ,
    • I need the number for the most matching ( How many match each item, it can be 1~10 because, the given string is 10 length )
    • with the code above, I can get 2~n items through matchedList

      I can get number for the most matching through increase, { } operator

    • I'm trying to match each alphabet in any other. ex) ABCDEFGHIJ and AXBCDEFGHI is the match = 9 but also JIHGFEDCBA and AXBCDEFGHI is the match = 9
  2. Was it wrong approach? i mean, Using regex is a bad idea for this solution?

Edit

The most matching means

Intersection with two string. For example

If YHBURQPOFM matches IGLOTVNPEY. Y, O, P, Three conditions are satisfied

So I would search every item for the whole items so that i can find the most matching

they can be 3 couples with satisfying 7 conditions, they can be 2 couples with satisfying 8 conditions

If 0 couples with satisfying 9 conditions, then 8 is the most matching

RayKim
  • 423
  • 1
  • 6
  • 22
  • I still do not understand your definition of "most matching". Please explain with examples. – PM 77-1 Nov 29 '19 at 01:56
  • are you trying to match each alphabet in the order, ex: ABCDEFGHIJ and AXBCDEFGHI is the match = 9 or 1? – wakakak Nov 29 '19 at 01:58
  • @PM77-1 I edited my question. Sorry for bad English. But i think you can understand it – RayKim Nov 29 '19 at 02:15
  • @GlenK that is the match = 9 but, JIHGFEDCBA and AXBCDEFGHI is also the match = 9 – RayKim Nov 29 '19 at 02:23
  • You need intersection between two strings? – PM 77-1 Nov 29 '19 at 02:24
  • See https://stackoverflow.com/questions/4448370/intersection-of-two-strings-in-java – PM 77-1 Nov 29 '19 at 02:28
  • @PM77-1 Right, I appreciate your answer. but not enough for my problem. for short. I need ```regex``` which need the match count and match alphabet specified in any order – RayKim Nov 29 '19 at 03:15
  • Your task is ill suited for RegEx and under normal business circumstances should be solved by other means. – PM 77-1 Nov 29 '19 at 14:38
  • If your [other question with the same topic/problem](https://stackoverflow.com/questions/59084480/a-question-about-this-regexp-balpahbetnumber-w) is no longer of interest than delete it. – Andreas Nov 29 '19 at 15:25

1 Answers1

1

Create 2 functions:

  1. A function that takes 2 strings and returns the number of intersections between them (if the strings are identical return is null)

  2. A function that takes an array of strings and on each string returns an array of sub arrays ([string A, string B, number of intersections between both strings])

Details commented in demo

const data = ["FUCAMVLNDI", "KXRMGDPOHU", "GPMKEUFLQJ",
  "YHBURQPOFM", "VKURNBXTIH", "IGLOTVNPEY", "SLCGRJOPYB",
  "HUDGIFNTPM", "EPHLCQZFOR", "TYDIAVUQZW", "KTYBZPXSHG",
  "CWXMLTFDJI", "ERCJSWPFYH", "TQSHIKRCMJ", "JHBOYCFWAK",
  "NPVAMTLXBQ", "BIWAUNPSOT", "TQZARXMSLH", "JLYDFHWMTN",
  "XODHNVFCWM", "EWZFKXRIVM", "KVEWZRXIMA"
];

/* 
 ** Pass 2 strings returns the total number of intersections
 */
const intCount = (stringA, stringB) => {
  // If strings are identical return null
  if (stringA === stringB) {
    return null;
  }
  // Convert both strings into an array of letters
  const A = stringA.split('');
  const B = stringB.split('');
  /* Array C is an array of letters for each time there's a matched letter
  between array A and array B */
  let C = A.filter(letter => B.includes(letter));
  // Return the total number of intersections
  return C.length;
}

/*
 ** Pass an array of strings returns an array:
 ** [ stringA, stringB, greatest number of intersections]
 */
const intSets = array => {
  // flatMap creates a 2 dimensional map and flattens it
  let map = array.flatMap((word, index, group) => {
    // Declare a word by index as a fixed reference
    const fixed = group[index];
    // Declare an empty array
    let set = [];
    // Iterate thru the array of words...
    for (let word of group) {
      // Declare an empty array...
      let pairs = [];
      // Add the fixed word to pairs array
      pairs.push(fixed);
      // Add the current word to pairs array
      pairs.push(word);
      // Add the return value of intCount() of both words
      pairs.push(intCount(fixed, word));
      // Add the pairs array to the set array
      set.push(pairs);
    }
    // Return set array of [string A, string B, intersection count] 
    return set;
    // console.log(set);
  });
  // Return the array in descending order of each pairs array number (index [2]).
  let ordered = map.sort((setA, setB) => setB[2] - setA[2]);
  // Return the pairs array with the greatest number of intersections
  return ordered[0];
}

console.log(intSets(data));
zer00ne
  • 41,936
  • 6
  • 41
  • 68