Pinta problem solution - 7-2 Remove duplicate characters

7-2 Remove duplicate characters

Original title:

After removing duplicate characters from the given string, sort the characters in ASCII code order from small to large and then output.

Input format :

Input is a non-empty string (less than 80 characters) terminated by a carriage return.

Output format:

Output the result string after dereordering.

.

Problem-solving ideas:

Problem-solving ideas:

  1. Remove duplicates through set
  2. Custom sorting

.

JavaScript (node) code:

const r = require("readline");

const rl = r.createInterface({
    
    
    input: process.stdin,
    output: process.stdout
});

rl.question('',(input)=>{
    
    
    
    const uniqueChars = [...new Set(input)];
    
    const sortedStr = uniqueChars.sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0)).join('');

    console.log(`${
      
      sortedStr}`);

    rl.close();
});


.

Complexity analysis:

Time complexity: O(n+klogk)
Space complexity: O(n)

Guess you like

Origin blog.csdn.net/Mredust/article/details/132800044