<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Saiko Software</title>
	<atom:link href="https://saiko-software.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://saiko-software.com/</link>
	<description></description>
	<lastBuildDate>Wed, 16 Jul 2025 04:43:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://saiko-software.com/wp-content/uploads/2024/03/cropped-logo-black-p5-32x32.png</url>
	<title>Saiko Software</title>
	<link>https://saiko-software.com/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Software Developer (NodeJS) &#8211; Job Opportunity at Saiko</title>
		<link>https://saiko-software.com/software-developer-nodejs-job-opportunity-at-saiko/</link>
					<comments>https://saiko-software.com/software-developer-nodejs-job-opportunity-at-saiko/#respond</comments>
		
		<dc:creator><![CDATA[admin-saiko]]></dc:creator>
		<pubDate>Tue, 15 Jul 2025 08:49:55 +0000</pubDate>
				<category><![CDATA[Hiring]]></category>
		<guid isPermaLink="false">https://saiko-software.com/?p=996</guid>

					<description><![CDATA[<p>Job Brief We are seeking talented NodeJS Developers to join our dynamic team at Saiko, a software company specializing in outsourcing for the Japanese and US markets. As a team member, you’ll work on both server-side and frontend aspects of our web applications, contributing to their full lifecycle. If you’re passionate about coding, problem-solving, and [&#8230;]</p>
<p>The post <a href="https://saiko-software.com/software-developer-nodejs-job-opportunity-at-saiko/">Software Developer (NodeJS) &#8211; Job Opportunity at Saiko</a> appeared first on <a href="https://saiko-software.com">Saiko Software</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h3 class="wp-block-heading"><strong>Job Brief</strong></h3>



<p>We are seeking talented <strong>NodeJS Developers</strong> to join our dynamic team at <strong>Saiko</strong>, a software company specializing in outsourcing for the Japanese and US markets. As a team member, you’ll work on both <strong>server-side</strong> and <strong>frontend</strong> aspects of our web applications, contributing to their full lifecycle. If you’re passionate about coding, problem-solving, and building innovative applications, we want to meet you!</p>



<p><strong>Salary</strong>: Up to 25M VND (gross), based on skills and experience.</p>



<h3 class="wp-block-heading"><strong>About Saiko</strong></h3>



<p>Saiko is a small, vibrant team dedicated to long-term collaboration. We focus on delivering high-quality software solutions for global clients, particularly in Japan and the US. At Saiko, we value relationships and aim to grow together with our team members.</p>



<h3 class="wp-block-heading"><strong>Responsibilities</strong></h3>



<ul class="wp-block-list">
<li>Contribute to the entire application lifecycle, focusing on coding and debugging.</li>



<li>Write clean, functional code for web applications.</li>



<li>Troubleshoot and debug to ensure smooth performance.</li>



<li>Conduct UI tests to optimize application performance.</li>



<li>Leverage cutting-edge technologies to enhance legacy applications.</li>



<li>Collaborate with clients to improve application quality.</li>



<li>Build reusable code and libraries for future use.</li>
</ul>



<h3 class="wp-block-heading"><strong>Requirements &amp; Skills</strong></h3>



<ul class="wp-block-list">
<li><strong>1-5 years</strong> of experience in software development with <strong>NodeJS</strong>.</li>



<li>Experience with <strong>ExpressJS</strong> or <strong>NestJS</strong>.</li>



<li>Experience with <strong>ReactJS</strong> or <strong>VueJS</strong>.</li>



<li>Previous experience as a team/technical leader is a plus.</li>



<li>Strong problem-solving skills and a positive mindset.</li>



<li><strong>Good English skills</strong> for client communication (written) and research.</li>



<li>Knowledge of databases (<strong>MongoDB</strong>, <strong>PostgreSQL</strong>, or <strong>MySQL</strong>).</li>



<li>Proficient with code versioning tools (<strong>Git</strong> preferred).</li>



<li>Bonus: Open-source <strong>GitHub</strong> repositories, <strong>LeetCode</strong> profile, or similar.</li>
</ul>



<h3 class="wp-block-heading"><strong>Benefits</strong></h3>



<ul class="wp-block-list">
<li>Competitive salary: <strong>10M &#8211; 25M VND (gross)</strong>, based on ability and position.</li>



<li>13th-month salary and holiday bonuses (Lunar New Year, etc.).</li>



<li><strong>Work schedule</strong>: Monday to Friday, full weekend off (Saturday &amp; Sunday).</li>



<li>Full social, health, and unemployment insurance as per labor law.</li>



<li>Training and mentorship by senior developers, if needed.</li>



<li>Annual company trips, sick leave, maternity leave, and more.</li>
</ul>



<h3 class="wp-block-heading"><strong>Interview Process</strong></h3>



<ol class="wp-block-list">
<li><strong>Round 1</strong>: 30-minute to 1-hour technical test.</li>



<li><strong>Round 2</strong>: Interview with our team.</li>
</ol>



<h3 class="wp-block-heading"><strong>How to Apply</strong></h3>



<p>Ready to join our journey? Send your CV, GitHub/LeetCode profiles, and a brief introduction to hr@saiko-software.com. Let’s build something amazing together at Saiko!</p>
<p>The post <a href="https://saiko-software.com/software-developer-nodejs-job-opportunity-at-saiko/">Software Developer (NodeJS) &#8211; Job Opportunity at Saiko</a> appeared first on <a href="https://saiko-software.com">Saiko Software</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saiko-software.com/software-developer-nodejs-job-opportunity-at-saiko/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Algorithm: Longest common prefix</title>
		<link>https://saiko-software.com/algorithm-longest-common-prefix/</link>
					<comments>https://saiko-software.com/algorithm-longest-common-prefix/#respond</comments>
		
		<dc:creator><![CDATA[admin-saiko]]></dc:creator>
		<pubDate>Wed, 02 Apr 2025 04:44:57 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[leetcode]]></category>
		<guid isPermaLink="false">https://saiko-software.com/?p=988</guid>

					<description><![CDATA[<p>Today we will look at an easy problem: finding the longest common prefix of an array of strings. Could you find the optimized solution for it?</p>
<p>The post <a href="https://saiko-software.com/algorithm-longest-common-prefix/">Algorithm: Longest common prefix</a> appeared first on <a href="https://saiko-software.com">Saiko Software</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Problem</h2>



<p>Write a function to find the longest common prefix string amongst an array of strings. <br>If there is no common prefix, return an empty string&nbsp;<code>""</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted has-ast-global-color-2-color has-ast-global-color-5-background-color has-text-color has-background has-link-color wp-elements-c0d94e01c8ac8cce8c02e4a75c10a6f1"><strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted has-ast-global-color-2-color has-ast-global-color-5-background-color has-text-color has-background has-link-color wp-elements-a8884e3464e9ffe0d50ccf12e188009f"><strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.</pre>



<h2 class="wp-block-heading">Solution</h2>



<h3 class="wp-block-heading">Brute force</h3>



<p>This is an easy problem. First we will look at the brute force solutiton:<br>– Loop through the first string in the array.<br>– Loop through other strings in the array and compare the characters in the same position.<br>– if the characters are not matched –&gt; break through the loop.<br>– if the characters are matched, add it to the result.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="const solution1 = function(strs) {
    let result = &quot;&quot;;
    let baseStr = strs[0];
    if (strs.length === 1) return baseStr;
    for (let i = 0; i &lt; baseStr.length; i++) {
        let matched = true;
        for (let j = 1; j &lt; strs.length; j++) {
            if (strs[j][i] !== baseStr[i]) {
                matched = false;
                break;
            }
        }

        if (matched) {
            result += baseStr[i];
        } else {
            break;
        }
    }
    return result;
};" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">const</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">solution1</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">function</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">strs</span><span style="color: #ECEFF4">)</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&quot;&quot;</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">baseStr</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">strs</span><span style="color: #D8DEE9FF">[</span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">]</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">strs</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">length </span><span style="color: #81A1C1">===</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF">) </span><span style="color: #81A1C1">return</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">baseStr</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">baseStr</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">length</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #81A1C1">++</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">matched</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">true;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">j</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">j</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">strs</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">length</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">j</span><span style="color: #81A1C1">++</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">strs</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">j</span><span style="color: #D8DEE9FF">][</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">] </span><span style="color: #81A1C1">!==</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">baseStr</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">]) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">matched</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">false;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">break;</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">matched</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">baseStr</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">]</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">else</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">break;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">return</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #81A1C1">;</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">Optimized solution</h3>



<p>To improve the speed, we need to reduce the number of loop. Instead of comparing the string one by one, if we sort them first, we only need to compare the first and the last string because they will be the most different.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="const solution2 = function(strs) {
    strs = strs.sort();
    let result = &quot;&quot;;
    let first = strs[0];
    let last = strs[strs.length - 1];

    for (let i = 0; i &lt; first.length; i++) {
        if (first[i] === last[i]) {
            result += first[i];
        } else {
            break;
        }
    }
    return result;
}" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">const</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">solution2</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">function</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">strs</span><span style="color: #ECEFF4">)</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">strs</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">strs</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">sort</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&quot;&quot;</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">first</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">strs</span><span style="color: #D8DEE9FF">[</span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">]</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">last</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">strs</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">strs</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">length </span><span style="color: #81A1C1">-</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF">]</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">first</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">length</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #81A1C1">++</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">first</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">] </span><span style="color: #81A1C1">===</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">last</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">]) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">first</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">]</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">else</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">break;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">return</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span></code></pre></div>



<p></p>
<p>The post <a href="https://saiko-software.com/algorithm-longest-common-prefix/">Algorithm: Longest common prefix</a> appeared first on <a href="https://saiko-software.com">Saiko Software</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saiko-software.com/algorithm-longest-common-prefix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Algorithm: Roman to Integer</title>
		<link>https://saiko-software.com/algorithm-roman-to-integer/</link>
					<comments>https://saiko-software.com/algorithm-roman-to-integer/#respond</comments>
		
		<dc:creator><![CDATA[admin-saiko]]></dc:creator>
		<pubDate>Tue, 01 Apr 2025 03:32:13 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[leetcode]]></category>
		<guid isPermaLink="false">https://saiko-software.com/?p=960</guid>

					<description><![CDATA[<p>Today we will look at an easy algorithm problem: Converting a Roman number string to Integer.</p>
<p>The post <a href="https://saiko-software.com/algorithm-roman-to-integer/">Algorithm: Roman to Integer</a> appeared first on <a href="https://saiko-software.com">Saiko Software</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Problem</h2>



<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>,&nbsp;<code>V</code>,&nbsp;<code>X</code>,&nbsp;<code>L</code>,&nbsp;<code>C</code>,&nbsp;<code>D</code>&nbsp;and&nbsp;<code>M</code>.</p>



<p>For example,&nbsp;<code>2</code>&nbsp;is written as&nbsp;<code>II</code>&nbsp;in Roman numeral, just two ones added together.&nbsp;<code>12</code>&nbsp;is written as&nbsp;<code>XII</code>, which is simply&nbsp;<code>X + II</code>. The number&nbsp;<code>27</code>&nbsp;is written as&nbsp;<code>XXVII</code>, which is&nbsp;<code>XX + V + II</code>.</p>



<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not&nbsp;<code>IIII</code>. Instead, the number four is written as&nbsp;<code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as&nbsp;<code>IX</code>. There are six instances where subtraction is used:</p>



<ul class="wp-block-list">
<li><code>I</code>&nbsp;can be placed before&nbsp;<code>V</code>&nbsp;(5) and&nbsp;<code>X</code>&nbsp;(10) to make 4 and 9.&nbsp;</li>



<li><code>X</code>&nbsp;can be placed before&nbsp;<code>L</code>&nbsp;(50) and&nbsp;<code>C</code>&nbsp;(100) to make 40 and 90.&nbsp;</li>



<li><code>C</code>&nbsp;can be placed before&nbsp;<code>D</code>&nbsp;(500) and&nbsp;<code>M</code>&nbsp;(1000) to make 400 and 900.</li>
</ul>



<p>Given a roman numeral, convert it to an integer.</p>



<p><strong>Example:</strong></p>



<pre class="wp-block-preformatted has-ast-global-color-2-color has-ast-global-color-4-background-color has-text-color has-background has-link-color wp-elements-f34152e15a03c5ac079ee889bab80e75"><strong>Input:</strong> s = "MCMXCIV"<br><strong>Output:</strong> 1994<br><strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.</pre>



<h2 class="wp-block-heading" id="intuition">Intuition</h2>



<p>Roman numerals use specific rules:</p>



<ul class="wp-block-list">
<li>If a smaller numeral comes before a larger one, it is subtracted (e.g., <code>IV = 4</code>).</li>



<li>Otherwise, the values are added (e.g., <code>VI = 6</code>).<br>We can traverse the string, compare each numeral with the next, and decide whether to add or subtract.</li>
</ul>



<h2 class="wp-block-heading" id="approach">Approach</h2>



<ol class="wp-block-list">
<li>Create a helper function to map Roman numerals (<code>I</code>, <code>V</code>, <code>X</code>, etc.) to their integer values.</li>



<li>Initialize a variable <code>result</code> to store the final integer value.</li>



<li>Loop through the string:
<ul class="wp-block-list">
<li>If the current numeral is smaller than the next numeral, subtract its value from <code>result</code>.</li>



<li>Otherwise, add its value to <code>result</code>.</li>
</ul>
</li>



<li>Return the final <code>result</code>.</li>
</ol>



<h2 class="wp-block-heading" id="complexity">Complexity</h2>



<ul class="wp-block-list">
<li><strong>Time Complexity</strong>: <code>O(n)</code><br>The string is traversed once, where <code>n</code> is its length.</li>



<li><strong>Space Complexity</strong>: <code>O(1)</code><br>Only a few variables are used; no additional data structures.</li>
</ul>



<h2 class="wp-block-heading">Code</h2>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
    const romanToInt = {
        'I': 1, 'V': 5, 'X': 10, 'L': 50,
        'C': 100, 'D': 500, 'M': 1000
    };
    let result = 0;
    for (let i = 0; i &lt; s.length; i++) {
        if (i + 1 &lt; s.length &amp;&amp; romanToInt[s[i]] &lt; romanToInt[s[i + 1]]) {
            result -= romanToInt[s[i]];
        } else {
            result += romanToInt[s[i]];
        }
    }
    return result;
};" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #616E88">/**</span></span>
<span class="line"><span style="color: #616E88"> * </span><span style="color: #ECEFF4">@</span><span style="color: #8FBCBB">param</span><span style="color: #616E88"> </span><span style="color: #ECEFF4">{</span><span style="color: #616E88">string</span><span style="color: #ECEFF4">}</span><span style="color: #616E88"> </span><span style="color: #D8DEE9">s</span></span>
<span class="line"><span style="color: #616E88"> * </span><span style="color: #ECEFF4">@</span><span style="color: #8FBCBB">return</span><span style="color: #616E88"> </span><span style="color: #ECEFF4">{</span><span style="color: #616E88">number</span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #616E88"> */</span></span>
<span class="line"><span style="color: #81A1C1">var</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">romanToInt</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">function</span><span style="color: #ECEFF4">(</span><span style="color: #D8DEE9">s</span><span style="color: #ECEFF4">)</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">const</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">romanToInt</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">I</span><span style="color: #ECEFF4">&#39;</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">V</span><span style="color: #ECEFF4">&#39;</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">5</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">X</span><span style="color: #ECEFF4">&#39;</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">10</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">L</span><span style="color: #ECEFF4">&#39;</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">50</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">C</span><span style="color: #ECEFF4">&#39;</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">100</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">D</span><span style="color: #ECEFF4">&#39;</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">500</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&#39;</span><span style="color: #A3BE8C">M</span><span style="color: #ECEFF4">&#39;</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1000</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">s</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">length</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">i</span><span style="color: #81A1C1">++</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">s</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">length </span><span style="color: #81A1C1">&amp;&amp;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">romanToInt</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">s</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">]] </span><span style="color: #81A1C1">&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">romanToInt</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">s</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF">]]) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">-=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">romanToInt</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">s</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">]]</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">else</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">romanToInt</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">s</span><span style="color: #D8DEE9FF">[</span><span style="color: #D8DEE9">i</span><span style="color: #D8DEE9FF">]]</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">return</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #81A1C1">;</span></span></code></pre></div>



<p></p>
<p>The post <a href="https://saiko-software.com/algorithm-roman-to-integer/">Algorithm: Roman to Integer</a> appeared first on <a href="https://saiko-software.com">Saiko Software</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saiko-software.com/algorithm-roman-to-integer/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
