-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake_words.cpp
More file actions
151 lines (127 loc) · 3.75 KB
/
make_words.cpp
File metadata and controls
151 lines (127 loc) · 3.75 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Created by Mayank Parasar on 2020-04-04.
//
/*
* Given a phone number, return all valid words that can be created using that phone number.
For instance, given the phone number 364
we can construct the words ['dog', 'fog'].
Here's a starting point:
lettersMaps = {
1: [],
2: ['a', 'b', 'c'],
3: ['d', 'e', 'f'],
4: ['g', 'h', 'i'],
5: ['j', 'k', 'l'],
6: ['m', 'n', 'o'],
7: ['p', 'q', 'r', 's'],
8: ['t', 'u', 'v'],
9: ['w', 'x', 'y', 'z'],
0: []
}
validWords = ['dog', 'fish', 'cat', 'fog']
def makeWords(phone):
#Fill this in
print(makeWords('364'))
# ['dog', 'fog']
* */
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
map<int, vector<char>> lettersMaps;
vector<string> validWords = {"dog", "fish", "cat", "fog"};
vector<int> int_to_vec(int num) {
vector<int> result;
while (num > 0) {
result.push_back(num % 10);
num = num / 10;
}
// reverse the vector
reverse(result.begin(), result.end());
return result;
}
vector<string> make_words(int num) {
// convert the num into a vector;
vector<int>numbers = int_to_vec(num);
// create a map of string and bool..
// bool would mark the words which are still valid...
map<string, bool> word_map;
// mark all the words as valid to begin with
// then progressively make them invalid.
for(auto word : validWords) {
word_map[word] = false;
}
for(int ii = 0; ii < numbers.size(); ii++) {
vector<char> availChar = lettersMaps[numbers[ii]];
for(auto word : word_map) {
// only check the words which are true..
bool match = false;
if(ii == 0 || (word.second == true && ii > 0)) {
for (auto ch : availChar) {
match = false;
if (word.first[ii] == ch) {
// word.second = true; // this does not set the value of map correctly
word_map[word.first] = true; // always use [] operator to set the value of map
match = true;
break;
}
}
}
if (!match) {
word.second = false;
}
}
// print map
// cout << boolalpha;
// for(auto i : word_map) {
// cout << i.first << ": ";
// cout << i.second << " ";
//
// cout << endl;
// }
}
// now also make any other entry false whose length is
// not same as number vector
for(auto word : word_map) {
if((word.second) &&
((int)word.first.length() != (int) numbers.size())) {
word_map[word.first] = false;
}
}
// extract the result from the map...
vector<string> result;
for(auto word : word_map) {
if(word.second == true)
result.push_back(word.first);
}
return result;
}
int main() {
// populate the map here...
lettersMaps[1] = {};
lettersMaps[2] = {'a', 'b', 'c'};
lettersMaps[3] = {'d', 'e', 'f'};
lettersMaps[4] = {'g', 'h', 'i'};
lettersMaps[5] = {'j', 'k', 'l'};
lettersMaps[6] = {'m', 'n', 'o'};
lettersMaps[7] = {'p', 'q', 'r', 's'};
lettersMaps[8] = {'t', 'u', 'v'};
lettersMaps[9] = {'w', 'x', 'y', 'z'};
lettersMaps[0] = {};
// print map
// for(auto i : lettersMaps) {
// cout << i.first << ": ";
// for(auto k : i.second) {
// cout << k << " ";
// }
// cout << endl;
// }
int num = 364;
vector<string> result = make_words(num);
cout << "words that can be formed with given number are: " << endl;
for(auto word : result)
cout << word << ", ";
return 0;
}