<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Trésor Bireke</title>
    <description>The latest articles on DEV Community by Trésor Bireke (@tresor11).</description>
    <link>https://dev.to/tresor11</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F312477%2F187c3342-4fa0-4b39-84d0-be9ad248a213.jpeg</url>
      <title>DEV Community: Trésor Bireke</title>
      <link>https://dev.to/tresor11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tresor11"/>
    <language>en</language>
    <item>
      <title>How to determine whether a given string can be rearranged into a palindrome?</title>
      <dc:creator>Trésor Bireke</dc:creator>
      <pubDate>Wed, 12 Feb 2020 00:07:19 +0000</pubDate>
      <link>https://dev.to/tresor11/how-to-determine-whether-a-given-string-can-be-rearranged-into-a-palindrome-4bie</link>
      <guid>https://dev.to/tresor11/how-to-determine-whether-a-given-string-can-be-rearranged-into-a-palindrome-4bie</guid>
      <description>&lt;p&gt;In this post, we'll create a simple method that checks whether a given string can be rearranged into a palindrome.&lt;br&gt;
but first, let's make sure we know what a palindrome is&lt;/p&gt;

&lt;p&gt;according to Wikipedia, A palindrome is a word, number, phrase, or another sequence of characters that reads the same backward as forward.&lt;/p&gt;

&lt;p&gt;example: madam, racecar...&lt;/p&gt;

&lt;p&gt;a simple method to check if a string is a palindrome using ruby would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;palindrome_check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;
 &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  constraints
&lt;/h2&gt;

&lt;p&gt;with the previous definition in mind, we can lets create the constraints for our method,&lt;br&gt;
A sting can be rearranged in a palindrome if:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It contains one or zero single character&lt;/strong&gt;: the reason is that if we have one single character it can be in the middle of our string and it won't affect the result&lt;/p&gt;

&lt;p&gt;example:&lt;/p&gt;

&lt;p&gt;rac_&lt;em&gt;e&lt;/em&gt;_car the &lt;strong&gt;e&lt;/strong&gt; is in the middle and thus it won't affect our result&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It contains one or zero characters with an odd number of occurrence in the string&lt;/strong&gt;: the reason is that the permuting our characters we need to have the same number of characters of both sides left and right and having more than one character with an odd number of occurrences would affect our solution while splitting the string&lt;/p&gt;

&lt;p&gt;examples:&lt;br&gt;
A string like &lt;strong&gt;aaabbbb&lt;/strong&gt; would be easily permuted to bb_&lt;em&gt;aaa&lt;/em&gt;_bb which returns true&lt;/p&gt;

&lt;p&gt;but with string like &lt;strong&gt;aaabbbbccc&lt;/strong&gt; it would return false because we could not have the occurrence characters on both sides&lt;/p&gt;

&lt;p&gt;with our contains being set let's write a simple ruby method that returns "YES" if a given string can be rearranged in a palindrome and  "NO" if it's not the case&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;palidrome_rearrange_check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;uniq&lt;/span&gt;
    &lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;odd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;odd&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;odd?&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"NO"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"NO"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;odd&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"YES"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that will be all for our method :-) if you have any question let me know &lt;/p&gt;

&lt;p&gt;in the comments&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
