How to sort the string based on the number of matchsticks for representation?


Introduction

In this tutorial, we implement an approach to sort a string on the basis of the number of matchsticks required for its representation. In this approach, we use N numbers of matchsticks and sort an array. The array can contain numbers, words, or both. Matchsticks are used to arrange them in the shape of a particular number or character.

Demonstration 1

Input = Arr = ["1", "3", "4"]
Output = The sorted array is 1 4 3 

Explanation

In the above input array, the array elements are 1, 3, and 4

Number of matchsticks required for 1 = 1

Number of matchsticks required for 3 = 5

Number of matchsticks required for 4 = 4

Demonstration 2

Input = Arr = ["23", "HI", "ABC" ]
Output = The sorted array is HI 23 ABC

Explanation

In the above input array, the array elements are 23, HI, and C.

Number of matchsticks required for 23 = 10

Number of matchsticks required for "HI" = 6

Number of matchsticks required for "ABC" = 17

C++ Library Functions

  • sizeof() − This a compile time unary operator in C++ that helps in finding the size of the variables, constant and data types.

sizeof(value);
  • vector − It is a dynamic array in C++. It provides effective array functionalities. It is one of the data structures for storing elements.

vector<data_type> vcetor_name;
  • vector:begin() − It is a predefined function in vector class and is defined in the <vector> header file. It returns the pointer of the starting element of the vector.

vector_name.begin();
  • vector:end() − It is a predefined function in vector class and is defined in the <vector> header file. It returns the pointer of the last element of the vector.

vector_name.end();
  • vector:sort() − It is a predefined function in vector class and is defined in the <vector> header file. It sorts the vector elements by using the begin() and end() functions as parameters.

sort(vector_name.begin(), vector_name.end());
  • unordered_map − It is a data structure in C++ that stores elements with unique key- value pairs. Its elements are not sorted in a particular manner.

unordered_map<data_type> map_name;
  • size() − It is a library function in C++ that returns the length of the input value. It is defined in the standard library of C++.

value.size();
  • auto − It is an automatic variable in C++. It helps in runtime declaring the data type of the variable.

auto auto_name;

Algorithm

  • Take an input array.

  • Declare a variable to store the number of matchsticks required to make each of the 26 alphabets.

  • Declare a variable to store the number of matchsticks required to make numbers from 0 to 9.

  • Iterate over each array element to find the number of sticks required.

  • Sort the array element on the basis of the number of matchsticks.

  • Print the sorted array elements.

Example 1

We implemented the tutorial problem statement with C++. Use a vector to store the array strings. The

#include <bits/stdc++.h>
using namespace std;

// array to store the number of matchsticks required to form characters from A to Z
int matchsticksAlphabets[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 };

//array to store the number of matchsticks required to from numbers from 0 to 9
int matchsticksNumber[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };

// Function for counting the number of matchsticks using count variable
int countingSticks(string strVar) {
   int cntVar = 0;

   // iterating each string of the array through for loop
   for (int x = 0; strVar[x]; x++) {   
      char charVar = strVar[x];
      if (charVar >= 'A' && charVar <= 'Z') 
         cntVar += matchsticksAlphabets[charVar - 'A'];
      else 
         cntVar += matchsticksNumber[charVar - '0'];
   }
   return cntVar;
}

// Function for sorting the array elements as per number of matchsticks
void sortingArr(string arrEle[], int s){   
   vector<pair<int, string> > vpVar;  // Vector for storing the number of matchsticks for each string
   for (int x = 0; x < s; x++) {
      vpVar.push_back(make_pair(countingSticks(arrEle[x]),arrEle[x]));
   }
   sort(vpVar.begin(), vpVar.end()); // function for sorting the vector elements
   cout << "The sorted array is: ";
   for (int x = 0; x < vpVar.size(); x++) // Printing the vector with sorted elements
      cout << vpVar[x].second << " ";
}

int main(){
   string arrEle[] = { "23", "HI", "ABC" };
   int s = sizeof(arrEle) / sizeof(arrEle[0]);
   sortingArr(arrEle, s);  //calling function for sorting the array
   return 0;
}

Output

The sorted array is: HI 23 ABC 

Example 2

We implement the tutorial problem of sorting a string on the basis of the number of matchsticks needed to represent its characters with C++. To implement the approach we use map and its functions for sorting the strings based on the number of matchsticks. Comparing operators are used for sorting the array elements.

#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;

//array for storing the number of matchsticks to form alphabets from A to Z
int alphabetSticks[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 };

// array for storing the number of matchsticks to form numbers from 0 to 9
int numberSticks[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };

// declaring function for count the matchsticks
int countingSticks(const string& strVar, const unordered_map<char, int>& matchstickCounts) {
   int cntVar = 0;
   for (char charVar : strVar)  {
      auto itVar = matchstickCounts.find(charVar);   //declaring automatic variable 
      if (itVar != matchstickCounts.end())
         cntVar += itVar->second;
   }
   return cntVar;   
}
    
//function for finding the more matchsticks using map functions
bool compareStringsByMatchsticks(const string& strVar1, const string& strVar2, const unordered_map<char, int>& matchstickCounts) {
   int matchsticks1 = countingSticks(strVar1, matchstickCounts);
   int matchsticks2 = countingSticks(strVar2, matchstickCounts);
   return matchsticks1 < matchsticks2;   
}
    
// declaring a function for sorting the array with number of matchsticks
void sortedArr(string arrVar[], int s, const unordered_map<char, int>& matchstickCounts) {
   sort(arrVar, arrVar + s, [&](const string& strVar1, const string& strVar2){
      return compareStringsByMatchsticks(strVar1, strVar2, matchstickCounts); 
   });
   cout << "The sorted array elements are : ";
   for (int x = 0; x < s; x++)  // Printing the array sorted on the basis of the number of matchsticks
      cout << arrVar[x] << " ";  
}

int main() {
   unordered_map<char, int> matchstickCounts;  // Define map for storing the strings 
   for (int x = 0; x < 26; x++)
      matchstickCounts['A' + x] = alphabetSticks[x];
   for (int x = 0; x < 10; x++) 
      matchstickCounts['0' + x] = numberSticks[x];
   string arrVar[] = { "123", "HI", "ABC" };
   int s = sizeof(arrVar) / sizeof(arrVar[0]);
   sortedArr(arrVar, s, matchstickCounts);
   return 0;
}

Output

The sorted array elements are : HI 123 ABC

Conclusion

We have reached the end of this tutorial. In this tutorial, we sorted the array strings by the number of matchsticks. Matchsticks form characters and numbers. The total number of matchsticks required is stored in two arrays. We implemented an approach to the task using a map and a vector. Different functions of map and vector are used for sorting the array elements by finding the number of matchsticks for each string in the array.

Updated on: 03-Oct-2023

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements