Longest Common Prefix in Javascript - javascript

I am trying to solve the Leet Code challenge 14. Longest Common Prefix:
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lower-case English letters.
My solution:
let strs = ["flower", "flow", "flight"];
var longestCommonPrefix = function (strs) {
for (let i = 0; i < strs.length; i++) {
for (let j = 0; j < strs[i].length; j++) {
// console.log(strs[i+2][j]);
if (strs[i][j] == strs[i + 1][j] && strs[i + 1][j] ==strs[i + 2][j]) {
return (strs[i][j]);
} else {
return "0";
}
}
}
};
console.log(longestCommonPrefix(strs));
Output: f
How can I iterate over every character and check if it is same and then go for next and if it fails then the longest common prefix will be returned?

As the longest common prefix must occur in every string of the array you can jus iterate over the length and check if all words have the same char at that index until you find a difference
function prefix(words){
// check border cases size 1 array and empty first word)
if (!words[0] || words.length == 1) return words[0] || "";
let i = 0;
// while all words have the same character at position i, increment i
while(words[0][i] && words.every(w => w[i] === words[0][i]))
i++;
// prefix is the substring from the beginning to the last successfully checked i
return words[0].substr(0, i);
}
console.log(1, prefix([]));
console.log(2, prefix([""]));
console.log(3, prefix(["abc"]));
console.log(4, prefix(["abcdefgh", "abcde", "abe"]));
console.log(5, prefix(["abc", "abc", "abc"]));
console.log(6, prefix(["abc", "abcde", "xyz"]));

Some of the issues:
Your inner loop will encounter a return on its first iteration. This means your loops will never repeat, and the return value will always be one character.
It is wrong to address strs[i+1] and strs[i+2] in your loop, as those indexes will go out of bounds (>= strs.length)
Instead of performing character by character comparison, you could use substring (prefix) comparison (in one operation): this may seem a waste, but as such comparison happens "below" JavaScript code, it is very fast (and as string size limit is 200 characters, this is fine).
The algorithm could start by selecting an existing string as prefix and then shorten it every time there is a string in the input that doesn't have it as prefix. At the end you will be left with the common prefix.
It is good to start with the shortest string as the initial prefix candidate, as the common prefix can certainly not be longer than that.
var longestCommonPrefix = function(strs) {
let prefix = strs.reduce((acc, str) => str.length < acc.length ? str : acc);
for (let str of strs) {
while (str.slice(0, prefix.length) != prefix) {
prefix = prefix.slice(0, -1);
}
}
return prefix;
};
let res = longestCommonPrefix(["flower","flow","flight"]);
console.log(res);

An approach based on sorting by word length, and for the shortest word, for exiting early, an entirely Array.every-based prefix-validation and -aggregation ...
function longestCommonPrefix(arr) {
const charList = [];
const [shortestWord, ...wordList] =
// sort shallow copy by item `length` first.
[...arr].sort((a, b) => a.length - b.length);
shortestWord
.split('')
.every((char, idx) => {
const isValidChar = wordList.every(word =>
word.charAt(idx) === char
);
if (isValidChar) {
charList.push(char);
}
return isValidChar;
});
return charList.join('');
}
console.log(
longestCommonPrefix(["flower","flow","flight"])
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

not the best solution but this should work
function longestPrefix(strs){
if(strs.length <1){
return "";
}
const sharedPrefix=function(str1,str2){
let i=0;
for(;i<Math.min(str1.length,str2.length) /*todo optimize*/;++i){
if(str1[i] !== str2[i]){
break;
}
}
return str1.substr(0,i);
};
let curr = strs[0];
for(let i=1;i<strs.length;++i){
curr=sharedPrefix(curr,strs[i]);
if(curr.length < 1){
// no shared prefix
return "";
}
}
return curr;
}

this:
strs[i][j] == strs[i + 1][j] ==strs[i + 2][j]
makes no sense in JS... or at least, makes no sense in what you are doing... to do this you should use a && operator, like this:
strs[i][j] == strs[i + 1][j] && strs[i + 1][j] ==strs[i + 2][j]
Otherwise JS will evaluate the first condition, and then will evaluate the result of that operation (either true or false) with the third value
In addition to this, consider that you are looping with i over the array, and so i will also be str.length - 1 but in the condition you are referencing strs[i + 2][j] that will be in that case strs[str.length + 1][j] that in your case, makes no sense.
About the solution:
You should consider that the prefix is common to all the values in the array, so you can take in consideration one value, and just check if all the other are equals... the most obvious is the first one, and you will end up with something like this:
let strs = ["flower", "flow", "flight", "dix"];
function longestCommonPrefix (strs) {
// loop over the characters of the first element
for (let j = 0; j < strs[0].length; j++) {
// ignore the first elements since is obvious that is equal to itself
for (let i = 1; i < strs.length; i++) {
/* in case you have like
[
'banana',
'bana'
]
the longest prefix is the second element
*/
if(j >= strs[i].length){
return strs[i]
}
// different i-th element
if(strs[0][j] != strs[i][j]){
return strs[0].substr(0, j)
}
}
}
// all good, then the first element is common to all the other elements
return strs[0]
};
console.log(longestCommonPrefix(strs));

you can do it like this, it works fast enough ~ 110ms
function longestCommonPrefix(strs){
if (strs.length === 0) {
return ''
}
const first = strs[0];
let response = '';
let prefix = '';
for (let i = 0; i < first.length; i++) {
prefix += first[i];
let find = strs.filter(s => s.startsWith(prefix));
if (find.length === strs.length) {
response = prefix;
}
}
return response;
};

let strs = ["flower", "flow", "flight"];
var longestCommonPrefix = function (strs) {
for (let i = 0; i < strs.length; i++) {
for (let j = 0; j < strs[i].length; j++) {
console.log(strs[i+2][j]);
if (strs[i][j] == strs[i + 1][j] && strs[i][j] ==strs[i + 2][j]) {
return (strs[i][j]);
} else {
return "0";
}
}
}
};
console.log(longestCommonPrefix(strs));
This **return ** f

Increase the index while the letter is the same at that index for all words in the list. Then slice on it.
function prefix(words) {
if (words.length === 0) { return '' }
let index = 0;
while (allSameAtIndex(words, index)) {
index++;
}
return words[0].slice(0, index);
}
function allSameAtIndex(words, index) {
let last;
for (const word of words) {
if (last !== undefined && word[index] !== last[index]) {
return false;
}
last = word;
}
return true;
}

I assume you are here for Leetcode problem solution.
var longestCommonPrefix = function(strs) {
let arr = strs.concat().sort();
const a1 = arr[0];
const a2 = arr[arr.length -1];
const length = a1.length;
let i=0;
while(i<length && a1.charAt(i) == a2.charAt(i)) i++;
return a1.substring(0,i);
};

function prefixLen(s1, s2) {
let i = 0;
while (i <= s1.length && s1[i] === s2[i]) i++;
return i;
}
function commonPrefix(arr) {
let k = prefixLen(arr[0], arr[1]);
for (let i = 2; i < arr.length; i++) {
k = Math.min(k, prefixLen(arr[0], arr[i]));
}
return arr[0].slice(0, k);
}
console.log(commonPrefix(['pirate', 'pizza', 'pilates'])) // -> "pi"

var longestCommonPrefix = function(strs) {
let prefix = "";
for(let i = 0; i < strs[0].length; i++) {
for(let j = 1; j < strs.length; j++) {
if(strs[j][i] !== strs[0][i]) return prefix;
}
prefix = prefix + strs[0][i];
}
return prefix;
};
console.log(longestCommonPrefix);

It is as simple as one loop and compare each element of the strings
const longestPrefix = (strs) => {
[word1, word2, word3] = strs;
let prefix = [];
if(strs === null || strs.length <= 2 || strs.length > 3) return 'please
insert 3 elements'
for (let i=0; i < word1.length; i++){
if(word1[i] === word2[i] && word1[i] === word3[i]){
prefix.push(word1[i])
}
}
return prefix.join('')
}

I read in another answer: 'Increase the index while the letter is the same at that index for all words in the list. Then slice on it.'
that's how I came up with this:
const findPrefix = (strs) => {
let i = 0;
while (strs.every((item) => strs[0][i] === item[i])) {
i++;
}
return strs[0].slice(0, i);
};
console.log(findPrefix(["flo", "flow", "flomingo"]));

const findPrefix = (strs) => {
let broke = false;
return strs[0].split("").reduce(
(acc, curr, index) =>
broke || !strs.every((word) => word[index] === curr)
? (broke = true && acc)
: (acc += curr),
""
);
};
console.log(findPrefix(["flower", "flow", "flamingo"]));

Here is my solution, Today I had an interview and the dev asked me the same question, I think I failed because I got stuck hahaha kinda nervous when someone is watching me 😂, anyway I decided to figure it out after the interview is done and this is my answer (without google it I swear) and for those who don't feel comfortable with the common "for loop"
const arr = ["absence", "absolute", "absolutely", "absorb"]
function getPrefix(arr) {
if (arr.length === 0 || arr.some(s => !s)) return null //if it's an empty array or one of its values is null or '', return null
const first = arr[0].split("") // turns the first position of the array, into an array
const res = arr.map(w => {
// mapping the original array
const compare = w.split("") // every item of the array will be converted in another array of its characters
return first.map((l, idx) => (compare[idx] === l ? l : "")).join("") // loop through the "first" array and compare each character
})
const prefix = first.join("").startsWith(res[res.length - 1]) // compare if the first letter starts with the last item of the returned array
? res[res.length - 1] // if true, return the final response which is the prefix
: null // else, return null, which means there is no common prefix
console.log("prefix: ", prefix)
return prefix
}
getPrefix(arr)

let arr = ["flower", "flow", "flight"]
function checkPrefix(array) {
let index = []
for (let i = 0; i <= array[0].length; i++) {
if (check(array[0][i], i, array)) {
index.push(i)
} else {
break;
}
}
console.log(array[0].substring(index[0], index[index.length - 1] + 1));
}
const check = (str, index, stringArr) => {
debugger
let status = true
stringArr.map(ele => {
debugger
if (ele[index] != str) {
status = false
}
})
return status
}
checkPrefix(arr)

/**
* #param {string[]} strs
* #return {string}
*/
var longestCommonPrefix = function(strs) {
let compare = strs[0];
let str = "";
for (let i = 1; i < strs.length; i++) {
let j = 0;
while(compare[j] != undefined && strs[i][j] != undefined) {
if(strs[i][j] == compare[j]) {
str += strs[i][j];
}
else break;
j++;
}
compare = str;
str = "";
}
return compare;
};

Longest common prefix in Javascript (All test case accepted. Asked by many company interviews.)
var longestCommonPrefix = function (strs) {
let string = '';
if (strs.length > 1) {
for (let i = 0; i < strs[0].length; i++) {
let str = strs[0].charAt(i);
for (let s = 0; s < strs.length - 1; s++) {
if (!(strs[s + 1].charAt(i) && strs[s].charAt(i) && strs[s + 1].charAt(i) == strs[s].charAt(i))) {
str = '';
}
}
if (!str) {
break;
}
string += str;
}
return string;
} else {
return strs[0];
}
};
longestCommonPrefix(["flower","flow","flight"]);

Code to find longest prefix
var longestCommonPrefix = function(strs) {
let match = false;
let len = strs[0].length ;
let ans = "";
let prev_ans ="";
if(strs.length ==1){
return strs[0];
}
for (let i = 1; i < strs.length; i++){
if( strs[i-1].length > strs[i].length){
len = strs[i].length;
}
}
for (let i = 1; i < strs.length; i++){
for (let j = 0; j < len; j++){
if(strs[i-1].charAt(j) == strs[i].charAt(j)){
ans += strs[i-1].charAt(j);
match = true;
}
else{
break;
}
}
if(prev_ans != "" && prev_ans !=ans){
if(prev_ans.length > ans.length){
return ans;
}else{
return prev_ans;
}
}
prev_ans = ans;
ans = "";
if (match == false){
return "";
}
}
return prev_ans;
};
console.log(longestCommonPrefix(["flow","fly","flu"]));

My solution:
function longestCommonPrefix(...words) {
words.sort(); // shortest string will be first and the longest last
return (
words[0].split('') // converts shortest word to an array of chars
.map((char, idx) => words[words.length - 1][idx] === char ? char : '\0') // replaces non-matching chars with NULL char
.join('') // converts back to a string
.split('\0') // splits the string by NULL characters
.at(0) // returns the first part
);
}
Usage example:
longestCommonPrefix('abca', 'abda', 'abea'); // 'ab'

let testcases = [
["flower", "flow"], //should return "flow"
["flower", "flow", "flight"], //should return "fl"
["flower", "flow", "fight"], //should return "f"
["flower", "flow", "floor"], //should return "flo"
["flower"], //should return "flower"
]
var longestCommonPrefix = function(strs) {
for(var i=0; i<strs.length; i++){
for(var j=0; j<strs[i].length; j++){
if(strs[i][j] + strs[i][j+1] === strs[i+1][j]+strs[i+1][j+1]){
return strs[i][j]+strs[i][j+1];
}
else {
return "";
}
}
}
};
for (let strs of testcases)
console.log(longestCommonPrefix(strs));

Related

How to get all maximum frequency characters in string?

I have a string and I want to retrieve all the characters with maximum occurring frequency. I have created a function that returns a single maximum frequency character in the string. But how to modify it in such a way that it returns all the characters with maximum occurring frequency in form or array or string.
const str = "helloo"
function findMaxChar(str) {
let obj = {}
let maxVal = 1
let val
for (let i = 0; i < str.length; i++) {
if (str[i] !== ' ') {
if (obj.hasOwnProperty(str[i])) {
obj[str[i]] = obj[str[i]] + 1
}
else {
obj[str[i]] = 1
}
}}
for (const item in obj) {
if (obj[item] > maxVal) {
maxVal = obj[item]
val = item
}
}
return val
}
Desired output = [l, o] // since both l and o has maximum occuring frequency
Maybe create a loop that finds the highest number of occurrences in a string, and then in the next loop check what characters appear that many times.
const str = "helloo"
function findMaxChar(str) {
let obj = {}
let maxVal = 0
let valArr = [];
for (let i = 0; i < str.length; i++) {
if (str[i] !== ' ') {
if (obj.hasOwnProperty(str[i])) {
obj[str[i]] = obj[str[i]] + 1
}
else {
obj[str[i]] = 1
}
}}
for (const item in obj) {
if (obj[item] >= maxVal) {
maxVal = obj[item];
}
}
for (const item in obj) {
if (obj[item] === maxVal) {
valArr.push(item)
}
}
return valArr.length > 1 ? valArr : valArr.join();
}
console.log(findMaxChar(str))
Sounds pretty straight forward, find the maximum frequency and then all chars that occurred with this frequency:
const maxFrequence = Math.max(...Object.values(obj))
const elements = Object.keys(obj).filter( char => obj[char] === maxFrequency)

triangle Number pattern using loop in javasctipt

Hello I'm having a problem.
I want to create a triangular number pattern as follows:
Output:
1223334444333221
=22333444433322=
===3334444333===
======4444======
I've tried to make the program. But the logic I use is not quite right.
function nomor4(level) {
let len = null;
let result = [];
while (level > 0) {
let arr = [];
for (let i = 1; i < level; i++) {
for (let repeat = 0; repeat <i; repeat++){
arr.push(i)
}
}
// convert arr.push value from array to string using join
//and add 1 and the copy value using reverse
let str_level = arr.join("") + "4444" + arr.reverse().join("");
if (len == null) {
len = str_level.length;
}
//Add Strip
while (str_level.length < len) {
str_level = "-" + str_level + "-";
}
result.push(str_level);
level--;
}
return result.join("\n");
}
console.log(nomor4(4))
if anyone can. please help me give the solution. Thank you
Here's a way of doing this by using two nested maps over arrays of rows (a counter for each row) and columns (the values to print in each row)
const size = 4
const fill = '='
const rows = Array.from({length: size}, (_, i) => i + 1) // 1,2,3,4
const cols = rows.concat(rows.slice(0, -1).reverse()) // 1,2,3,4,3,2,1
const result = rows
.map(r => cols
.map(c => ((c >= r) ? c : fill).toString().repeat(c)
).join('')
).join('\n')
console.log(result)
There are better ways than this, but I will put this here just to show how to do it with minimal changes from the OP (I just changed - to = and the for loop with i).
function nomor4(level) {
let len = null;
let result = [];
while (level > 0) {
let arr = [];
for (let i = 5-level; i < 4; i++) {
for (let repeat = 0; repeat <i; repeat++){
arr.push(i)
}
}
// convert arr.push value from array to string using join
//and add 1 and the copy value using reverse
let str_level = arr.join("") + "4444" + arr.reverse().join("");
if (len == null) {
len = str_level.length;
}
//Add Strip
while (str_level.length < len) {
str_level = "=" + str_level + "=";
}
result.push(str_level);
level--;
}
return result.join("\n");
}
console.log(nomor4(4))
function nomor4(level) {
let realValue = level;
let len = null;
let result = [];
while (level >= 0) {
let arr = [];
for (let i = 1; i <= realValue; i++) {
if (realValue !== i) {
for (let repeat = 0; repeat < i; repeat++) {
if (realValue - level <= i) {
arr.push(i);
}
}
}
}
// convert arr.push value from array to string using join
// and add 1 and the copy value using reverse
let str_level = arr.join("") + "4444" + arr.reverse().join("");
if (len == null) {
len = str_level.length;
}
//Add Strip
while (str_level.length < len) {
str_level = "-" + str_level + "-";
}
result.push(str_level);
result = [...new Set(result)];
level--;
}
return result.join("\n");
}
console.log(nomor4(4));

JavaScript How to Create a Function that returns a string with number of times a characters shows up in a string

I am trying to figure out how to make a function that takes a string. Then it needs to return a string with each letter that appears in the function along with the number of times it appears in the string. For instance "eggs" should return e1g2s1.
function charRepString(word) {
var array = [];
var strCount = '';
var countArr = [];
// Need an Array with all the characters that appear in the String
for (var i = 0; i < word.length; i++) {
if (array.indexOf(word[i]) === false) {
array.push(word[i]);
}
}
// Need to iterate through the word and compare it with each char in the Array with characters and save the count of each char.
for (var j = 0; j < word.length; i++) {
for (var k = 0; k < array.length; k++){
var count = 0;
if (word[i] === array[k]){
count++;
}
countArr.push(count);
}
// Then I need to put the arrays into a string with each character before the number of times its repeated.
return strCount;
}
console.log(charRepString("taco")); //t1a1co1
console.log(charRepString("egg")); //e1g2
let str = prompt('type a string ') || 'taco'
function getcount(str) {
str = str.split('')
let obj = {}
for (i in str) {
let char = str[i]
let keys = Object.getOwnPropertyNames(obj)
if (keys.includes(char)) {
obj[char] += 1
} else {
obj[char] = 1
}
}
let result = ''
Object.getOwnPropertyNames(obj).forEach((prop) => {
result += prop + obj[prop]
})
return result
}
console.log(getcount(str))
If the order of the alphanumeric symbols matters
const str = "10zza";
const counted = [...[...str].reduce((m, s) => (
m.set(s, (m.get(s) || 0) + 1), m
), new Map())].flat().join("");
console.log(counted); // "1101z2a1"
Or also like (as suggested by Bravo):
const str = "10zza";
const counted = [...new Set([...str])].map((s) =>
`${s}${str.split(s).length-1}`
).join("");
console.log(counted); // "1101z2a1"
A more clear and verbose solution-
Let m be max number of symbols in charset
Time complexity- O(n log(m))
Space complexity- O(m)
function countFrequencies(str) {
const freqs = new Map()
for (const char of str) {
const prevFreq = freqs.get(char) || 0
freqs.set(char, prevFreq + 1)
}
return freqs
}
function getCountStr(str) {
const freqs = countFrequencies(str)
const isListed = new Set()
const resultArray = []
for (const char of str) {
if (isListed.has(char)) continue
resultArray.push(char)
resultArray.push(freqs.get(char))
isListed.add(char)
}
return resultArray.join("")
}
console.log(getCountStr("egg"))
console.log(getCountStr("taco"))
console.log(getCountStr("10za"))
Using Set constructor, first we will get the unique data.
function myfun(str){
let createSet = new Set(str);
let newArr = [...createSet].map(function(elem){
return `${elem}${str.split(elem).length-1}`
});
let newStr = newArr.join('');
console.log(newStr);
}
myfun('array');

Write function strLetterCount and return new string with the characters followed by the count

I need to write a function strLetterCount(word) that takes the string input, and returns a string followed by the count of occurrences.
Ex:
strLetterCount("coconut"); // "c2o2n1u1t1"
Here is what I have so far:
function strLetterCount (word){
let results = ""
for(let i = 0; i < word.length; i++) {
let charAt = word.charAt(i)
let count = 0
results += charAt
for (let j = 0; j < word.length ; j++)
{
if(word.charAt(j) === charAt)
{
count++
}
}
results += count
}
return results;
}
But the issue is that it returns c2o2c2o2n1u1t1 instead of c2o2n1u1t1. I'm not sure how to get rid of the duplicates
function strLetterCount (word){
let results = ""
for(let i = 0; i < word.length; i++) {
let charAt = word.charAt(i)
if (!results.includes(charAt)) // <----------------------
{
let count = 0
results += charAt
for (let j = 0; j < word.length ; j++)
{
if(word.charAt(j) === charAt)
{
count++
}
}
results += count
}
}
return results;
}
console.log(strLetterCount("coconut"));
Using String.prototype.split function, you can convert string into char array.
Based on that char array, using Array.prototype.reduce, you can get the duplicated char info.
With that duplicated info, can get the string linked using Array.prototype.map.
console.log(strLetterCount("coconut")); // "c2o2n1u1t1"
function strLetterCount(word) {
const wordArr = word.split('');
const duplicates = wordArr.reduce((acc, cur) => {
acc[cur] ? acc[cur] ++ : acc[cur] = 1;
return acc;
}, {});
return Object.entries(duplicates).map(([key, value]) => key + value).join('');
}
function strLetterCount(word) {
let results = {};
for (let i = 0; i < word.length; i++) {
let c = word.charAt(i)
results[c] = (results[c] || 0) + 1;
}
let r = '';
for (let c in results)
r += c + results[c];
return r;
}
console.log(strLetterCount("coconut")); // "c2o2n1u1t1"
Using the String split method, a Set, the Array map method and the Array join method you can do it in a single line of code
const strLetterCount = word => [...new Set(word.split(''))].map(l => `${l}${word.split(l).length - 1}`).join('');
console.log(strLetterCount('coconut'));

Test word against array for anagrams - Javascript

So far.. I have this:
function anagrams(word, words) {
for(let i = 0; i <= words.length; i++){
const aCharMap = buildCharMap(word);
const bCharMap = buildCharMap(words[i]);
if(Object.keys(aCharMap).length !== Object.keys(bCharMap).length) {
words.pop(words[i])
}
for (let char in aCharMap) {
if (aCharMap[char] !== bCharMap[char]) {
words.pop(words[i]);
}
}
console.log(word);
console.log(words);
}
}
function buildCharMap(str) {
const charMap = {};
for (let char of str.replace(/[^\w]/g, '').toLowerCase()) {
charMap[char] = charMap[char] + 1 || 1;
}
return charMap;
}
The question at hand is obvious if you read through the code but here it is
Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none. For example:
anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) => ['aabb', 'bbaa']
anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']) => ['carer', 'racer']
anagrams('laser', ['lazing', 'lazy', 'lacer']) => []
Is pop used the way you use it?
Alternatively you can do like this:
function anagrams(word, words) {
let result = [];
const aCharMap = buildCharMap(word);
// you also had a off-by-one error in loop
for (let i = 0; i < words.length; i++) {
const bCharMap = buildCharMap(words[i]);
let isAnagram = (a, b) => {
if (Object.keys(a).length !== Object.keys(b).length) return false;
for (let char in a) {
if (aCharMap[char] !== b[char]) {
return false
}
}
return true;
}
if (isAnagram(aCharMap, bCharMap)) result.push(words[i])
}
function buildCharMap(str) {
const charMap = {};
for (let char of str.replace(/[^\w]/g, '').toLowerCase()) {
charMap[char] = charMap[char] + 1 || 1;
}
return charMap;
}
return result
}
console.log(anagrams("test", ["estt", "ttee", "tset"]))

Categories