Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine groups in a string

I am trying to determine groups in a string.

For example "AAABBCCCCD" should have ["AAA", "BB", "CCCC", "D"] as groups based on the pattern in the string.

Here is my simple function to do that:

const patternGroup = (str) => {
  let cursor = str[0]
  let groups = []
  let currentGroup = ""
  for (let i = 0; i < str.length; i++) {
    let ch = str[i]
    if (ch === cursor) {
      currentGroup += ch
    }
    else {
      groups.push(currentGroup)
      currentGroup = ch
      cursor = ch
    }
  }
  if (currentGroup !== "") {
    groups.push(currentGroup)
  }
  return groups
}

It works as intented but I am looking for a simpler function, maybe using map/reduce or regex. Any ideas?

like image 587
Ben Dev Avatar asked Jul 27 '20 04:07

Ben Dev


People also ask

How do you group numbers in a string?

string[] strArr = strFood . GroupBy(y => Char. IsDigit(y)). Select(y => y.

How do I capture a group in regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".

What is regex match group?

Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.

What is capturing group in regex Javascript?

Groups group multiple patterns as a whole, and capturing groups provide extra submatch information when using a regular expression pattern to match against a string. Backreferences refer to a previously captured group in the same regular expression.

How do you know if a word is in a group?

Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group. We are given a list strs of strings where every string in strs is an anagram of every other string in strs.

What is a similar string group?

839. Similar String Groups Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. Also two strings X and Y are similar if they are equal.

How to get the group of the matched pattern in matcher?

The group (String string) method of Matcher Class is used to get the group of the match result already done, from the specified string. Parameters: This method takes a parameter string which is the String from which the group index of the matched pattern is required.

How to count the number of groups in the current match?

You can count the number of groups in the current match using the groupCount () method of the Matcher class. This method calculates the number of capturing groups in the current match and returns it.


Video Answer


2 Answers

you can simply use RegExp:

var input = "AAABBCCCCD";
const res = input.match(/([^])(\1*)/g);

console.log(res)
like image 117
PoryaGrand Avatar answered Oct 09 '22 09:10

PoryaGrand


Similar to your implementation:

const patternGroupWithReduce = (str) => {
  let currentGroup = ""
  return str.split("").reduce((acc, cur, i) => {
    currentGroup += cur
    if (str[i + 1] !== cur) {
      acc.push(currentGroup)
      currentGroup = ""
    }
    return acc
  }, [])
}

const patternGroupWithRegex = (str) => {
  return str.match(/(.)\1*/g)
}

RegEx /(.)\1/g* uses the back reference for the capturing group.

\1* matches the same char as recently captured one as many times as possible.

Here is the visualization for it: enter image description here

like image 34
omt66 Avatar answered Oct 09 '22 10:10

omt66