Skip to content

Commit 7297039

Browse files
committedJan 5, 2018
Added sort chars by frequency problem and some refactoring
1 parent 0732437 commit 7297039

File tree

8 files changed

+66
-350
lines changed

8 files changed

+66
-350
lines changed
 

‎CMakeCache.txt

-311
This file was deleted.

‎CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ foreach( sourcefile ${SOURCES} )
2828
continue()
2929
endif()
3030
endforeach( sourcefile ${SOURCES} )
31-

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,6 @@ Include contains single header implementation of data structures and some algori
229229
| The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance.| [hamming_distance.cpp](leet_code_problems/hamming_distance.cpp)|
230230
| Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.| [merge_trees.cpp](leet_code_problems/merge_trees.cpp)|
231231
| Write a function that takes a string as input and reverse only the vowels of a string.|[reverse_vowels.cpp](leet_code_problems/reverse_vowels.cpp)|
232+
| Given a string, sort it in decreasing order based on the frequency of characters.For example: <ul><li>Input: cccbbbbaa Output: bbbcccaa</li></ul>| [sortCharByFrequency.cpp](leet_code_problems/sortCharByFrequency.cpp)|
232233

233234

‎common_ds_algo_problems/minHeap.cpp

-35
This file was deleted.
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Given a string, sort it in decreasing order based on the frequency of characters.
3+
* Example:
4+
* Input: cccbbbbaa
5+
* Output: bbbcccaa
6+
*
7+
* Input: AaaBBaa
8+
* Output: aaaaBBA
9+
*
10+
* Input: cccaaa
11+
* Output:cccaaa or aaaccc
12+
*
13+
* Notice that order of chars in alphabatical order does not matter for same frequency elements.
14+
* However, also all the same characters should be together. cacaca will not be a valid answer.
15+
*/
16+
17+
#include <iostream>
18+
#include <unordered_map>
19+
#include <map>
20+
21+
std::string frequencySort(std::string s) {
22+
23+
// Create a hash map of our characters with frequency as their values.
24+
std::unordered_map<char,int> frequencyMap;
25+
for (char c : s) {
26+
frequencyMap[c]++;
27+
}
28+
29+
// Now we will maintain an another ordered map whose key would be
30+
// the frequency and values would be accumulated chars of same frequency.
31+
//
32+
std::map<int, std::string> stringMap;
33+
for (auto frqPair : frequencyMap) {
34+
char c = frqPair.first;
35+
int n = frqPair.second;
36+
stringMap[n] += std::string(n, c);
37+
}
38+
39+
// Now pick the accumulated string in reverse order (highest first)
40+
std::string result;
41+
for (auto it = stringMap.rbegin(); it != stringMap.rend(); ++it) {
42+
result += it->second;
43+
}
44+
return result;
45+
}
46+
47+
int main()
48+
{
49+
std::string input1{"cccbbbbaa"};
50+
std::cout << "Input: " << input1 << std::endl;
51+
std::cout << "Output:" << frequencySort(input1) << std::endl;
52+
std::string input2{"cccaaa"};
53+
std::cout << "Input:" << input2 << std::endl;
54+
std::cout << "Output:" << frequencySort(input2) << std::endl;
55+
return 0;
56+
}

‎sort_search_problems/find_pairs_with_sum.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414

1515

16-
#include <bits/stdc++.h>
16+
#include <iostream>
17+
#include <vector>
18+
#include <unordered_map>
1719

1820

1921
void printPairWithSum(const std::vector<int>& arr, const int sum)

‎tree_problems/invert_a_tree.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*
99
*/
1010

11-
#include <bits/stdc++.h>
11+
#include <iostream>
12+
#include <queue>
13+
#include <iomanip>
1214

1315
struct TreeNode
1416
{

‎tree_problems/reverseLevelOrderTraversal.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919

2020

21-
#include <bits/stdc++.h>
21+
#include <iostream>
22+
#include <stack>
23+
#include <queue>
2224

2325

2426
struct TreeNode

0 commit comments

Comments
 (0)
Please sign in to comment.