Algorithms - No Repeats Please. Update: now it's about approach, not task

Tell us what’s happening:
I am confused by this part of the task:

Assume that all characters in the provided string are each unique.

To me the above means, that str parameter should be:

'abc'
'abcde'

But not with duplicates of characters:

'aba'
'abcdea'

However, there are example and test cases:

'aab'
permAlone("aabb")
permAlone("aaa")

I don’t know, did I misunderstand instructions completely?

  **Your code so far**
function permAlone(str) {
return str;
}

permAlone('aab');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Algorithms - No Repeats Please

Link to the challenge:

I think the instructions mean to say “Treat all characters in the provided string as if they are each unique”.

1 Like

well, this way it’s more clear. Thanks.

I am trying to solve it with maths.

I guess I can find answer without actually building any permutations, just knowing number of repeating characters. I built object for in the code, I hope idea can be gathered from it’s structure.

I know that number of total permutations can be calculated with factorial.
But I am struugling to develop formula for this particular task, though I see some patterns.

I did some tests, from that it should be clear where I am in solving this right now.

Note: I know how to solve it by brute force: built array af all perms (I did that recursively in Python some time ago). Then filter it, checking every element for not having repeats. But that would be heavy stuff.

const factorial = (num) => {
  let mult = num - 1;
  while (mult > 0) {
    num *= mult;
    mult--;
  }
  return num;
}

function permAlone(str) {
  
  const len = str.length;
  let charOccurances = {};
  
  for (let i = 0; i < len; i++) {
    if (charOccurances.hasOwnProperty(str[i])) {
      charOccurances[str[i]] += 1;
    }
    else {
      charOccurances[str[i]] = 1;
    }
  }
  
  console.log('Checking occurances of chars for string ', str);
  console.log(charOccurances);
  
  const allPossiblePerms = factorial(len);
  
  console.log('logging number of all perms ', allPossiblePerms);
  
  return str;
}

const testCases = [
  ["aab", 2],
  ["aaa", 0],
  ["aabb", 8],
  ["abcdefa", 3600],
  ["abfdefa", 2640],
  ["zzzzzzzz", 0],
  ["a", 1],
  ["aaab", 0],
  ["aaabb", 12],
  ];
  
for (test of testCases) {
  console.log('TEST CASE ', test[0]);
  console.log('result: ', permAlone(test[0]));
  console.log('no repeats perms, expected: ', test[1]);
  console.log('---------------')
}

/* TESTS OUTPUT
Output:

TEST CASE  aab
Checking occurances of chars for string  aab
{ a: 2, b: 1 }
logging number of all perms  6
result:  aab
no repeats perms, expected:  2
---------------
TEST CASE  aaa
Checking occurances of chars for string  aaa
{ a: 3 }
logging number of all perms  6
result:  aaa
no repeats perms, expected:  0
---------------
TEST CASE  aabb
Checking occurances of chars for string  aabb
{ a: 2, b: 2 }
logging number of all perms  24
result:  aabb
no repeats perms, expected:  8
---------------
TEST CASE  abcdefa
Checking occurances of chars for string  abcdefa
{ a: 2, b: 1, c: 1, d: 1, e: 1, f: 1 }
logging number of all perms  5040
result:  abcdefa
no repeats perms, expected:  3600
---------------
TEST CASE  abfdefa
Checking occurances of chars for string  abfdefa
{ a: 2, b: 1, f: 2, d: 1, e: 1 }
logging number of all perms  5040
result:  abfdefa
no repeats perms, expected:  2640
---------------
TEST CASE  zzzzzzzz
Checking occurances of chars for string  zzzzzzzz
{ z: 8 }
logging number of all perms  40320
result:  zzzzzzzz
no repeats perms, expected:  0
---------------
TEST CASE  a
Checking occurances of chars for string  a
{ a: 1 }
logging number of all perms  1
result:  a
no repeats perms, expected:  1
---------------
TEST CASE  aaab
Checking occurances of chars for string  aaab
{ a: 3, b: 1 }
logging number of all perms  24
result:  aaab
no repeats perms, expected:  0
---------------
TEST CASE  aaabb
Checking occurances of chars for string  aaabb
{ a: 3, b: 2 }
logging number of all perms  120
result:  aaabb
no repeats perms, expected:  12
---------------
*/

I solved it by creating unique combinations of indexes. I looked up the suggested solutions and I still like my approach better lol

I just pushed my solution here below, but I don’t like it.
repo on git hub
I believe it can be solved by good mathematician and calculator without any building at all. Edit. Well, maybe it will be long process with just calculator, but I thnik there are formulas that can be useful here.
I looked up solutions, they are all building something also as far as I figured.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.