Remove duplicate characters in a string

Problem Given a string, Write a program to remove duplicate characters from the string. Examples Example 1: Input: s = kodeknight Output: kodenight Example 2: Input: s = k5kc Output: k5c Solution There are multiple approach to solve it. Method 1 : Brute Force Code C void removeDuplicates(char *str) { if (!str) { return; } int len = strlen(str); if (len < 2){ return; } int tail = 1; for (int i = 1; i < len; ++i) { int j; for (j = 0; j < tail; ++j) if (str[i] == str[j]) { break; } if (j == tail) { str[tail] = str[i]; ++tail; } } str[tail] = '\0'; } ...

Determine if a string has all unique characters

Problem Implement an algorithm to determine if a string has all unique characters. Solution Method 1 - Brute Force - Compare All Characters Brute force way can be we take each character in the string and compare it to every other character in the string. We do this using for loops. This would mean a time complexity of O(n^2) because of the nested loop. ...

Longest Substring with K Distinct Characters

Problem Given a string, find the longest substring that contains only k unique or distinct characters. Examples Example 1: Input: S = "aabacbebebe", K = 3 Output: 7 Explanation: "cbebebe" is the longest substring with 3 distinct characters. Example 2: Input: S = "aaaa", K = 2 Output: -1 Explanation: There's no substring with 2 distinct characters. ...

Remove duplicates from an unsorted linked list keeping only one instance

Problem Write code to remove duplicates from an unsorted linked list. FOLLOW UP: How would you solve this problem if a temporary buffer is not allowed? Examples Example 1: 2 -> 3 -> 2 -> 5 -> 2 -> 8 -> 2 -> 3 -> 8 -> null => 2 -> 3 -> 5 -> 8 -> null ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy