Banana-Leopard/TASTY huggingface dataset:
// C++ program for space optimized
// solution of Word Wrap problem.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find space optimized
// solution of Word Wrap problem.
void solveWordWrap(int arr[], int n, int k)
{
    int i, j;
 
    // Variable to store number of
    // characters in given line.
    int currlen;
 
    // Variable to store possible
    // minimum cost of line.
    int cost;
 
    // DP table in which dp[i] represents
    // cost of line starting with word
    // arr[i].
    int dp[n];
 
    // Array in which ans[i] store index
    // of last word in line starting with
    // word arr[i].
    int ans[n];
 
    // If only one word is present then
    // only one line is required. Cost
    // of last line is zero. Hence cost
    // of this line is zero. Ending point
    // is also n-1 as single word is
    // present.
    dp[n - 1] = 0;
    ans[n - 1] = n - 1;
 
    // Make each word first word of line
    // by iterating over each index in arr.
    for (i = n - 2; i >= 0; i--) {
        currlen = -1;
        dp[i] = INT_MAX;
 
        // Keep on adding words in current
        // line by iterating from starting
        // word upto last word in arr.
        for (j = i; j < n; j++) {
 
            // Update number of characters
            // in current line. arr[j] is
            // number of characters in
            // current word and 1
            // represents space character
            // between two words.
            currlen += (arr[j] + 1);
 
            // If limit of characters
            // is violated then no more
            // words can be added to
            // current line.
            if (currlen > k)
                break;
 
            // If current word that is
            // added to line is last
            // word of arr then current
            // line is last line. Cost of
            // last line is 0. Else cost
            // is square of extra spaces
            // plus cost of putting line
            // breaks in rest of words
            // from j+1 to n-1.
            if (j == n - 1)
                cost = 0;
            else
                cost = (k - currlen) * (k - currlen) + dp[j + 1];
 
            // Check if this arrangement gives
            // minimum cost for line starting
            // with word arr[i].
            if (cost < dp[i]) {
                dp[i] = cost;
                ans[i] = j;
            }
        }
    }
 
    // Print starting index and ending index
    // of words present in each line.
    i = 0;
    while (i < n) {
        cout << i + 1 << " " << ans[i] + 1 << " ";
        i = ans[i] + 1;
    }
}
 
// Driver function
int main()
{
    int arr[] = { 3, 2, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int M = 6;
    solveWordWrap(arr, n, M);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to print shortest possible path to
// type all characters of given string using a remote
#include <iostream>
using namespace std;
 
// Function to print shortest possible path to
// type all characters of given string using a remote
void printPath(string str)
{
    int i = 0;
    // start from character 'A' present at position (0, 0)
    int curX = 0, curY = 0;
    while (i < str.length())
    {
        // find coordinates of next character
        int nextX = (str[i] - 'A') / 5;
        int nextY = (str[i] - 'B' + 1) % 5;
 
        // Move Up if destination is above
        while (curX > nextX)
        {
            cout << "Move Up" << endl;
            curX--;
        }
 
        // Move Left if destination is to the left
        while (curY > nextY)
        {
            cout << "Move Left" << endl;
            curY--;
        }
 
        // Move down if destination is below
        while (curX < nextX)
        {
            cout << "Move Down" << endl;
            curX++;
        }
 
        // Move Right if destination is to the right
        while (curY < nextY)
        {
            cout << "Move Right" << endl;
            curY++;
        }
 
        // At this point, destination is reached
        cout << "Press OK" << endl;
        i++;
    }
}
 
// Driver code
int main()
{
    string str = "COZY";
 
    printPath(str);
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to find minimum number of points
// in a given path
#include <bits/stdc++.h>
using namespace std;
 
// method returns minimum number of points in given path
int numberOfPointInPath(string path)
{
 int N = path.length();
 
 // Map to store last occurrence of direction
 map<char, int> dirMap;
 
 // variable to store count of points till now,
 // initializing from 1 to count first point
 int points = 1;
 
 // looping over all characters of path string
 for (int i = 0; i < N; i++) {
 
  // storing current direction in curDir
  // variable
  char curDir = path[i];
 
  // marking current direction as visited
  dirMap[curDir] = 1;
 
  // if at current index, we found both 'L'
  // and 'R' or 'U' and 'D' then current
  // index must be a point
  if ((dirMap['L'] && dirMap['R']) ||
   (dirMap['U'] && dirMap['D'])) {
    
   // clearing the map for next segment
   dirMap.clear();
 
   // increasing point count
   points++;
 
   // revisiting current direction for next segment
   dirMap[curDir] = 1;
  }
 }
 
 // +1 to count the last point also
 return (points + 1);
}
 
// Driver code to test above methods
int main()
{
 string path = "LLUUULLDD";
 cout << numberOfPointInPath(path) << endl;
 return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP program to check whether second string
// can be formed from first string
#include <bits/stdc++.h>
using namespace std;
const int MAX = 256;
 
bool canMakeStr2(string str1, string str2)
{
    // Create a count array and count frequencies
    // characters in str1.
    int count[MAX] = {0};
    for (int i = 0; i < str1.length(); i++)
        count[str1[i]]++;
         
    // Now traverse through str2 to check
    // if every character has enough counts
    for (int i = 0; i < str2.length(); i++)
    {
        if (count[str2[i]] == 0)
           return false;
        count[str2[i]]--;
    }
    return true;
}
 
// Driver Code
int main()
{
    string str1 = "geekforgeeks";
    string str2 = "for";
    if (canMakeStr2(str1, str2))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ code to find the reverse alphabetical
// order from a given position
#include <iostream>
#include <string>
using namespace std;
 
// Function which take the given string
// and the position from which the reversing shall
// be done and returns the modified string
string compute(string str, int n)
{
    // Creating a string having reversed alphabetical order
    string reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba";
    int l = str.length();
 
    // The string up to the point specified in the question,
    // the string remains unchanged and from the point up to
    // the length of the string, we reverse the alphabetical
    // order
    for (int i = n; i < l; i++)
        str[i] = reverseAlphabet[str[i] - 'a'];
 
    return str;
}
 
// Driver function
int main()
{
    string str = "pneumonia";
    int n = 4;
    string answer = compute(str, n - 1);
    cout << answer;
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find last index of
// character x in given string.
#include <iostream>
using namespace std;
 
// Returns last index of x if it is present.
// Else returns -1.
int findLastIndex(string& str, char x)
{
    int index = -1;
    for (int i = 0; i < str.length(); i++)
        if (str[i] == x)
            index = i;
    return index;
}
 
// Driver code
int main()
{
    // String in which char is to be found
    string str = "geeksforgeeks";
 
    // char whose index is to be found
    char x = 'e';
    int index = findLastIndex(str, x);
    if (index == -1)
        cout << "Character not found";
    else
        cout << "Last index is " << index;
    return 0;
}

Time complexity: linear
Space complexity: constant
// Simple CPP program to find last index of
// character x in given string.
#include <iostream>
using namespace std;
 
// Returns last index of x if it is present.
// Else returns -1.
int findLastIndex(string& str, char x)
{
    // Traverse from right
    for (int i = str.length() - 1; i >= 0; i--)
        if (str[i] == x)
            return i;
 
    return -1;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    char x = 'e';
    int index = findLastIndex(str, x);
    if (index == -1)
        cout << "Character not found";
    else
        cout << "Last index is " << index;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find position of a number
// in a series of numbers with 4 and 7 as the
// only digits.
#include <iostream>
#include <algorithm>
using namespace std;
 
int findpos(string n)
{
    int i = 0, pos = 0;
    while (n[i] != '\0') {
 
        // check all digit position
        switch (n[i])
        {
 
        // if number is left then pos*2+1
        case '4':
            pos = pos * 2 + 1;
            break;
 
        // if number is right then pos*2+2
        case '7':
            pos = pos * 2 + 2;
            break;
        }
        i++;
    }
    return pos;
}
 
// Driver code
int main()
{
    // given a number which is constructed
    // by 4 and 7 digit only
    string n = "774";
    cout << findpos(n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++++ program to find winner in an election.
#include "bits/stdc++.h"
using namespace std;
 
/* We have four Candidates with name as 'John',
  'Johnny', 'jamie', 'jackie'.
   The votes in String array are as per the
   votes casted. Print the name of candidates
   received Max vote. */
void findWinner(vector<string>& votes)
{
 
    // Insert all votes in a hashmap
    unordered_map<string, int> mapObj;
    for (auto& str : votes) {
        mapObj[str]++;
    }
 
    // Traverse through map to find the candidate
    // with maximum votes.
    int maxValueInMap = 0;
    string winner;
    for (auto& entry : mapObj) {
        string key = entry.first;
        int val = entry.second;
        if (val > maxValueInMap) {
            maxValueInMap = val;
            winner = key;
        }
 
        // If there is a tie, pick lexicographically
        // smaller.
        else if (val == maxValueInMap && winner > key)
            winner = key;
    }
    cout << winner << endl;
}
 
// Driver code
int main()
{
    vector<string> votes
        = { "john", "johnny", "jackie", "johnny",
            "john", "jackie", "jamie",  "jamie",
            "john", "johnny", "jamie",  "johnny",
            "john" };
 
    findWinner(votes);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to check if a query string
// is present is given set.
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 256;
 
bool isPresent(string s, string q)
{
    // Count occurrences of all characters
    // in s.
    int freq[MAX_CHAR] = { 0 };
    for (int i = 0; i < s.length(); i++)
        freq[s[i]]++;
 
    // Check if number of occurrences of
    // every character in q is less than
    // or equal to that in s.
    for (int i = 0; i < q.length(); i++) {
        freq[q[i]]--;
        if (freq[q[i]] < 0)
           return false;
    }
 
    return true;
}
 
// driver program
int main()
{
    string s = "abctd";
    string q = "cat";
 
    if (isPresent(s, q))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find the arrangement
// of queue at time = t
#include <bits/stdc++.h>
using namespace std;
 
// prints the arrangement at time = t
void solve(int n, int t, string s)
{
    // Checking the entire queue for
    // every moment from time = 1 to
    // time = t.
    for (int i = 0; i < t; i++)
        for (int j = 0; j < n - 1; j++)
         
            /*If current index contains 'B'
              and next index contains 'G'
              then swap*/
            if (s[j] == 'B' && s[j + 1] == 'G') {
                char temp = s[j];
                s[j] = s[j + 1];
                s[j + 1] = temp;
                j++;
            }
             
    cout << s;
}
 
// Driver function for the program
int main()
{
    int n = 6, t = 2;
    string s = "BBGBBG";
    solve(n, t, s);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to check whether the
// given EMEI number is valid or not.
#include<bits/stdc++.h>
using namespace std;
 
// Function for finding and returning
// sum of digits of a number
int sumDig(int n)
{
    int a = 0;
    while (n > 0)
    {
        a = a + n % 10;
        n = n / 10;
    }
    return a;
}
 
bool isValidIMEI(long n)
{
     
    // Converting the number into 
    // String for finding length
    string s = to_string(n);
    int len = s.length();
 
    if (len != 15)
        return false;
 
    int sum = 0;
    for(int i = len; i >= 1; i--)
    {
       int d = (int)(n % 10);
        
       // Doubling every alternate digit
       if (i % 2 == 0)
           d = 2 * d;
            
       // Finding sum of the digits
       sum += sumDig(d);
       n = n / 10;
    }
     
    return (sum % 10 == 0);
}
 
// Driver code
int main()
{
    // 15 digits cannot be stored
    // in 'int' data type
    long n = 490154203237518L;
 
    if (isValidIMEI(n))
        cout << "Valid IMEI Code";
    else
        cout << "Invalid IMEI Code";
     
    return 0;
}
 
// This code is contributed by Yash_R

Time complexity: nlogn
Space complexity: linear
// C++ program to decode a median string
// to the original string
 
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate the median back string
string decodeMedianString(string s)
{
    // length of string
    int l = s.length();
 
    // initialize a blank string
    string s1 = "";
 
    // Flag to check if length is even or odd
    bool isEven = (l % 2 == 0)? true : false;
 
    // traverse from first to last
    for (int i = 0; i < l; i += 2) {
 
        // if len is even then add first character
        // to beginning of new string and second
        // character to end
        if (isEven) {  
            s1 = s[i] + s1;
            s1 += s[i + 1];
        } else {
 
            // if current length is odd and is
            // greater than 1
            if (l - i > 1) {
 
                // add first character to end and
                // second character to beginning
                s1 += s[i];
                s1 = s[i + 1] + s1;
            } else {
 
                // if length is 1, add character 
                // to end
                s1 += s[i];
            }
        }
    }
 
    return s1;
}
 
// driver program
int main()
{
    string s = "eekgs";
    cout << decodeMedianString(s);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to check if a
// given ISBN is valid or not
#include <bits/stdc++.h>
using namespace std;
 
bool isValidISBN(string& isbn)
{
    // length must be 10
    int n = isbn.length();
    if (n != 10)
        return false;
 
    // Computing weighted sum
    // of first 9 digits
    int sum = 0;
    for (int i = 0; i < 9; i++)
    {
        int digit = isbn[i] - '0';
        if (0 > digit || 9 < digit)
            return false;
        sum += (digit * (10 - i));
    }
 
    // Checking last digit.
    char last = isbn[9];
    if (last != 'X' && (last < '0' ||
                        last > '9'))
        return false;
 
    // If last digit is 'X', add 10
    // to sum, else add its value.
    sum += ((last == 'X') ? 10 :
                  (last - '0'));
 
    // Return true if weighted sum
    // of digits is divisible by 11.
    return (sum % 11 == 0);
}
 
// Driver code
int main()
{
    string isbn = "007462542X";
    if (isValidISBN(isbn))
        cout << "Valid";
    else
        cout << "Invalid";
    return 0;
}

Time complexity: constant
Space complexity: constant
// C++ program to check if a given credit
// card is valid or not.
#include <iostream>
using namespace std;
 
// Return this number if it is a single digit, otherwise,
// return the sum of the two digits
int getDigit(int number)
{
  if (number < 9)
    return number;
  return number / 10 + number % 10;
}
 
// Return the number of digits in d
int getSize(long d)
{
  string num = to_string(d);
  return num.length();
}
 
// Return the first k number of digits from
// number. If the number of digits in number
// is less than k, return number.
long getPrefix(long number, int k)
{
  if (getSize(number) > k)
  {
    string num = to_string(number);
    return stol(num.substr(0, k));
  }
  return number;
}
 
// Return true if the digit d is a prefix for number
bool prefixMatched(long number, int d)
{
  return getPrefix(number, getSize(d)) == d;
}
 
// Get the result from Step 2
int sumOfDoubleEvenPlace(long int number)
{
  int sum = 0;
  string num = to_string(number) ;
  for (int i = getSize(number) - 2; i >= 0; i -= 2)
    sum += getDigit(int(num[i] - '0') * 2);
 
  return sum;
}
 
// Return sum of odd-place digits in number
int sumOfOddPlace(long number)
{
  int sum = 0;
  string num = to_string(number) ;
  for (int i = getSize(number) - 1; i >= 0; i -= 2)
    sum += num[i] - '0';
  return sum;
}
 
// Return true if the card number is valid
bool isValid(long int number)
{
  return (getSize(number) >= 13 &&
          getSize(number) <= 16) &&
    (prefixMatched(number, 4) ||
     prefixMatched(number, 5) ||
     prefixMatched(number, 37) ||
     prefixMatched(number, 6)) &&
    ((sumOfDoubleEvenPlace(number) +
      sumOfOddPlace(number)) % 10 == 0);
}
 
// Driver Code
int main()
{
  long int number = 5196081888500645L;
  cout << number << " is " <<  (isValid(number) ? "valid" : "invalid");
  return 0;
}
 
// This code is contributed by yuvraj_chandra

Time complexity: linear
Space complexity: constant
// CPP program to Maximize the given number.
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to maximize the number N with
// limit as M.
string maximizeNumber(string N, int M)
{
    // Sorting the digits of the
    // number in increasing order.
    sort(N.begin(), N.end());
 
    for (int i = 0; i < N.size(); i++) {
        for (int j = i + 1; j < N.size(); j++) {
 
            // Copying the string into another
            // temp string.
            string t = N;
 
            // Swapping the j-th char(digit)
            // with i-th char(digit)
            swap(t[j], t[i]);
 
            // Sorting the temp string
            // from i-th pos to end.
            sort(t.begin() + i + 1, t.end());
 
            // Checking if the string t is
            // greater than string N and less
            // than or equal to the number M.
            if (stoll(t) > stoll(N) && stoll(t) <= M)
 
                // If yes then, we will permanently
                // swap the i-th char(or digit)
                // with j-th char(digit).
                swap(N[i], N[j]);
        }
    }
 
    // Returns the maximized number.
    return N;
}
 
// Driver function
int main()
{
    string N = "123";
    int M = 222;
    cout << maximizeNumber(N, M);
    return 0;
}
// This code is contributed by KaaL-EL.

Time complexity: cubic
Space complexity: linear
// CPP program to find if a given corner string
// is present at corners.
#include <bits/stdc++.h>
using namespace std;
 
bool isCornerPresent(string str, string corner)
{
    int n = str.length();
    int cl = corner.length();
 
    // If length of corner string is more, it
    // cannot be present at corners.
    if (n < cl)
       return false;
 
    // Return true if corner string is present at
    // both corners of given string.
    return (str.substr(0, cl).compare(corner) == 0 &&
            str.substr(n-cl, cl).compare(corner) == 0);
}
 
// Driver code
int main()
{
   string str = "geeksforgeeks";
   string corner = "geeks";
   if (isCornerPresent(str, corner))
      cout << "Yes";
   else
      cout << "No";
   return 0;
}

Time complexity: linear
Space complexity: constant