String Matching in an Array โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโ€”or scroll down to preview all options.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

Understanding String Matching in an Array
Detailed explanation and reference materials
Problem Overview

String Matching in an Array โ€“ Explained with Intuition & Visualization

Difficulty: Easy
Topics: Strings, Brute Force, Searching


๐Ÿง  Problem Understanding (In Simple Words)

You are given a list of words.

๐Ÿ‘‰ Your task is to find: Which words are contained inside another word in the same list?

In other words:

  • If one word appears as a part of another word, include it in the result.

๐Ÿ’ก Intuition (How to Think About It)

Think of each word as a pattern.

Now ask: ๐Ÿ‘‰ "Does this pattern exist inside any other word?"

Example:

  • "as" is inside "mass"
  • "hero" is inside "superhero"

So both should be included.


๐Ÿ” Key Idea

For every word:

  • Compare it with all other words
  • Check: ๐Ÿ‘‰ Is this word a substring of any other word?

If yes โ†’ add it to result


๐Ÿš€ Approach (Step-by-Step)

  1. Create an empty result list
  2. For each word w1 in the array:
    • Compare it with every other word w2
    • If w1 is found inside w2:
      • Add w1 to result
      • Stop checking further for this word
  3. Return the result list

๐ŸŽฏ Visualization Using DrawToCode

This problem becomes much clearer when visualized:

  • You can compare two strings visually
  • Highlight matching parts (substrings)
  • See how smaller words fit inside larger ones

๐Ÿ‘‰ Using DrawToCode, you can:

  • Track substring matching step-by-step
  • Understand why some matches fail
  • Build intuition for string problems

๐Ÿ’ป Code (Java Example)

java
import java.util.*;

public class Main {
    public static List<String> stringMatching(String[] words) {
        List<String> result = new ArrayList<>();

        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j < words.length; j++) {
                if (i != j && words[j].contains(words[i])) {
                    result.add(words[i]);
                    break;
                }
            }
        }
        return result;
    }
}

โฑ๏ธ Complexity Analysis

  • Time Complexity: O(nยฒ * m)
    (n = number of words, m = average length of words)

  • Space Complexity: O(1) (excluding output list)


๐Ÿ” Example Walkthrough

Input:

words = ["mass", "as", "hero", "superhero"]

Execution:

  • Check "mass" โ†’ not inside any other word โŒ
  • Check "as" โ†’ found inside "mass" โœ…
  • Check "hero" โ†’ found inside "superhero" โœ…
  • Check "superhero" โ†’ not inside any other word โŒ

๐Ÿ‘‰ Output:

["as", "hero"]

Input:

words = ["blue", "green", "bu"]

Execution:

  • "blue" โ†’ no match
  • "green" โ†’ no match
  • "bu" โ†’ not found in any word

๐Ÿ‘‰ Output:

[]

๐Ÿงช Edge Cases to Consider

  • Words with same prefixes but not substrings
  • Very short strings
  • No matching substrings

๐Ÿ Final Thought

This problem helps you understand:

  • String comparison techniques
  • Substring detection
  • Brute-force pattern matching

๐Ÿ‘‰ Once you visualize string overlaps, these problems become much easier to solve

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
strings
Brute Force
searching-sorting
java
Reference Link
https://leetcode.com/problems/string-matching-in-an-array/?envType=daily-question&envId=2025-01-06

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly ๐Ÿงฉ

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

๐Ÿ”’ Locked steps cannot be moved. Only unlocked steps are draggable.

๐Ÿ”Š Enable sound for swap feedback and completion effects.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.