0

I have a problem, and can't figure out a soulution right now.

I have a string like this "abcd dfeg uyti lksh bxnm abcd uyti".

And i want to replace the duplicates with some other text (i use a function to generate random letters).

But there is a problem, that the new text is alerdy generated.

EXAMPLE INPUT "aaaa bbbb cccc bbbb cccc dddd eeee"

My output is like this "aaaa bbbb cccc aaaa eeee dddd eeee". which is wrong

But the output i want is like this "aaaa bbbb cccc ffff gggg dddd eeee"

I tried this code

function formatTEXT(text) {
    let words = text.split(" ");
    let replaced = new Set();
    let duplicatesFound = true;
    while (duplicatesFound) {
        const wordCounts = {};
        duplicatesFound = false;
        for (let i = 0; i < words.length; i++) {
            const word = words[i];
            if (!wordCounts[word]) {
                wordCounts[word] = 1;
            } else {
                wordCounts[word]++;
            }
        }

        for (let i = 0; i < words.length; i++) {
            let replacement = generateTags(lettersN);
            const word = words[i];
            if (!replaced.has(word) && wordCounts[word] > 1 && !wordCounts[replacement]) {
                words[i] = replacement;
                replaced.add(word);
                duplicatesFound = true;
                console.log(replacement);
            }
        }
    }
    return words.join(" ");
}

formatTEXT("aaaa bbbb cccc bbbb cccc dddd eeee");

but it still does not work, there are still some duplicates in here.

5
  • Can you provide some meaningful input/output, uyti lksh bxnm it not helpful. Commented Jan 10, 2023 at 17:23
  • What have you found out during debugging? Commented Jan 10, 2023 at 17:28
  • With !wordCounts[replacement] as condition this function's result strongly depends on what generateTags does. Without its implementation what do you expect us to say? Commented Jan 10, 2023 at 18:53
  • The generateTags function generates random text example ajss kdjf rufj etc. Commented Jan 10, 2023 at 20:00
  • What if your input is the word "aa", repeated 27 times? Do you expect the random generator to replace the other 26 instances with "bb" or attempt to cycle through all lowercase letters? If the latter case is expected, what should the last instance be replaced with, since all lowercase letters would be exhausted at that point? Commented Jan 10, 2023 at 21:09

2 Answers 2

1
function formatText(text) {
  let hashMappedString = text
    .split(' ')
    .reduce((previousState, currentElement) => {
      previousState[currentElement] = currentElement;
      return previousState;
    }, {});
  

  var newWords = '';
  for (let word in hashMappedString) {
    newWords += ` ${word}`;
    console.log(word);
  }
  return newWords.trim(); // to remove the space in the beginning
}
console.log(formatText('aaaa bbbb cccc bbbb cccc dddd eeee')); 

// this code has the time complexity of O(n) + O(n) which is O(2n) which is O(n) by ignoring the constant

Sign up to request clarification or add additional context in comments.

Comments

0

What you could do is build a list while checking for duplicates, then join it back into a string:

function formatText(text) {
    let words = text.split(" ");
    var newWords = []
    for (let i in words) {
        if (!newWords.includes(words[i])) {
            newWords.push(words[i])
        }
    }
    return newWords.join(" ")
}

console.log(formatText("aaaa bbbb cccc bbbb cccc dddd eeee"))

Output: "aaaa bbbb cccc dddd eeee"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.