Broad Network


4.1 Check if two strings are the same in Go

4. Basic String Algorithm Problems in Go

Full Course on Data Structures and Algorithms in Go

By: Chrysanthus Date Published: 3 Feb 2026

The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.

Problem

Given two strings, s1 and s2, check if the two strings are identical(same) or not. If the strings are identical, print "Yes", otherwise, print "No". Consider case sensitivity. Employ O(n) time and O(n) space.

Examples:

Input: s1 = "abcd", s2 = "abcd" 
Output: Yes 

Input: s1 = "", s2 = "" 
Output: Yes 

Input: s1 = "Broad-network.com", s2 = "moc.krowten-daorB" 
Output: No 

Input: s1 = "Broad", s2 = "Broad-network.com" 
Output: No 

Note:

Two empty strings are the same. An empty string should not have the space character. If the same text are in both strings and one is not in the same order as the other one, then both strings are not the same. The strings must be the same in number of characters, order of characters, and same casing for both strings.

Strategy

Compare the lengths of the strings, the characters and their order, and their casings.

Note: do not compare the variables of the two strings. Comparing their variables means comparing their references or pointers, which will hardly be the same.

Also note that since the two different cases (upper and lower) of the same character, are represented by different numbers in the computer, the program does not need extra coding to check the casing.

Program

The following program does the comparison (read through the code and comments):

package main

import (
	"fmt"
)

    func areStringsSame(sx string, sy string) bool {
        //length of strings
        var Nx = len(sx);
        var Ny = len(sy);
    
        // Compare lengths first
        if (Nx != Ny) {
            return false;
        }

        // Compare character by character
        var shorterLength = 0;
        if (Nx < Ny) {
            shorterLength = Nx;
        } else {
            shorterLength = Ny;
        }
        
        for i := 0; i < shorterLength; i++ {    //avoid accessing out of bounds
            if (sx[i] != sy[i]) {
                return false;
            }
        }

        return true;    //in case the strings are the same
    }

func main() {
    var s1 = "Broad-network.com";
    var s2 = "Broad-network.com";
    //call function and print answer
    if (areStringsSame(s1, s2) == true) {
        fmt.Println("Yes");
    } else {
        fmt.Println("No");
    }
    
    var sa = "Broad";
    var sb = "Broad-network.com";
    //call function and print answer
    if (areStringsSame(sa, sb) == true) {
        fmt.Println("Yes");
    } else {
        fmt.Println("No");
    }
}

The output is:

    Yes
    No

The time complexity is actually O(2N) for the input and function for-loops; and the space complexity is actually O(2N) for the two strings. Since the coefficient (multiplier) is normally omitted, either time complexity is given by O(N).

The time and space complexities for the actual comparing operations of the lengths and characters, of the two strings, and possible temporary variables, are ignored.

Thanks for reading.





Related Links

More Related Links

Cousins

BACK NEXT

Comments


Note: You can use the Search Box above to find articles and discussions of interest.