Pages

Saturday, May 31, 2025

Remove Duplicates using Recursion in C++

 #include<iostream>

#include<vector>
#include<cmath>
using namespace std;

void removeDuplicates(string str, string ans, int i, int map[26]) {
    // Base case
    if(i == str.size()) {
        cout << "ans: " << ans << endl;
    return;
    }

    int mapIdx = (int)(str[i] - 'a');

    if(map[mapIdx]) { // duplicate
            removeDuplicates(str, ans, i+1, map);


    } else {// not duplicate
    map[mapIdx] = true;
    removeDuplicates(str, ans+str[i], i+1, map);
    }
}

int main() {
    string str = "appnnacollege";
    string ans = "";
    int map[26] = {false};



    removeDuplicates(str, ans,0, map);
return 0;

}

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...