Banana-Leopard/TASTY huggingface dataset:
// C++ program to implement custom trim() function
#include <iostream>
using namespace std;
 
// Function to in-place trim all spaces in the
// string such that all words should contain only
// a single space between them.
void removeSpaces(string &str)
{
    // n is length of the original string
    int n = str.length();
 
    // i points to next position to be filled in
    // output string/ j points to next character
    // in the original string
    int i = 0, j = -1;
 
    // flag that sets to true is space is found
    bool spaceFound = false;
 
    // Handles leading spaces
    while (++j < n && str[j] == ' ');
 
    // read all characters of original string
    while (j < n)
    {
        // if current characters is non-space
        if (str[j] != ' ')
        {
            // remove preceding spaces before dot,
            // comma & question mark
            if ((str[j] == '.' || str[j] == ',' ||
                 str[j] == '?') && i - 1 >= 0 &&
                 str[i - 1] == ' ')
                str[i - 1] = str[j++];
 
            else
                // copy current character at index i
                // and increment both i and j
                str[i++] = str[j++];
 
            // set space flag to false when any
            // non-space character is found
            spaceFound = false;
        }
        // if current character is a space
        else if (str[j++] == ' ')
        {
            // If space is encountered for the first
            // time after a word, put one space in the
            // output and set space flag to true
            if (!spaceFound)
            {
                str[i++] = ' ';
                spaceFound = true;
            }
        }
    }
 
    // Remove trailing spaces
    if (i <= 1)
        str.erase(str.begin() + i, str.end());
    else
        str.erase(str.begin() + i - 1, str.end());
}
 
// Driver Code
int main()
{
    string str = "   Hello Geeks . Welcome   to"
                 "  GeeksforGeeks   .    ";
 
    removeSpaces(str);
 
    cout << str;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
void replaceSpaces(string input)
{
    string rep = "%20";
    for(int i=0 ; i<input.length() ; i++)
    {
        if(input[i] == ' ')
            input.replace(i,1,rep);
    }
    cout<<input;
}
 
int main()
{
    string input = "Mr John Smith";
    replaceSpaces(input);
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to replace spaces with %20
#include<stdio.h>
 
// Maximum length of string after modifications.
const int MAX = 1000;
 
// Replaces spaces with %20 in-place and returns
// new length of modified string. It returns -1
// if modified string cannot be stored in str[]
int replaceSpaces(char str[])
{
    // count spaces and find current length
    int space_count = 0, i;
    for (i = 0; str[i]; i++)
        if (str[i] == ' ')
            space_count++;
 
    // Remove trailing spaces
    while (str[i-1] == ' ')
    {
       space_count--;
       i--;
    }
 
    // Find new length.
    int new_length = i + space_count * 2 + 1;
 
    // New length must be smaller than length
    // of string provided.
    if (new_length > MAX)
        return -1;
 
    // Start filling character from end
    int index = new_length - 1;
 
    // Fill string termination.
    str[index--] = '\0';
 
    // Fill rest of the string from end
    for (int j=i-1; j>=0; j--)
    {
        // inserts %20 in place of space
        if (str[j] == ' ')
        {
            str[index] = '0';
            str[index - 1] = '2';
            str[index - 2] = '%';
            index = index - 3;
        }
        else
        {
            str[index] = str[j];
            index--;
        }
    }
 
    return new_length;
}
 
// Driver code
int main()
{
    char str[MAX] = "Mr John Smith   ";
 
    // Prints the replaced string
    int new_length = replaceSpaces(str);
    for (int i=0; i<new_length; i++)
        printf("%c", str[i]);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find the string which contain
// the first character of each word of another
// string.
#include<bits/stdc++.h>
using namespace std;
 
// Function to find string which has first
// character of each word.
string firstLetterWord(string str)
{
    string result = "";
 
    // Traverse the string.
    bool v = true;
    for (int i=0; i<str.length(); i++)
    {
        // If it is space, set v as true.
        if (str[i] == ' ')
            v = true;
 
        // Else check if v is true or not.
        // If true, copy character in output
        // string and set v as false.
        else if (str[i] != ' ' && v == true)
        {
            result.push_back(str[i]);
            v = false;
        }
    }
 
    return result;
}
 
// Driver code
int main()
{
    string str = "geeks for geeks";
    cout << firstLetterWord(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
string processWords(char *input)
{
    /* we are splitting the input based on
    spaces (s)+ : this regular expression
    will handle scenarios where we have words
    separated by multiple spaces */
    char *p;
    vector<string> s;
 
    p = strtok(input, " ");
    while (p != NULL)
    {
        s.push_back(p);
        p = strtok(NULL, " ");
    }
 
    string charBuffer;
 
    for (string values : s)
 
        /* charAt(0) will pick only the first character
        from the string and append to buffer */
        charBuffer += values[0];
 
    return charBuffer;
}
 
// Driver code
int main()
{
    char input[] = "geeks for geeks";
    cout << processWords(input);
    return 0;
}
 
// This code is contributed by
// sanjeev2552

Time complexity: quadratic
Space complexity: linear
// C++ program to print all strings that can be
// made by placing spaces
#include <bits/stdc++.h>
using namespace std;
 
void printSubsequences(string str)
{
    int n = str.length();
    unsigned int opsize = pow(2, n - 1);
 
    for (int counter = 0; counter < opsize; counter++) {
        for (int j = 0; j < n; j++) {
 
            cout << str[j];
            if (counter & (1 << j))
                cout << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    string str = "ABC";
    printSubsequences(str);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to check whether two strings are anagrams
// of each other
#include <bits/stdc++.h>
using namespace std;
 
/* function to check whether two strings are anagram of
each other */
bool areAnagram(string str1, string str2)
{
    // Get lengths of both strings
    int n1 = str1.length();
    int n2 = str2.length();
 
    // If length of both strings is not same, then they
    // cannot be anagram
    if (n1 != n2)
        return false;
 
    // Sort both the strings
    sort(str1.begin(), str1.end());
    sort(str2.begin(), str2.end());
 
    // Compare sorted strings
    for (int i = 0; i < n1; i++)
        if (str1[i] != str2[i])
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    string str1 = "gram";
    string str2 = "arm";
 
    // Function Call
    if (areAnagram(str1, str2))
        cout << "The two strings are anagram of each other";
    else
        cout << "The two strings are not anagram of each "
                "other";
 
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to check if two strings
// are anagrams of each other
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
 
/* function to check whether two strings are anagram of
each other */
bool areAnagram(char* str1, char* str2)
{
    // Create 2 count arrays and initialize all values as 0
    int count1[NO_OF_CHARS] = { 0 };
    int count2[NO_OF_CHARS] = { 0 };
    int i;
 
    // For each character in input strings, increment count
    // in the corresponding count array
    for (i = 0; str1[i] && str2[i]; i++) {
        count1[str1[i]]++;
        count2[str2[i]]++;
    }
 
    // If both strings are of different length. Removing
    // this condition will make the program fail for strings
    // like "aaca" and "aca"
    if (str1[i] || str2[i])
        return false;
 
    // Compare count arrays
    for (i = 0; i < NO_OF_CHARS; i++)
        if (count1[i] != count2[i])
            return false;
 
    return true;
}
 
/* Driver code*/
int main()
{
    char str1[] = "gram";
    char str2[] = "arm";
 
    // Function Call
    if (areAnagram(str1, str2))
        cout << "The two strings are anagram of each other";
    else
        cout << "The two strings are not anagram of each "
                "other";
 
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: linear
Space complexity: constant
// C++ program to check if two strings
// are anagrams of each other
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
 
bool areAnagram(char* str1, char* str2)
{       
    // Create a count array and initialize all values as 0
    int count[NO_OF_CHARS] = { 0 };
    int i;
   
    // For each character in input strings, increment count
    // in the corresponding count array
    for (i = 0; str1[i] && str2[i]; i++) {
        count[str1[i]]++;
        count[str2[i]]--;
    }
   
    // If both strings are of different length. Removing
    // this condition will make the program fail for strings
    // like "aaca" and "aca"
    if (str1[i] || str2[i])
        return false;
 
    // See if there is any non-zero value in count array
    for (i = 0; i < NO_OF_CHARS; i++)
        if (count[i])
            return false;
    return true;
}
 
// Driver code
int main()
{
    char str1[] = "gram";
    char str2[] = "arm";
   
    // Function call
    if (areAnagram(str1, str2))
        cout << "The two strings are anagram of each other";
    else
        cout << "The two strings are not anagram of each "
                "other";
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ implementation of the approach
// Function that returns true if a and b
// are anagarams of each other
#include <bits/stdc++.h>
using namespace std;
 
bool isAnagram(string a, string b)
{
 
    // Check if length of both strings is same or not
    if (a.length() != b.length()) {
        return false;
    }
    // Create a HashMap containing Character as Key and
    // Integer as Value. We will be storing character as
    // Key and count of character as Value.
    unordered_map<char, int> Map;
    // Loop over all character of String a and put in
    // HashMap.
    for (int i = 0; i < a.length(); i++) {
        Map[a[i]]++;
    }
    // Now loop over String b
    for (int i = 0; i < b.length(); i++) {
        // Check if current character already exists in
        // HashMap/map
        if (Map.find(b[i]) != Map.end()) {
            // If contains reduce count of that
            // character by 1 to indicate that current
            // character has been already counted as
            // idea here is to check if in last count of
            // all characters in last is zero which
            // means all characters in String a are
            // present in String b.
            Map[b[i]] -= 1;
        }
        else {
            return false;
        }
    }
 
    // Loop over all keys and check if all keys are 0.
    // If so it means it is anagram.
    for (auto items : Map) {
        if (items.second != 0) {
            return false;
        }
    }
    // Returning True as all keys are zero
    return true;
}
 
// Driver code
int main()
{
    string str1 = "gram";
    string str2 = "arm";
 
    // Function call
    if (isAnagram(str1, str2))
        cout << "The two strings are anagram of each other"
             << endl;
    else
        cout << "The two strings are not anagram of each "
                "other"
             << endl;
}
 
// This code is contributed by shinjanpatra

Time complexity: linear
Space complexity: constant
// C++ program to search all anagrams of a pattern in a text
#include <bits/stdc++.h>
#define MAX 256
using namespace std;
 
// This function returns true if contents of arr1[] and
// arr2[] are same, otherwise false.
bool compare(char arr1[], char arr2[])
{
    for (int i = 0; i < MAX; i++)
        if (arr1[i] != arr2[i])
            return false;
    return true;
}
 
// This function search for all permutations of pat[] in
// txt[]
void search(char* pat, char* txt)
{
    int M = strlen(pat), N = strlen(txt);
 
    // countP[]:  Store count of all characters of pattern
    // countTW[]: Store count of current window of text
    char countP[MAX] = { 0 }, countTW[MAX] = { 0 };
    for (int i = 0; i < M; i++) {
        (countP[pat[i]])++;
        (countTW[txt[i]])++;
    }
 
    // Traverse through remaining characters of pattern
    for (int i = M; i < N; i++) {
        // Compare counts of current window of text with
        // counts of pattern[]
        if (compare(countP, countTW))
            cout << "Found at Index " << (i - M) << endl;
 
        // Add current character to current window
        (countTW[txt[i]])++;
 
        // Remove the first character of previous window
        countTW[txt[i - M]]--;
    }
 
    // Check for the last window in text
    if (compare(countP, countTW))
        cout << "Found at Index " << (N - M) << endl;
}
 
/* Driver program to test above function */
int main()
{
    char txt[] = "BACDGABCDA";
    char pat[] = "ABCD";
    search(pat, txt);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: linear
// C++ program to find minimum number of characters
// to be removed to make two strings anagram.
#include<bits/stdc++.h>
using namespace std;
const int CHARS = 26;
 
// function to calculate minimum numbers of characters
// to be removed to make two strings anagram
int remAnagram(string str1, string str2)
{
    // make hash array for both string and calculate
    // frequency of each character
    int count1[CHARS] = {0}, count2[CHARS] = {0};
 
    // count frequency of each character in first string
    for (int i=0; str1[i]!='\0'; i++)
        count1[str1[i]-'a']++;
 
    // count frequency of each character in second string
    for (int i=0; str2[i]!='\0'; i++)
        count2[str2[i]-'a']++;
 
    // traverse count arrays to find number of characters
    // to be removed
    int result = 0;
    for (int i=0; i<26; i++)
        result += abs(count1[i] - count2[i]);
    return result;
}
 
// Driver program to run the case
int main()
{
    string str1 = "bcadeh", str2 = "hea";
    cout << remAnagram(str1, str2);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ implementation to count number of deletions
// required from two strings to create an anagram
#include <bits/stdc++.h>
using namespace std;
const int CHARS = 26;
 
int countDeletions(string str1, string str2)
{
    int arr[CHARS] = {0};
    for (int i = 0; i < str1.length(); i++)
        arr[str1[i] - 'a']++;
     
    for (int i = 0; i < str2.length(); i++)
        arr[str2[i] - 'a']--;
     
    long long int ans = 0;
    for(int i = 0; i < CHARS; i++)
        ans +=abs(arr[i]);
    return ans;
}
 
int main() {
    string str1 = "bcadeh", str2 = "hea";
    cout << countDeletions(str1, str2);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to check if two strings are k anagram
// or not.
#include<bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
 
// Function to check that string is k-anagram or not
bool arekAnagrams(string str1, string str2, int k)
{
    // If both strings are not of equal
    // length then return false
    int n = str1.length();
    if (str2.length() != n)
        return false;
 
    int count1[MAX_CHAR] = {0};
    int count2[MAX_CHAR] = {0};
 
    // Store the occurrence of all characters
    // in a hash_array
    for (int i = 0; i < n; i++)
        count1[str1[i]-'a']++;
    for (int i = 0; i < n; i++)
        count2[str2[i]-'a']++;
      
    int count = 0;
 
    // Count number of characters that are
    // different in both strings
    for (int i = 0; i < MAX_CHAR; i++)
        if (count1[i] > count2[i])
            count = count + abs(count1[i]-count2[i]);
 
    // Return true if count is less than or
    // equal to k
    return (count <= k);
}
 
// Driver code
int main()
{
    string str1 = "anagram";
    string str2 = "grammar";
    int k = 2;
    if (arekAnagrams(str1, str2, k))
        cout << "Yes";
    else
        cout<< "No";
    return 0;
}

Time complexity: linear
Space complexity: constant
// Optimized C++ program to check if two strings
// are k anagram or not.
#include<bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 26;
 
// Function to check if str1 and str2 are k-anagram
// or not
bool areKAnagrams(string str1, string str2, int k)
{
    // If both strings are not of equal
    // length then return false
    int n = str1.length();
    if (str2.length() != n)
        return false;
 
    int hash_str1[MAX_CHAR] = {0};
 
    // Store the occurrence of all characters
    // in a hash_array
    for (int i = 0; i < n ; i++)
        hash_str1[str1[i]-'a']++;
 
    // Store the occurrence of all characters
    // in a hash_array
    int count = 0;
    for (int i = 0; i < n ; i++)
    {
        if (hash_str1[str2[i]-'a'] > 0)
            hash_str1[str2[i]-'a']--;
        else
            count++;
 
        if (count > k)
            return false;
    }
 
    // Return true if count is less than or
    // equal to k
    return true;
}
 
// Driver code
int main()
{
    string str1 = "fodr";
    string str2 = "gork";
    int k = 2;
    if (areKAnagrams(str1, str2, k) == true)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check k
// anagram of two strings
bool kAnagrams(string str1, string str2, int k)
{
    int flag = 0;
 
    // First Condition: If both the
    // strings have different length ,
    // then they cannot form anagram
    if (str1.length() != str2.length())
        return false;
    int n = str1.length();
    // Converting str1 to Character Array arr1
    char arr1[n];
 
    // Converting str2 to Character Array arr2
    char arr2[n];
 
    strcpy(arr1, str1.c_str());
    strcpy(arr2, str2.c_str());
    // Sort arr1 in increasing order
    sort(arr1, arr1 + n);
 
    // Sort arr2 in increasing order
    sort(arr2, arr2 + n);
 
    vector<char> list;
   
    // Iterate till str1.length()
    for (int i = 0; i < str1.length(); i++) {
 
        // Condition if arr1[i] is
        // not equal to arr2[i]
        // then add it to list
        if (arr1[i] != arr2[i]) {
            list.push_back(arr2[i]);
        }
    }
 
    // Condition to check if
    // strings for K-anagram or not
    if (list.size() <= k)
        flag = 1;
 
    if (flag == 1)
        return true;
    else
        return false;
}
 
// Driver Code
int main()
{
 
    string str1 = "anagram", str2 = "grammar";
    int k = 3;
 
    // Function Call
    kAnagrams(str1, str2, k);
    if (kAnagrams(str1, str2, k) == true)
        cout << "Yes";
    else
        cout << "No";
 
    // This code is contributed by bolliranadheer
}

Time complexity: linear
Space complexity: constant
// C++ program to check if binary
// representations of two numbers are anagrams.
#include <bits/stdc++.h>
 
using namespace std;
 
// Check each bit in a number is set or not
// and return the total count of the set bits.
int countSetBits(int n)
{
    int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
 
bool areAnagrams(int A, int B)
{
    return countSetBits(A) == countSetBits(B);
}
 
// Driver code
int main()
{
    int a = 8, b = 4;
    cout << areAnagrams(a, b) << endl;
    return 0;
}
 
// This code is contributed by phasing17

Time complexity: constant
Space complexity: constant
// C++ program for finding all anagram
// pairs in the given array
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
 
// Utility function for
// printing anagram list
void printAnagram(unordered_map<string,vector<string> >& store)
{
    for (auto it:store)
    {
        vector<string> temp_vec(it.second);
        int size = temp_vec.size();
       
        for (int i = 0; i < size; i++)
        cout << temp_vec[i] << " ";
           
        cout << "\n";
    }
}
 
// Utility function for storing
// the vector of strings into HashMap
void storeInMap(vector<string>& vec)
{
    unordered_map<string,vector<string> > store;
   
    for (int i = 0; i < vec.size(); i++)
    {
        string tempString(vec[i]);
       
          // sort the string
        sort(tempString.begin(),tempString.end());
       
          // make hash of a sorted string
        store[tempString].push_back(vec[i]);
    }
 
    // print utility function for printing
    // all the anagrams
    printAnagram(store);
}
 
// Driver code
int main()
{
    // initialize vector of strings
    vector<string> arr;
    arr.push_back("geeksquiz");
    arr.push_back("geeksforgeeks");
    arr.push_back("abcd");
    arr.push_back("forgeeksgeeks");
    arr.push_back("zuiqkeegs");
    arr.push_back("cat");
    arr.push_back("act");
    arr.push_back("tca");
 
    // utility function for storing
    // strings into hashmap
    storeInMap(arr);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to count total anagram
// substring of a string
#include <bits/stdc++.h>
using namespace std;
 
// Total number of lowercase characters
#define MAX_CHAR 26
 
// Utility method to return integer index
// of character 'c'
int toNum(char c)
{
    return (c - 'a');
}
 
// Returns count of total number of anagram
// substrings of string str
int countOfAnagramSubstring(string str)
{
    int N = str.length();
 
    // To store counts of substrings with given
    // set of frequencies.
    map<vector<int>, int> mp;
 
    // loop for starting index of substring
    for (int i=0; i<N; i++)
    {
        vector<int> freq(MAX_CHAR, 0);
 
        // loop for length of substring
        for (int j=i; j<N; j++)
        {
            // update freq array of current
            // substring
            freq[toNum(str[j])]++;
 
            // increase count corresponding
            // to this freq array
            mp[freq]++;
        }
    }
 
    // loop over all different freq array and
    // aggregate substring count
    int result = 0;
    for (auto it=mp.begin(); it!=mp.end(); it++)
    {
        int freq = it->second;
        result += ((freq) * (freq-1))/2;
    }
    return result;
}
 
//  Driver code to test above methods
int main()
{
    string str = "xyyx";
    cout << countOfAnagramSubstring(str) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: linear