FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-examples/cpp-algorithm/src/hashtable/find_anagram.h at main · codejsha/algorithm-examples · GitHub
codejsha
/
algorithm-examples
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
hashtable
/
find_anagram.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (38 loc) · 1.17 KB
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
hashtable
/
find_anagram.h
Copy path
File metadata and controls
43 lines (38 loc) · 1.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#
ifndef
CPP_ALGORITHM_FIND_ANAGRAM_H
#
define
CPP_ALGORITHM_FIND_ANAGRAM_H
#
include
<
algorithm
>
#
include
<
string
>
#
include
<
unordered_map
>
#
include
<
vector
>
namespace
FindAnagram
{
/*
*
* \brief Find the anagram mappings.
* \param dictionary the dictionary of words
* \return the anagram mappings
*/
std::vector<std::vector<std::string>>
FindAnagramMappings
(
const
std::vector<std::string>& dictionary);
}
//
----------------------------------------------------------------------------
inline
std::vector<std::vector<std::string>>
FindAnagram::FindAnagramMappings
(
const
std::vector<std::string>& dictionary)
{
std::unordered_map<std::string, std::vector<std::string>> dictionary_map;
for
(
const
std::string& word : dictionary)
{
std::string sorted_word = word;
std::ranges::sort
(sorted_word);
dictionary_map[sorted_word].
emplace_back
(word);
}
std::vector<std::vector<std::string>> anagram_mappings;
for
(
const
auto
& [word, anagrams] : dictionary_map)
{
if
(anagrams.
size
() >
1
)
{
anagram_mappings.
emplace_back
(anagrams);
}
}
return
anagram_mappings;
}
#
endif
Back
|
FazBrowse Home
|
New Git URL