Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,276 questions

52,302 answers

573 users

How to group words in a string by the first N letters in TypeScript

2 Answers

0 votes
function groupByFirstNLetters(s: string, n: number = 3): Record<string, string[]> {
  const words = s.toLowerCase().match(/[a-zA-Z]+/g) ?? [];
  const groups: Record<string, string[]> = {};

  for (const w of words) {
    if (w.length >= n) {
      const prefix = w.slice(0, n);
      if (!groups[prefix]) groups[prefix] = [];
      groups[prefix].push(w);
    }
  }

  return groups;
}

const s =
  "The lowly inhabitants of the lowland were surprised to see the lower branches of the trees.";

const groups = groupByFirstNLetters(s, 3);

// Print version 1
for (const [prefix, words] of Object.entries(groups)) {
  console.log(prefix, ":", words);
}

console.log();

// Print version 2
for (const [prefix, words] of Object.entries(groups)) {
  console.log(`${prefix}: ${words.join(", ")}`);
}





/*
run:

"the",  ":",  ["the", "the", "the", "the"] 
"low",  ":",  ["lowly", "lowland", "lower"] 
"inh",  ":",  ["inhabitants"] 
"wer",  ":",  ["were"] 
"sur",  ":",  ["surprised"] 
"see",  ":",  ["see"] 
"bra",  ":",  ["branches"] 
"tre",  ":",  ["trees"] 

"the: the, the, the, the" 
"low: lowly, lowland, lower" 
"inh: inhabitants" 
"wer: were" 
"sur: surprised" 
"see: see" 
"bra: branches" 
"tre: trees" 

*/

 



answered 1 day ago by avibootz
0 votes
function groupByFirstNLetters(s: string, n: number = 3): Record<string, string[]> {
  return (s.toLowerCase().match(/[a-zA-Z]+/g) ?? [])
    .filter(w => w.length >= n)
    .reduce<Record<string, string[]>>((groups, w) => {
      const prefix = w.slice(0, n);
      (groups[prefix] ??= []).push(w);
      return groups;
    }, {});
}

const s =
  "The lowly inhabitants of the lowland were surprised to see the lower branches of the trees.";

const groups = groupByFirstNLetters(s, 3);

// Print version 1
for (const [prefix, words] of Object.entries(groups)) {
  console.log(prefix, ":", words);
}

console.log();

// Print version 2
for (const [prefix, words] of Object.entries(groups)) {
  console.log(`${prefix}: ${words.join(", ")}`);
}




/*
run:

"the",  ":",  ["the", "the", "the", "the"] 
"low",  ":",  ["lowly", "lowland", "lower"] 
"inh",  ":",  ["inhabitants"] 
"wer",  ":",  ["were"] 
"sur",  ":",  ["surprised"] 
"see",  ":",  ["see"] 
"bra",  ":",  ["branches"] 
"tre",  ":",  ["trees"] 

"the: the, the, the, the" 
"low: lowly, lowland, lower" 
"inh: inhabitants" 
"wer: were" 
"sur: surprised" 
"see: see" 
"bra: branches" 
"tre: trees" 

*/

 



answered 1 day ago by avibootz
...