I’m trying to solve this question:
Have the function
CountingAnagrams
take thestr
parameter and determine how many anagrams exist in the string.
Examples:
Input: "aa aa odg dog gdo"
— Output: 2
Input: "a c b c run urn urn"
— Output: 1
I tried this solution but it does not display the correct answer. What am I doing wrong?
const CountingAnagrams = (str) => {
let l = str.length,
c = 0,
c1,
c2;
if (l % 2 == 0) {
c1 = str.slice(0, l / 2).split("");
c2 = str.slice(l / 2).split("");
let l2 = c1.length;
for (let i = 0; i < l2; i++) {
let id = c2.indexOf(c1[i]);
if (id !== -1) {
c2[id] = " ";
}
else {
c += 1;
}
}
}
else {
return "-1";
}
return c;
};
console.log("cars are very cool so are arcs and my os", CountingAnagrams("cars are very cool so are arcs and my os"));
console.log("aa aa odg dog gdo", CountingAnagrams("aa aa odg dog gdo"));
console.log("a c b c run urn urn", CountingAnagrams("a c b c run urn urn"));
I’m trying to solve this question:
Have the function
CountingAnagrams
take thestr
parameter and determine how many anagrams exist in the string.
Examples:
Input: "aa aa odg dog gdo"
— Output: 2
Input: "a c b c run urn urn"
— Output: 1
I tried this solution but it does not display the correct answer. What am I doing wrong?
const CountingAnagrams = (str) => {
let l = str.length,
c = 0,
c1,
c2;
if (l % 2 == 0) {
c1 = str.slice(0, l / 2).split("");
c2 = str.slice(l / 2).split("");
let l2 = c1.length;
for (let i = 0; i < l2; i++) {
let id = c2.indexOf(c1[i]);
if (id !== -1) {
c2[id] = " ";
}
else {
c += 1;
}
}
}
else {
return "-1";
}
return c;
};
console.log("cars are very cool so are arcs and my os", CountingAnagrams("cars are very cool so are arcs and my os"));
console.log("aa aa odg dog gdo", CountingAnagrams("aa aa odg dog gdo"));
console.log("a c b c run urn urn", CountingAnagrams("a c b c run urn urn"));
Share
edited Jun 26, 2020 at 7:23
Om3ga
asked Jun 25, 2020 at 13:04
Om3gaOm3ga
33.1k45 gold badges149 silver badges230 bronze badges
4
- @user4642212 Thats how it is in the question. Because of RUN and URN. I posted the entire question now. – Om3ga Commented Jun 25, 2020 at 13:17
-
Please provide a minimal reproducible example. You can use Stack Snippets (icon looks like
<>
in the editor toolbar) to provide code that can be run here on Stack Overflow. – Heretic Monkey Commented Jun 25, 2020 at 13:17 -
What kind of “solution” is that? Right from the get-go: if the string length is odd, it returns
"-1"
; this seems pletely unrelated to the question. – Sebastian Simon Commented Jun 25, 2020 at 13:32 -
1. split into words. 2. create object to hold sorted words as keys and array of words as value. 3. for each word, sort it to be a key in object. 4. if key doesn't exist, create key with value empty array (
[]
). 5. if word doesn't exist in value,push
it. 6. repeat 3 until all words processed. 7. Iterate over object and count how many keys have value of length > 1. – iAmOren Commented Jun 25, 2020 at 13:41
2 Answers
Reset to default 9
const CountingAnagrams = (str) => {
// Set() helps to remove all the duplicates
const wordUnique = new Set(str.split(/\s+/)),
wordArray = [
...wordUnique
],
hash = {};
let count = 0;
wordArray.forEach((word) => {
// Key will be the sorted word e.g. cba will bee abc
let key = word.split('').sort().join('');
// If there is an anagram they will have the same key so whenever the key is avaialable in the hash count will be updated
if (key in hash) {
count += 1;
}
else {
// true is assigned just for making the key available in the hash
hash[key] = true;
}
});
return count;
};
console.log("cars are very cool so are arcs and my os", CountingAnagrams("cars are very cool so are arcs and my os"));
console.log("aa aa odg dog gdo", CountingAnagrams("aa aa odg dog gdo"));
console.log("a c b c run urn urn", CountingAnagrams("a c b c run urn urn"));
Sliding Window Solution C++ //(count is stored in ans and anagram positions is in vector v
vector<int> findAnagrams(string s, string ana) {
unordered_map<char, int> m;
for(auto it : ana) m[it]++;
int k=ana.length();
int count=m.size();
int i=0, j=0;
int ans=0;
vector<int>v;
while(j<s.length()){
if(m.find(s[j])!=m.end()){
m[s[j]]--;
if(m[s[j]]==0) count--;
}
if((j-i+1)<k) j++;
else if((j-i+1)==k){
if(count==0){
ans++;
v.push_back(i);
}
if(m.find(s[i])!=m.end()){
m[s[i]]++;
if(m[s[i]]==1) count++;
}
i++;
j++;
}
}
cout<<ans;
return v;}