Banana-Leopard/TASTY huggingface dataset:
// C++ program to find k'th non-repeating character
// in a string
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 256;
 
// Returns index of k'th non-repeating character in
// given string str[]
int kthNonRepeating(string str, int k)
{
    int n = str.length();
 
    // count[x] is going to store count of
    // character 'x' in str. If x is not present,
    // then it is going to store 0.
    int count[MAX_CHAR];
 
    // index[x] is going to store index of character
    // 'x' in str.  If x is not  present or x is
    // repeating, then it is going to store  a value
    // (for example, length of string) that cannot be
    // a valid index in str[]
    int index[MAX_CHAR];
 
    // Initialize counts of all characters and indexes
    // of non-repeating  characters.
    for (int i = 0; i < MAX_CHAR; i++)
    {
        count[i] = 0;
        index[i] = n;  // A value more than any index
                       // in str[]
    }
 
    // Traverse the input string
    for (int i = 0; i < n; i++)
    {
        // Find current character and increment its
        // count
        char x = str[i];
        ++count[x];
 
        // If this is first occurrence, then set value
        // in index as index of it.
        if (count[x] == 1)
            index[x] = i;
 
        // If character repeats, then remove it from
        // index[]
        if (count[x] == 2)
            index[x] = n;
    }
 
    // Sort index[] in increasing order.  This step
    // takes O(1) time as size of index is 256 only
    sort(index, index+MAX_CHAR);
 
    // After sorting, if index[k-1] is value, then
    // return it, else return -1.
    return (index[k-1] != n)? index[k-1] : -1;
}
 
// Driver code
int main()
{
   string str = "geeksforgeeks";
   int k = 3;
   int res = kthNonRepeating(str, k);
   (res == -1)? cout << "There are less than k non-"
                        "repeating characters"
              : cout << "k'th non-repeating character"
                       " is  "<< str[res];
   return 0;
}

Time complexity: nlogn
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
#define MAX_CHAR 256
int kthNonRepeating(string input, int k)
{
    int inputLength = input.length();
 
    /*
     * indexArr will store index of non-repeating
     * characters, inputLength for characters not in input
     * and inputLength+1 for repeated characters.
     */
    int indexArr[MAX_CHAR];
 
    // initialize all values in indexArr as inputLength.
    for (int i = 0; i < MAX_CHAR; i++) {
        indexArr[i] = inputLength;
    }
    for (int i = 0; i < inputLength; i++) {
        char c = input[i];
        if (indexArr == inputLength) {
            indexArr = i;
        }
        else {
            indexArr = inputLength + 2;
        }
    }
 
    sort(indexArr, indexArr + MAX_CHAR);
    return (indexArr[k - 1] != inputLength)
               ? indexArr[k - 1]
               : -1;
}
 
int main()
{
    string input = "geeksforgeeks";
    int k = 3;
    int res = kthNonRepeating(input, k);
    if (res == -1)
        cout << "There are less than k non-repeating "
                "characters";
    else
        cout << "k'th non-repeating character is  "
             << input[res];
    return 0;
}
 
// This code is contributed by gauravrajput1

Time complexity: nlogn
Space complexity: linear
// CPP program to find the first
// repeated character in a string
#include <bits/stdc++.h>
using namespace std;
 
// Returns first repeating character in str.
char firstRepeating(string &str)
{
    // Creates an empty hashset
    unordered_set<char> h;
 
    // Traverse the input array from left to right
    for (int i=0; i<str.length(); i++)
    {
        char c = str[i];
 
        // If element is already in hash set, update x
        // and then break
        if (h.find(c) != h.end())
            return c;
 
        else // Else add element to hash set
            h.insert(c);
    }
 
    // If there was no repeated character
    return '\0';
}
 
// Driver method to test above method
int main ()
{
    string str = "geeksforgeeks";
    cout << firstRepeating(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP code to find most frequent word in
// an array of strings
#include <bits/stdc++.h>
using namespace std;
 
void mostFrequentWord(string arr[], int n)
{
    // freq to store the freq of the most occurring variable
    int freq = 0;
    // res to store the most occurring string in the array of
    // strings
    string res;
 
    // running nested for loops to find the most occurring
    // word in the array of strings
    for (int i = 0; i < n; i++) {
        int count = 0;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] == arr[i]) {
                count++;
            }
        }
        // updating our max freq of occured string in the
        // array of strings
        if (count >= freq) {
            res = arr[i];
            freq = count;
        }
    }
 
    cout << "The word that occurs most is : " << res
         << endl;
    cout << "No of times: " << freq << endl;
}
 
// Driver code
int main()
{
    // given set of keys
    string arr[]
        = { "geeks",   "for",   "geeks", "a",    "portal",
            "to",      "learn", "can",   "be",   "computer",
            "science", "zoom",  "yup",   "fire", "in",
            "be",      "data",  "geeks" };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    mostFrequentWord(arr, n);
 
    return 0;
}

Time complexity: constant
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
class Solution {
public:
    // Function to find most frequent word
    // in an array of strings.
    string mostFrequentWord(string arr[], int n)
    {
        unordered_map<string, int> freq;
        unordered_map<string, int> occurrence;
        int max = 0;
        string result;
        int k = 1;
 
        for (int i = 0; i < n; i++) {
            if (occurrence.count(arr[i]) > 0) {
                continue;
            }
 
            occurrence[arr[i]] = k++;
        }
 
        for (int i = 0; i < n; i++) {
 
            freq[arr[i]]++;
 
            if (max <= freq[arr[i]]) {
 
                if (max < freq[arr[i]]) {
                    max = freq[arr[i]];
                    result = arr[i];
                }
                else {
                    if (occurrence[result]
                        < occurrence[arr[i]]) {
                        max = freq[arr[i]];
                        result = arr[i];
                    }
                }
            }
        }
 
        return result;
    }
};
 
int main()
{
 
    string arr[]
        = { "geeks",   "for",   "geeks", "a",    "portal",
            "to",      "learn", "can",   "be",   "computer",
            "science", "zoom",  "yup",   "fire", "in",
            "be",      "data",  "geeks" };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    Solution obj;
    cout << obj.mostFrequentWord(arr, n) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// c++ implementation
// Function returns word with highest frequency
 
#include <bits/stdc++.h>
using namespace std;
 
// Function returns word with highest frequency
string findWord(vector<string> arr)
{
    // Create HashMap to store word and it's frequency
    unordered_map<string, int> hs;
    // Iterate through array of words
    for (int i = 0; i < arr.size(); i++) {
        hs[arr[i]]++;
    }
 
    string key = "";
    int value = 0;
    for (auto me : hs) {
        // Check for word having highest frequency
        if (me.second > value) {
            value = me.second;
            key = me.first;
        }
    }
    // Return word having highest frequency
    return key;
}
 
int main()
{
    vector<string> arr{ "geeks",    "for",     "geeks",
                        "a",        "portal",  "to",
                        "learn",    "can",     "be",
                        "computer", "science", "zoom",
                        "yup",      "fire",    "in",
                        "be",       "data",    "geeks" };
    string sol = findWord(arr);
    // Print word having highest frequency
    cout << sol << endl;
}
 
// This code is contributed by Aarti_Rathi

Time complexity: linear
Space complexity: linear
// CPP code to find most frequent word in
// an array of strings
#include <bits/stdc++.h>
using namespace std;
 
/*structing the trie*/
struct Trie {
    string key;
    int cnt;
    unordered_map<char, Trie*> map;
};
 
/* Function to return a new Trie node */
Trie* getNewTrieNode()
{
    Trie* node = new Trie;
    node->cnt = 0;
    return node;
}
 
/* function to insert a string */
void insert(Trie*& root, string& str, int& maxCount,
            string& mostFrequentString)
{
    // start from root node
    Trie* temp = root;
 
    for (int i = 0; i < str.length(); i++) {
 
        char x = str[i];
 
        /*a new node if path doesn't exists*/
        if (temp->map.find(x) == temp->map.end())
            temp->map[x] = getNewTrieNode();
 
        // go to next node
        temp = temp->map[x];
    }
 
    // store key and its count in leaf nodes
    temp->key = str;
    temp->cnt += 1;
    if (maxCount < temp->cnt) {
        maxCount = temp->cnt;
        mostFrequentString = str;
    }
}
 
void mostFrequentWord(string arr[], int n)
{
    // Insert all words in a Trie
    Trie* root = getNewTrieNode();
    int cnt = 0;
    string key = "";
    for (int i = 0; i < n; i++)
        insert(root, arr[i], cnt, key);
 
    cout << "The word that occurs most is : " << key
         << endl;
    cout << "No of times: " << cnt << endl;
}
 
// Driver code
int main()
{
    // given set of keys
    string arr[]
        = { "geeks",   "for",   "geeks", "a",    "portal",
            "to",      "learn", "can",   "be",   "computer",
            "science", "zoom",  "yup",   "fire", "in",
            "be",      "data",  "geeks" };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    mostFrequentWord(arr, n);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// Efficiently check First repeated character
// in C++ program
#include<bits/stdc++.h>
using namespace std;
 
// Returns -1 if all characters of str are
// unique.
// Assumptions : (1) str contains only characters
//                 from 'a' to 'z'
//             (2) integers are stored using 32
//                 bits
int FirstRepeated(string str)
{
    // An integer to store presence/absence
    // of 26 characters using its 32 bits.
    int checker = 0;
 
    for (int i = 0; i < str.length(); ++i)
    {
        int val = (str[i]-'a');
 
        // If bit corresponding to current
        // character is already set
        if ((checker & (1 << val)) > 0)
            return i;
 
        // set bit in checker
        checker |= (1 << val);
    }
 
    return -1;
}
 
// Driver code
int main()
{
    string s = "abcfdeacf";
    int i=FirstRepeated(s);
    if (i!=-1)
        cout <<"Char = "<< s[i] << "   and Index = "<<i;
    else
        cout << "No repeated Char";
    return 0;
}

Time complexity: linear
Space complexity: constant
// Queries for same characters in a repeated
// string
#include <bits/stdc++.h>
using namespace std;
 
// Print whether index i and j have same
// element or not.
void query(char s[], int i, int j)
{
    int n = strlen(s);
 
    // Finding relative position of index i,j.
    i %= n;
    j %= n;
 
    // Checking is element are same at index i, j.
    (s[i] == s[j]) ? (cout << "Yes" << endl)
                   : (cout << "No" << endl);
}
 
// Driven Program
int main()
{
    char X[] = "geeksforgeeks";
 
    query(X, 0, 8);
    query(X, 8, 13);
    query(X, 6, 15);
 
    return 0;
}

Time complexity: constant
Space complexity: constant
// C++ program to count occurrences of a given
// character
#include <iostream>
#include <string>
using namespace std;
 
// Function that return count of the given
// character in the string
int count(string s, char c)
{
    // Count variable
    int res = 0;
 
    for (int i=0;i<s.length();i++)
 
        // checking character in string
        if (s[i] == c)
            res++;
 
    return res;
}
 
// Driver code
int main()
{
    string str= "geeksforgeeks";
    char c = 'e';
    cout << count(str, c) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to count occurrences of
// a character using library
#include<bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    char c = 'e';
   
    // Count returns number of occurrences of
    // c between two given positions provided
    // as two iterators.
    cout << count(str.begin(), str.end(), c);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print the string in given pattern
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the string
void printStringAlternate(string str)
{
    unordered_map<char, int> occ;
 
    // Start traversing the string
    for (int i = 0; i < str.length(); i++) {
 
        // Convert uppercase to lowercase
        char temp = tolower(str[i]);
 
        // Increment occurrence count
        occ[temp]++;
 
        // If count is odd then print the character
        if (occ[temp] & 1)
            cout << str[i];
    }
 
    cout << endl;
}
 
// Drivers code
int main()
{
    string str = "Geeks for geeks";
    string str2 = "It is a long day Dear";
 
    printStringAlternate(str);
    printStringAlternate(str2);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// Program to find all occurrences of the word in
// a matrix
#include <bits/stdc++.h>
using namespace std;
 
#define ROW 3
#define COL 5
 
// check whether given cell (row, col) is a valid
// cell or not.
bool isvalid(int row, int col, int prevRow, int prevCol)
{
    // return true if row number and column number
    // is in range
    return (row >= 0) && (row < ROW) &&
           (col >= 0) && (col < COL) &&
           !(row== prevRow && col == prevCol);
}
 
// These arrays are used to get row and column
// numbers of 8 neighboursof a given cell
int rowNum[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int colNum[] = {-1, 0, 1, -1, 1, -1, 0, 1};
 
// A utility function to do DFS for a 2D boolean
// matrix. It only considers the 8 neighbours as
// adjacent vertices
void DFS(char mat[][COL], int row, int col,
         int prevRow, int prevCol, char* word,
         string path, int index, int n)
{
    // return if current character doesn't match with
    // the next character in the word
    if (index > n || mat[row][col] != word[index])
        return;
 
    //append current character position to path
    path += string(1, word[index]) + "(" + to_string(row)
            + ", " + to_string(col) + ") ";
 
    // current character matches with the last character
    // in the word
    if (index == n)
    {
        cout << path << endl;
        return;
    }
 
    // Recur for all connected neighbours
    for (int k = 0; k < 8; ++k)
        if (isvalid(row + rowNum[k], col + colNum[k],
                    prevRow, prevCol))
 
            DFS(mat, row + rowNum[k], col + colNum[k],
                row, col, word, path, index+1, n);
}
 
// The main function to find all occurrences of the
// word in a matrix
void findWords(char mat[][COL], char* word, int n)
{
    // traverse through the all cells of given matrix
    for (int i = 0; i < ROW; ++i)
        for (int j = 0; j < COL; ++j)
 
            // occurrence of first character in matrix
            if (mat[i][j] == word[0])
 
                // check and print if path exists
                DFS(mat, i, j, -1, -1, word, "", 0, n);
}
 
// Driver program to test above function
int main()
{
    char mat[ROW][COL]= { {'B', 'N', 'E', 'Y', 'S'},
                          {'H', 'E', 'D', 'E', 'S'},
                          {'S', 'G', 'N', 'D', 'E'}
                        };
 
    char word[] ="DES";
 
    findWords(mat, word, strlen(word) - 1);
 
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to arrange given string
#include <bits/stdc++.h>
using namespace std;
 
// Function which arrange the given string
void arrangeString(string str, int x, int y)
{
    int count_0 = 0;
    int count_1 = 0;
    int len = str.length();
 
    // Counting number of 0's and 1's in the
    // given string.
    for (int i = 0; i < len; i++) {
        if (str[i] == '0')
            count_0++;
        else
            count_1++;
    }
 
    // Printing first all 0's x-times
    // and decrement count of 0's x-times
    // and do the similar task with '1'
    while (count_0 > 0 || count_1 > 0) {
     for (int j = 0; j < x && count_0 > 0; j++) {
        if (count_0 > 0) {
           cout << "0";
           count_0--;
        }
     }
     for (int j = 0; j < y && count_1 > 0; j++) {
        if (count_1 > 0) {
           cout << "1";
           count_1--;
        }
      }
    }
}
 
// Driver Code
int main()
{
    string str = "01101101101101101000000";
    int x = 1;
    int y = 2;
    arrangeString(str, x, y);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// Finds maximum occurring digit without using any array/string
#include <bits/stdc++.h>
using namespace std;
 
// Simple function to count occurrences of digit d in x
int countOccurrences(long int x, int d)
{
    int count = 0;  // Initialize count of digit d
    while (x)
    {
        // Increment count if current digit is same as d
        if (x%10 == d)
           count++;
        x = x/10;
    }
    return count;
}
 
// Returns the max occurring digit in x
int maxOccurring(long int x)
{
   // Handle negative number
   if (x < 0)
      x = -x;
 
   int result = 0; // Initialize result which is a digit
   int max_count = 1; // Initialize count of result
 
   // Traverse through all digits
   for (int d=0; d<=9; d++)
   {
      // Count occurrences of current digit
      int count = countOccurrences(x, d);
 
      // Update max_count and result if needed
      if (count >= max_count)
      {
         max_count = count;
         result = d;
      }
   }
   return result;
}
 
// Driver program
int main()
{
    long int x = 1223355;
    cout << "Max occurring digit is " << maxOccurring(x);
    return 0;
}

Time complexity: constant
Space complexity: constant
// C++ program to bring all spaces in front of
// string using swapping technique
#include<bits/stdc++.h>
using namespace std;
 
// Function to find spaces and move to beginning
void moveSpaceInFront(char str[])
{
    // Traverse from end and swap spaces
    int i = strlen(str)-1;
    for (int j = i; j >= 0; j--)
        if (str[j] != ' ')
            swap(str[i--], str[j]);
}
 
// Driver code
int main()
{
    char str[] = "Hey there, it's GeeksforGeeks";
    moveSpaceInFront(str);
    cout << str;
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to bring all spaces in front of
// string using swapping technique
#include<bits/stdc++.h>
using namespace std;
 
// Function to find spaces and move to beginning
void moveSpaceInFront(char str[])
{
     // Keep copying non-space characters
     int i = strlen(str);
     for (int j=i; j >= 0; j--)
          if (str[j] != ' ')
             str[i--] = str[j];
 
     // Move spaces to be beginning
     while (i >= 0)
         str[i--] = ' ';
}
 
// Driver code
int main()
{
    char str[] = "Hey there, it's GeeksforGeeks";
    moveSpaceInFront(str);
    cout << str;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to put spaces between words starting
// with capital letters.
#include <iostream>
using namespace std;
 
// Function to amend the sentence
void amendSentence(string str)
{
    // Traverse the string
    for(int i=0; i < str.length(); i++)
    {
        // Convert to lowercase if its
        // an uppercase character
        if (str[i]>='A' && str[i]<='Z')
        {
            str[i]=str[i]+32;
 
            // Print space before it
            // if its an uppercase character
            if (i != 0)
                cout << " ";
 
            // Print the character
            cout << str[i];
        }
 
        // if lowercase character
        // then just print
        else
            cout << str[i];
    }
}
 
// Driver code
int main()
{
    string str ="BruceWayneIsBatman";
    amendSentence(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to remove spaces using stringstream
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove spaces
string removeSpaces(string str)
{
    stringstream ss;
    string temp;
 
    // Storing the whole string
    // into string stream
    ss << str;
 
    // Making the string empty
    str = "";
 
    // Running loop till end of stream
    while (!ss.eof()) {
 
        // Extracting word by word from stream
        ss >> temp;
 
        // Concatenating in the string to be
        // returned
        str = str + temp;
    }
    return str;
}
 
// Driver function
int main()
{
    // Sample Inputs
    string s = "This is a test";
    cout << removeSpaces(s) << endl;
 
    s = "geeks for geeks";
    cout << removeSpaces(s) << endl;
 
    s = "geeks quiz is awesome!";
    cout << removeSpaces(s) << endl;
 
    s = "I love     to     code";
    cout << removeSpaces(s) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to remove spaces using stringstream
// and getline()
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove spaces
string removeSpaces(string str)
{
    // Storing the whole string
    // into string stream
    stringstream ss(str);
    string temp;
 
    // Making the string empty
    str = "";
 
    // Running loop till end of stream
    // and getting every word
    while (getline(ss, temp, ' ')) {
        // Concatenating in the string
        // to be returned
        str = str + temp;
    }
    return str;
}
// Driver function
int main()
{
    // Sample Inputs
    string s = "This is a test";
    cout << removeSpaces(s) << endl;
 
    s = "geeks for geeks";
    cout << removeSpaces(s) << endl;
 
    s = "geeks quiz is awesome!";
    cout << removeSpaces(s) << endl;
 
    s = "I   love     to     code";
    cout << removeSpaces(s) << endl;
 
    return 0;
}
 
// Code contributed by saychakr13

Time complexity: linear
Space complexity: linear