Longest Common Substring

Find the longest contiguous substring that appears in both strings using an iterative approach.

Code

Algorithms
longest=""
for ((i=0; i<${#a}; i++)); do
  for ((j=i+1; j<=${#a}; j++)); do
    current="${a:i:j-i}"
    if [[ "$b" == *"$current"* ]] && [ ${#current} -gt ${#longest} ]; then
      longest="$current"
    fi
  done
done
echo "$longest"

Parameters

First string

Second string

Server

More Bash Snippets