<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[string permutation]]></title><description><![CDATA[<p>Hi,<br />
i want to check if one is a permutation of the other one...any comments to my approach?</p>
<pre><code>bool in_string_permutation(string &amp;s1, string &amp;s2) {
	std::unordered_map&lt;int,int&gt; ht;
	auto s_it = s2.begin();
	bool check = true;

	if(s1.length() != s2.length()) {
		return false;
	}

	for_each(s1.begin(), s1.end(), [&amp;](char &amp;s)
	{ 
		// inc count
		auto it = ht.find(s);
		if (it != ht.end()) {
			ht[s]++;
		}
		else {
			ht.insert(std::make_pair(s, 1));
		}

		// dec count
		auto it2 = ht.find(*s_it);
		if (it2 != ht.end()) {
			ht[*s_it]--;
		}
		else {
			ht.insert(std::make_pair(*s_it, -1));
		}

		s_it++;
	});

	for_each(ht.begin(), ht.end(), [&amp;](std::pair&lt;const int, int&gt; &amp;p)
	{ 
		if(p.second != 0) {
			check = false;
		}
	});

	return check;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/321287/string-permutation</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Jul 2026 17:00:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/321287.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 30 Oct 2013 23:24:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to string permutation on Wed, 30 Oct 2013 23:24:32 GMT]]></title><description><![CDATA[<p>Hi,<br />
i want to check if one is a permutation of the other one...any comments to my approach?</p>
<pre><code>bool in_string_permutation(string &amp;s1, string &amp;s2) {
	std::unordered_map&lt;int,int&gt; ht;
	auto s_it = s2.begin();
	bool check = true;

	if(s1.length() != s2.length()) {
		return false;
	}

	for_each(s1.begin(), s1.end(), [&amp;](char &amp;s)
	{ 
		// inc count
		auto it = ht.find(s);
		if (it != ht.end()) {
			ht[s]++;
		}
		else {
			ht.insert(std::make_pair(s, 1));
		}

		// dec count
		auto it2 = ht.find(*s_it);
		if (it2 != ht.end()) {
			ht[*s_it]--;
		}
		else {
			ht.insert(std::make_pair(*s_it, -1));
		}

		s_it++;
	});

	for_each(ht.begin(), ht.end(), [&amp;](std::pair&lt;const int, int&gt; &amp;p)
	{ 
		if(p.second != 0) {
			check = false;
		}
	});

	return check;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2364028</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2364028</guid><dc:creator><![CDATA[jeff1]]></dc:creator><pubDate>Wed, 30 Oct 2013 23:24:32 GMT</pubDate></item><item><title><![CDATA[Reply to string permutation on Thu, 31 Oct 2013 07:36:11 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">bool in_string_permutation(string s1, string s2)
{
    sort(begin(s1), end(s1);
    sort(begin(s2), end(s2);
    return s1 == s2;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2364039</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2364039</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Thu, 31 Oct 2013 07:36:11 GMT</pubDate></item><item><title><![CDATA[Reply to string permutation on Thu, 31 Oct 2013 07:37:25 GMT]]></title><description><![CDATA[<p>Looks very complicated. You should either pass const string &amp; or string, because the function should not change the parameters passed.</p>
<pre><code>bool is_string_permutation(string s1, string s2){
    //maybe do a length check first
    sort(s1.begin(), s1.end());
    sort(s2.begin(), s2.end());
    return s1 == s2;
}
</code></pre>
<p>Different approach:</p>
<pre><code>bool is_string_permutation(const string s1, const string s2){
    //maybe do a length check first
    map&lt;char, size_t&gt; m1;
    for (const auto &amp;c : s1)
        m1[c]++;
    map&lt;char, size_t&gt; m2;
    for (const auto &amp;c : s2)
        m2[c]++;
    return m1 == m2;
}
</code></pre>
<p>//dammit, too late</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2364040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2364040</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Thu, 31 Oct 2013 07:37:25 GMT</pubDate></item></channel></rss>