<?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: Sona Mittal</title>
    <description>The latest articles on DEV Community by Sona Mittal (@sonamittal7).</description>
    <link>https://dev.to/sonamittal7</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F916040%2Faf4db8bf-0b14-4b06-8a15-5c382f8f9753.jpeg</url>
      <title>DEV Community: Sona Mittal</title>
      <link>https://dev.to/sonamittal7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sonamittal7"/>
    <language>en</language>
    <item>
      <title>LeetCode #14: Longest Common Prefix in Java | Simple Vertical Scanning Approach</title>
      <dc:creator>Sona Mittal</dc:creator>
      <pubDate>Thu, 20 Aug 2026 07:57:53 +0000</pubDate>
      <link>https://dev.to/sonamittal7/understanding-longest-common-prefix-with-a-simple-approach-jp8</link>
      <guid>https://dev.to/sonamittal7/understanding-longest-common-prefix-with-a-simple-approach-jp8</guid>
      <description>&lt;p&gt;Given an array of strings, find the longest common prefix among all the strings.&lt;br&gt;
If there is no common prefix, return an empty string "".&lt;br&gt;
Example:&lt;/p&gt;

&lt;p&gt;Input: ["flower", "flow", "flight"]&lt;br&gt;
Output: "fl"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftvytjadyqyrvd1tje3bm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftvytjadyqyrvd1tje3bm.png" alt=" " width="800" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello everyone! 👋&lt;/p&gt;

&lt;p&gt;Let's solve the Longest Common Prefix problem.&lt;/p&gt;

&lt;p&gt;Before solving the problem, we should understand it first. There are multiple ways to solve this problem. Some approaches are :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Vertical scanning &lt;/li&gt;
&lt;li&gt;Trie-based approach&lt;/li&gt;
&lt;li&gt;Horizontal scanning&lt;/li&gt;
&lt;li&gt;Divide and conquer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For this problem, I will use the vertical Scanning approach because it is easier to understand and implement.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is vertical scanning?
&lt;/h2&gt;

&lt;p&gt;Vertical scanning means comparing the character/element at the same &lt;br&gt;
   index position across multiple strings.&lt;/p&gt;

&lt;p&gt;String 1: C A R&lt;br&gt;
String 2: C A T&lt;br&gt;
String 3: C A N&lt;/p&gt;

&lt;p&gt;Now compare the characters vertically.&lt;/p&gt;

&lt;p&gt;Index 0 → C, C, C&lt;br&gt;
Index 1 → A, A, A&lt;br&gt;
Index 2 → R, T, N&lt;/p&gt;

&lt;p&gt;At index 0:&lt;/p&gt;

&lt;p&gt;C = C = C ✓&lt;/p&gt;

&lt;p&gt;At index 1:&lt;/p&gt;

&lt;p&gt;A = A = A ✓&lt;/p&gt;

&lt;p&gt;At index 2:&lt;/p&gt;

&lt;p&gt;R ≠ T ≠ N ✗&lt;/p&gt;

&lt;p&gt;So we stop at index 2.&lt;/p&gt;

&lt;p&gt;The common prefix is:&lt;/p&gt;

&lt;p&gt;"CA"&lt;/p&gt;

&lt;p&gt;This is why it is called vertical scanning — we compare characters at the same index across different strings.&lt;/p&gt;
&lt;h4&gt;
  
  
  - Let's Understand the Algorithm
&lt;/h4&gt;

&lt;p&gt;Now let's understand the algorithm step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Take the first string as a reference &lt;/li&gt;
&lt;li&gt;Traverse the reference string character by character.&lt;/li&gt;
&lt;li&gt;Compare the current character with the character at the same index in all remaining strings.&lt;/li&gt;
&lt;li&gt;If any character does not match, stop comparing.&lt;/li&gt;
&lt;li&gt;If we reach the end of any string, stop comparing.&lt;/li&gt;
&lt;li&gt;If all characters match, continue to the next index.&lt;/li&gt;
&lt;li&gt;Repeat this process until a mismatch occurs or the end of a string is reached.&lt;/li&gt;
&lt;li&gt;Return the characters matched so far as the longest common prefix.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;br&gt;
Suppose we have:&lt;br&gt;
["flower", "flow", "flight"]&lt;/p&gt;

&lt;p&gt;Index 0 → f = f = f ✓&lt;br&gt;
Index 1 → l = l = l ✓&lt;br&gt;
Index 2 → o = o = i ✗&lt;br&gt;
At index 2, the characters do not match, so we stop.&lt;br&gt;
Therefore, the longest common prefix is: "fl"&lt;/p&gt;

&lt;p&gt;Java Implementation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;longestCommonPrefix&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;strs&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strs&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;strs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strs&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;firstS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;strs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;strs&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                        &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;strs&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;firstS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;firstS&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;strs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"flower"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"flow"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"flight"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;longestCommonPrefix&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strs&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding the Code &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;
&lt;/h2&gt;

&lt;p&gt;Now let's understand the code line by line.&lt;br&gt;
&lt;strong&gt;1. Check for an Empty Input&lt;/strong&gt;&lt;br&gt;
if (strs == null || strs.length == 0) { return ""; }&lt;/p&gt;

&lt;p&gt;First, we check whether the input array is:&lt;/p&gt;

&lt;p&gt;null&lt;br&gt;
empty&lt;/p&gt;

&lt;p&gt;If there are no strings to compare, there cannot be a common prefix.&lt;/p&gt;

&lt;p&gt;Therefore, we return: ""&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Take the First String as Reference&lt;/strong&gt;&lt;br&gt;
String firstS = strs[0];&lt;/p&gt;

&lt;p&gt;Here, we use the first string as our reference.&lt;/p&gt;

&lt;p&gt;For:&lt;/p&gt;

&lt;p&gt;["flower", "flow", "flight"]&lt;/p&gt;

&lt;p&gt;we get:&lt;/p&gt;

&lt;p&gt;firstS = "flower"&lt;/p&gt;

&lt;p&gt;We will use "flower" to decide which character we are currently checking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Traverse the Reference String&lt;/strong&gt;&lt;br&gt;
for (int i = 0; i &amp;lt; firstS.length(); i++) {&lt;/p&gt;

&lt;p&gt;This loop goes through the reference string character by character.&lt;/p&gt;

&lt;p&gt;For "flower":&lt;/p&gt;

&lt;p&gt;Index:  0 1 2 3 4 5&lt;br&gt;
String: f l o w e r&lt;/p&gt;

&lt;p&gt;So i will be:&lt;/p&gt;

&lt;p&gt;0 → 1 → 2 → 3 → 4 → 5&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Get the Current Character&lt;/strong&gt;&lt;br&gt;
char ch = firstS.charAt(i);&lt;/p&gt;

&lt;p&gt;At every index, we get the current character from the reference string.&lt;/p&gt;

&lt;p&gt;For example, when:&lt;/p&gt;

&lt;p&gt;i = 0&lt;/p&gt;

&lt;p&gt;we get:&lt;/p&gt;

&lt;p&gt;ch = 'f'&lt;/p&gt;

&lt;p&gt;When:&lt;/p&gt;

&lt;p&gt;i = 1&lt;/p&gt;

&lt;p&gt;we get:&lt;/p&gt;

&lt;p&gt;ch = 'l'&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Compare With the Remaining Strings&lt;/strong&gt;&lt;br&gt;
for (int j = 1; j &amp;lt; strs.length; j++) {&lt;/p&gt;

&lt;p&gt;We start j from 1 because index 0 is already our reference string.&lt;/p&gt;

&lt;p&gt;For:&lt;/p&gt;

&lt;p&gt;["flower", "flow", "flight"]&lt;/p&gt;

&lt;p&gt;we compare with:&lt;/p&gt;

&lt;p&gt;strs[1] → "flow"&lt;br&gt;
strs[2] → "flight"&lt;/p&gt;

&lt;p&gt;The first condition checks whether we have reached the end of the current string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Check Two Conditions&lt;/strong&gt;&lt;br&gt;
if (i &amp;gt;= strs[j].length()&lt;br&gt;
        || strs[j].charAt(i) != ch) {&lt;/p&gt;

&lt;p&gt;There are two important conditions here.&lt;/p&gt;

&lt;p&gt;Condition 1: String Has Ended&lt;br&gt;
i &amp;gt;= strs[j].length()&lt;/p&gt;

&lt;p&gt;This checks whether we have reached the end of the current string.&lt;/p&gt;

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

&lt;p&gt;"flow"&lt;br&gt;
0123&lt;/p&gt;

&lt;p&gt;If:&lt;/p&gt;

&lt;p&gt;i = 4&lt;/p&gt;

&lt;p&gt;then there is no character at index 4.&lt;/p&gt;

&lt;p&gt;So we stop.&lt;/p&gt;

&lt;p&gt;Condition 2: Character Does Not Match&lt;br&gt;
strs[j].charAt(i) != ch&lt;/p&gt;

&lt;p&gt;This checks whether the character at the same index is different.&lt;/p&gt;

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

&lt;p&gt;flower → o&lt;br&gt;
flow   → o&lt;br&gt;
flight → i&lt;/p&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;p&gt;'o' != 'i'&lt;/p&gt;

&lt;p&gt;So the condition becomes true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Return the Common Prefix&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If either condition is true:&lt;/p&gt;

&lt;p&gt;return firstS.substring(0, i);&lt;/p&gt;

&lt;p&gt;we return the characters matched before index i.&lt;/p&gt;

&lt;p&gt;For example, if the mismatch occurs at index 2:&lt;/p&gt;

&lt;p&gt;flower&lt;br&gt;
012345&lt;br&gt;
  ↑&lt;br&gt;
index 2&lt;/p&gt;

&lt;p&gt;We call:&lt;/p&gt;

&lt;p&gt;firstS.substring(0, 2)&lt;/p&gt;

&lt;p&gt;In Java, the second index is exclusive.&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;p&gt;substring(0, 2)&lt;/p&gt;

&lt;p&gt;returns:&lt;/p&gt;

&lt;p&gt;"fl"&lt;/p&gt;

&lt;p&gt;It includes:&lt;/p&gt;

&lt;p&gt;index 0 → f&lt;br&gt;
index 1 → l&lt;/p&gt;

&lt;p&gt;but does not include:&lt;/p&gt;

&lt;p&gt;index 2 → o&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;p&gt;"fl"&lt;/p&gt;

&lt;p&gt;is our longest common prefix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Complexity
&lt;/h2&gt;

&lt;p&gt;Let's understand the time complexity.&lt;br&gt;
n = number of strings&lt;br&gt;
m = length of the reference string&lt;br&gt;
In the worst case, we may check m characters across n strings.&lt;br&gt;
So the time complexity is:&lt;br&gt;
&lt;strong&gt;O(n × m)&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Space Complexity
&lt;/h2&gt;

&lt;p&gt;The space complexity is: O(1). Why?&lt;br&gt;
Because we are not creating an extra array or storing all the characters being compared.&lt;br&gt;
We only use a few variables such as: &lt;br&gt;
String firstS&lt;br&gt;
char ch&lt;br&gt;
int i&lt;br&gt;
int j&lt;br&gt;
The input strings are already given to us, so we don't count them as extra space.&lt;/p&gt;

&lt;p&gt;Therefore:&lt;br&gt;
Space Complexity: O(1)&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
