Skip to main content

Check if two strings differ by one character

Check if two strings differ by single character in java

/*
    Java program for
    Check if two strings differ by one character
*/
public class Difference
{
	// Function which is returns the imbalance character status
	public int skipByOneChar(String first, 
                             String second, int f1, int f2, int n)
	{
		int imbalance = 0;
		for (int i = 0; i < n && imbalance <= 1; ++i)
		{
			if (first.charAt(f1 + i) != second.charAt(f2 + i))
			{
				imbalance++;
			}
		}
		return imbalance;
	}
	public void differBySingleChar(String first, String second)
	{
		// Get the size of given string 
		int n = first.length();
		int m = second.length();
		int imbalance = 0;
		if (n == m)
		{
			// Simple case when both string are same length
			imbalance = skipByOneChar(first, second, 0, 0, n);
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				// Case 2
				// Check if, last character is not match in second string
				imbalance = skipByOneChar(first, second, 0, 0, n - 1);
				if (imbalance != 0)
				{
					// When case 2 fail
					// Check if, first character is not match in second string
					imbalance = skipByOneChar(first, second, 1, 0, n - 1);
				}
				if (imbalance == 0)
				{
					// Means last character is not match in second string or
					// Means first character is not match in second string 
					// Only one character are different
					imbalance = 1;
				}
				else
				{
					// When More than one character are different
					imbalance = 2;
				}
			}
			else
			{
				// Case 3
				// Check if, last character is not match in first string
				imbalance = skipByOneChar(first, second, 0, 0, m - 1);
				if (imbalance != 0)
				{
					// When Case 3 fail
					// Check if, first character is not match in first string
					imbalance = skipByOneChar(first, second, 0, 1, m - 1);
				}
				if (imbalance == 0)
				{
					// Means last character is not match in first string or
					// Means first character is not match in first string 
					imbalance = 1;
				}
				else
				{
					// When More than one character are different
					imbalance = 2;
				}
			}
		}
		System.out.print("\n First : " + first + ", Second : " + second);
		if (imbalance == 1)
		{
			System.out.println("\n Yes ");
		}
		else
		{
			System.out.println("\n No ");
		}
	}
	public static void main(String[] args)
	{
		Difference task = new Difference();
		// Test Cases
		task.differBySingleChar("code", "codes");
		task.differBySingleChar("man", "woman");
		task.differBySingleChar("ok", "kok");
      	task.differBySingleChar("kok", "oK");
		task.differBySingleChar("code", "scodz");
		// Case sensitive
		task.differBySingleChar("Apple", "apple");
		task.differBySingleChar("mango", "mango");
	}
}
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in c++

// Include header file
#include <iostream>
#include <string>
//  Stdc++11 program for
//  Check if two strings differ by one character
class Difference
{
    // Function which is returns the imbalance character status
    public:
    int skipByOneChar(std::string first, 
                      std::string second, int f1, int f2, int n)
    {
        int imbalance = 0;
        for (int i = 0; i < n && imbalance <= 1; ++i)
        {
            if (first[f1 + i] != second[f2 + i])
            {
                imbalance++;
            }
        }
        return imbalance;
    }
    void differBySingleChar(std::string first, std::string second)
    {
        // Get the size of given string 
        int n = first.length();
        int m = second.length();
        int imbalance = 0;
        if (n == m)
        {
            // Simple case when both string are same length
            imbalance = skipByOneChar(first, second, 0, 0, n);
        }
        else if (n - m == 1 || m - n == 1)
        {
            // When one string contains extra character
            if (n > m)
            {
                // Case 2
                // Check if, last character is not match in second string
                imbalance = skipByOneChar(first, second, 0, 0, n - 1);
                if (imbalance != 0)
                {
                    // When case 2 fail
                    // Check if, first character is not match in second string
                    imbalance = skipByOneChar(first, second, 1, 0, n - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in second string or
                    // Means first character is not match in second string
                    // Only one character are different
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
            else 
            {
                // Case 3
                // Check if, last character is not match in first string
                imbalance = skipByOneChar(first, second, 0, 0, m - 1);
                if (imbalance != 0)
                {
                    // When Case 3 fail
                    // Check if, first character is not match in first string
                    imbalance = skipByOneChar(first, second, 0, 1, m - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in first string or
                    // Means first character is not match in first string 
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
        }
        std::cout << "\n First : " + first + ", Second : " + second;
        if (imbalance == 1)
        {
            std::cout << "\n Yes " << std::endl;
        }
        else 
        {
            std::cout << "\n No " << std::endl;
        }
    }
};
int main(int argc, char **argv){
    Difference* task = new Difference();
    // Test Cases
    task->differBySingleChar("code", "codes");
    task->differBySingleChar("man", "woman");
    task->differBySingleChar("ok", "kok");
    task->differBySingleChar("kok", "oK");
    task->differBySingleChar("code", "scodz");
    // Case sensitive
    task->differBySingleChar("Apple", "apple");
    task->differBySingleChar("mango", "mango");
	return 0;
};
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in c

// Include header file
#include <stdio.h>
#include <string.h>
//  C program for
//  Check if two strings differ by one character

// Function which is returns the imbalance character status
int skipByOneChar (const char *first , const char *second, int f1, int f2, int n)
{
    int imbalance = 0;
    for (int i = 0; i < n && imbalance <= 1; i++)
    {
        if (first[f1 + i] != second[second,f2 + i])
        {
            imbalance++;
        }
    }
    return imbalance;
}
void  differBySingleChar (const char *first , const char *second)
{
    // Get the size of given string 
    int n = strlen(first);
    int m = strlen(second);
    int imbalance = 0;
    if (n == m)
    {
        // Simple case when both string are same length
        imbalance = skipByOneChar(first, second, 0, 0, n);
    }
    else
    if (n - m == 1 || m - n == 1)
    {
        // When one string contains extra character
        if (n > m)
        {
            // Case 2
            // Check if, last character is not match in second string
            imbalance = skipByOneChar(first, second, 0, 0, n - 1);
            if (imbalance != 0)
            {
                // When case 2 fail
                // Check if, first character is not match in second string
                imbalance = skipByOneChar(first, second, 1, 0, n - 1);
            }
            if (imbalance == 0)
            {
                // Means last character is not match in second string or
                // Means first character is not match in second string
                // Only one character are different
                imbalance = 1;
            }
            else
            {
                // When More than one character are different
                imbalance = 2;
            }
        }
        else
        {
            // Case 3
            // Check if, last character is not match in first string
            imbalance = skipByOneChar(first, second, 0, 0, m - 1);
            if (imbalance != 0)
            {
                // When Case 3 fail
                // Check if, first character is not match in first string
                imbalance = skipByOneChar(first, second, 0, 1, m - 1);
            }
            if (imbalance == 0)
            {
                // Means last character is not match in first string or
                // Means first character is not match in first string 
                imbalance = 1;
            }
            else
            {
                // When More than one character are different
                imbalance = 2;
            }
        }
    }
    printf("\n First : %s, Second : %s" , first  , second);
    if (imbalance == 1)
    {
        printf("\n Yes \n");
    }
    else
    {
        printf("\n No \n");
    }
}

int main()
{
  
    // Test Cases
    differBySingleChar("code", "codes");
    differBySingleChar("man", "woman");
    differBySingleChar("ok", "kok");
    differBySingleChar("kok", "oK");
    differBySingleChar("code", "scodz");
    // Case sensitive
    differBySingleChar("Apple", "apple");
    differBySingleChar("mango", "mango");
	return 0; 
}
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in golang

package main
import "fmt"

// Go lang program for
// Check if two strings differ by one character

// Function which is returns the imbalance character status
func  skipByOneChar( first  string,  second  string,  
                    f1  int,  f2  int,  n  int)int {
    var  imbalance  int = 0;
    for  i  := 0; i < n && imbalance <= 1; i++ {
        if (first[f1 + i] != second[f2 + i]) {
            imbalance++;
        }
    }
    return imbalance;
}
func differBySingleChar(first  string,  second  string) {
    // Get the size of given string 
    var  n  int = len(first);
    var  m  int = len(second);
    var  imbalance  int = 0;
    if (n == m) {
        // Simple case when both string are same length
        imbalance = skipByOneChar(first, second, 0, 0, n);
    } else if (n - m == 1 || m - n == 1) {
        // When one string contains extra character
        if (n > m) {
            // Case 2
            // Check if, last character is not match in second string
            imbalance = skipByOneChar(first, second, 0, 0, n - 1);
            if (imbalance != 0) {
                // When case 2 fail
                // Check if, first character is not match in second string
                imbalance = skipByOneChar(first, second, 1, 0, n - 1);
            }
            if (imbalance == 0) {
                // Means last character is not match in second string or
                // Means first character is not match in second string
                // Only one character are different
                imbalance = 1;
            } else {
                // When More than one character are different
                imbalance = 2;
            }
        } else {
            // Case 3
            // Check if, last character is not match in first string
            imbalance = skipByOneChar(first, second, 0, 0, m - 1);
            if (imbalance != 0) {
                // When Case 3 fail
                // Check if, first character is not match in first string
                imbalance = skipByOneChar(first, second, 0, 1, m - 1);
            }
            if (imbalance == 0) {
                // Means last character is not match in first string or
                // Means first character is not match in first string 
                imbalance = 1;
            } else {
                // When More than one character are different
                imbalance = 2;
            }
        }
    }
    fmt.Print("\n First : " + first + ", Second : " + second);
    if (imbalance == 1) {
        fmt.Println("\n Yes ");
    } else {
        fmt.Println("\n No ");
    }
}
func main(){
    // Test Cases
    differBySingleChar("code", "codes");
    differBySingleChar("man", "woman");
    differBySingleChar("ok", "kok");
    differBySingleChar("kok", "oK");
    differBySingleChar("code", "scodz");
    // Case sensitive
    differBySingleChar("Apple", "apple");
    differBySingleChar("mango", "mango");
}
Output
 First : code, Second : codes
 Yes 

 First : man, Second : woman
 No 

 First : ok, Second : kok
 Yes 

 First : kok, Second : oK
 No 

 First : code, Second : scodz
 No 

 First : Apple, Second : apple
 Yes 

 First : mango, Second : mango
 No 

Check if two strings differ by one character in c#

// Include namespace system
using System; 
//    C# program for
//    Check if two strings differ by one character
public class Difference
{
    // Function which is returns the imbalance character status
    public int skipByOneChar(String first, String second, 
                              int f1, int f2, int n)
    {
        var imbalance = 0;
        for (int i = 0; i < n && imbalance <= 1; ++i)
        {
            if (first[f1 + i] != second[f2 + i])
            {
                imbalance++;
            }
        }
        return imbalance;
    }
    public void differBySingleChar(String first, String second)
    {
        // Get the size of given string 
        var n = first.Length;
        var m = second.Length;
        var imbalance = 0;
        if (n == m)
        {
            // Simple case when both string are same length
            imbalance = this.skipByOneChar(first, second, 0, 0, n);
        }
        else if (n - m == 1 || m - n == 1)
        {
            // When one string contains extra character
            if (n > m)
            {
                // Case 2
                // Check if, last character is not match in second string
                imbalance = this.skipByOneChar(first, second, 0, 0, n - 1);
                if (imbalance != 0)
                {
                    // When case 2 fail
                    // Check if, first character is not match in second string
                    imbalance = this.skipByOneChar(first, second, 1, 0, n - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in second string or
                    // Means first character is not match in second string
                    // Only one character are different
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
            else 
            {
                // Case 3
                // Check if, last character is not match in first string
                imbalance = this.skipByOneChar(first, second, 0, 0, m - 1);
                if (imbalance != 0)
                {
                    // When Case 3 fail
                    // Check if, first character is not match in first string
                    imbalance = this.skipByOneChar(first, second, 0, 1, m - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in first string or
                    // Means first character is not match in first string 
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
        }
        Console.Write("\n First : " + first + ", Second : " + second);
        if (imbalance == 1)
        {
            Console.WriteLine("\n Yes ");
        }
        else 
        {
            Console.WriteLine("\n No ");
        }
    }
    public static void Main(String[] args)
    {
        var task = new Difference();
        // Test Cases
        task.differBySingleChar("code", "codes");
        task.differBySingleChar("man", "woman");
        task.differBySingleChar("ok", "kok");
        task.differBySingleChar("kok", "oK");
        task.differBySingleChar("code", "scodz");
        // Case sensitive
        task.differBySingleChar("Apple", "apple");
        task.differBySingleChar("mango", "mango");
    }
}
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in vb.net

' Include namespace system
Imports System 
'    Vb.net program for
'    Check if two strings differ by one character
public Class Difference
    ' Function which is returns the imbalance character status
    Public Function  skipByOneChar(ByVal first As String, 
                                   ByVal second As String, 
                                   ByVal f1 As Integer, 
                                   ByVal f2 As Integer, 
                                   ByVal n As Integer) As Integer
        Dim imbalance As Integer = 0
        With Nothing
            Dim i As Integer = 0
            While i < n AndAlso imbalance <= 1
                if (first(f1 + i) <> second(f2 + i)) Then
                     Math.Min(
                       Threading.Interlocked.Increment(imbalance),
                       imbalance - 1
                     )
                End If
                Threading.Interlocked.Increment(i)
            End While
        End With
        Return  imbalance
    End Function
    Public Sub differBySingleChar(ByVal first As String, 
                                  ByVal second As String)
        ' Get the size of given string 
        Dim n As Integer = first.Length
        Dim m As Integer = second.Length
        Dim imbalance As Integer = 0
        if (n = m) Then
            ' Simple case when both string are same length
            imbalance = Me.skipByOneChar(first, second, 0, 0, n)
        ElseIf (n - m = 1 OrElse m - n = 1) Then
            ' When one string contains extra character
            if (n > m) Then
                ' Case 2
                ' Check if, last character is not match in second string
                imbalance = Me.skipByOneChar(first, second, 0, 0, n - 1)
                if (imbalance <> 0) Then
                    ' When case 2 fail
                    ' Check if, first character is not match in second string
                    imbalance = Me.skipByOneChar(first, second, 1, 0, n - 1)
                End If
                if (imbalance = 0) Then
                    ' Means last character is not match in second string or
                    ' Means first character is not match in second string
                    ' Only one character are different
                    imbalance = 1
                Else
                    ' When More than one character are different
                    imbalance = 2
                End IF
            Else
                ' Case 3
                ' Check if, last character is not match in first string
                imbalance = Me.skipByOneChar(first, second, 0, 0, m - 1)
                if (imbalance <> 0) Then
                    ' When Case 3 fail
                    ' Check if, first character is not match in first string
                    imbalance = Me.skipByOneChar(first, second, 0, 1, m - 1)
                End If
                if (imbalance = 0) Then
                    ' Means last character is not match in first string or
                    ' Means first character is not match in first string
                    imbalance = 1
                Else
                    ' When More than one character are different
                    imbalance = 2
                End IF
            End IF
        End If
        Console.Write( vbLf &" First : " + first + ", Second : " + second)
        if (imbalance = 1) Then
            Console.WriteLine( vbLf &" Yes ")
        Else
            Console.WriteLine( vbLf &" No ")
        End IF
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Difference = New Difference()
        ' Test Cases
        task.differBySingleChar("code", "codes")
        task.differBySingleChar("man", "woman")
        task.differBySingleChar("ok", "kok")
        task.differBySingleChar("kok", "oK")
        task.differBySingleChar("code", "scodz")
        ' Case sensitive
        task.differBySingleChar("Apple", "apple")
        task.differBySingleChar("mango", "mango")
    End Sub
End Class
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in php

<?php 
//    Php program for
//    Check if two strings differ by one character
class Difference
{
    // Function which is returns the imbalance character status
    function skipByOneChar($first, $second, $f1, $f2, $n)
    {
        $imbalance = 0;
        for ($i = 0; $i < $n && $imbalance <= 1; ++$i)
        {
            if ($first[$f1 + $i] != $second[$f2 + $i])
            {
                $imbalance++;
            }
        }
        return $imbalance;
    }
    function differBySingleChar($first, $second)
    {
        // Get the size of given string 
        $n = strlen($first);
        $m = strlen($second);
        $imbalance = 0;
        if ($n == $m)
        {
            // Simple case when both string are same length
            $imbalance = $this->skipByOneChar(
              $first, $second, 0, 0, $n
            );
        }
        else if ($n - $m == 1 || $m - $n == 1)
        {
            // When one string contains extra character
            if ($n > $m)
            {
                // Case 2
                // Check if, last character is not match in second string
                $imbalance = $this->skipByOneChar(
                  $first, $second, 0, 0, $n - 1
                );
                if ($imbalance != 0)
                {
                    // When case 2 fail
                    // Check if, first character is not match in second string
                    $imbalance = $this->skipByOneChar(
                      $first, $second, 1, 0, $n - 1
                    );
                }
                if ($imbalance == 0)
                {
                    // Means last character is not match in second string or
                    // Means first character is not match in second string
                    // Only one character are different
                    $imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    $imbalance = 2;
                }
            }
            else 
            {
                // Case 3
                // Check if, last character is not match in first string
                $imbalance = $this->skipByOneChar(
                  $first, $second, 0, 0, $m - 1
                );
                if ($imbalance != 0)
                {
                    // When Case 3 fail
                    // Check if, first character is not match in first string
                    $imbalance = $this->skipByOneChar(
                      $first, $second, 0, 1, $m - 1
                    );
                }
                if ($imbalance == 0)
                {
                    // Means last character is not match in first string or
                    // Means first character is not match in first string 
                    $imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    $imbalance = 2;
                }
            }
        }
        echo "\n First : " . $first . ", Second : " . $second;
        if ($imbalance == 1)
        {
            echo "\n Yes ","\n";
        }
        else 
        {
            echo "\n No ","\n";
        }
    }
}

// Test
 $task = new Difference();
// Test Cases
$task->differBySingleChar("code", "codes");
$task->differBySingleChar("man", "woman");
$task->differBySingleChar("ok", "kok");
$task->differBySingleChar("kok", "oK");
$task->differBySingleChar("code", "scodz");
// Case sensitive
$task->differBySingleChar("Apple", "apple");
$task->differBySingleChar("mango", "mango");
Output
 First : code, Second : codes
 Yes 

 First : man, Second : woman
 No 

 First : ok, Second : kok
 Yes 

 First : kok, Second : oK
 No 

 First : code, Second : scodz
 No 

 First : Apple, Second : apple
 Yes 

 First : mango, Second : mango
 No 

Check if two strings differ by one character in node js

//    Node Js program for
//    Check if two strings differ by one character
class Difference
{
	// Function which is returns the imbalance character status
	skipByOneChar(first, second, f1, f2, n)
	{
		var imbalance = 0;
		for (var i = 0; i < n && imbalance <= 1; ++i)
		{
			if (first.charAt(f1 + i) != second.charAt(f2 + i))
			{
				imbalance++;
			}
		}
		return imbalance;
	}
	differBySingleChar(first, second)
	{
		// Get the size of given string 
		var n = first.length;
		var m = second.length;
		var imbalance = 0;
		if (n == m)
		{
			// Simple case when both string are same length
			imbalance = this.skipByOneChar(first, second, 0, 0, n);
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				// Case 2
				// Check if, last character is not match in second string
				imbalance = this.skipByOneChar(first, second, 0, 0, n - 1);
				if (imbalance != 0)
				{
					// When case 2 fail
					// Check if, first character is not match in second string
					imbalance = this.skipByOneChar(first, second, 1, 0, n - 1);
				}
				if (imbalance == 0)
				{
					// Means last character is not match in second string or
					// Means first character is not match in second string
					// Only one character are different
					imbalance = 1;
				}
				else
				{
					// When More than one character are different
					imbalance = 2;
				}
			}
			else
			{
				// Case 3
				// Check if, last character is not match in first string
				imbalance = this.skipByOneChar(first, second, 0, 0, m - 1);
				if (imbalance != 0)
				{
					// When Case 3 fail
					// Check if, first character is not match in first string
					imbalance = this.skipByOneChar(first, second, 0, 1, m - 1);
				}
				if (imbalance == 0)
				{
					// Means last character is not match in first string or
					// Means first character is not match in first string 
					imbalance = 1;
				}
				else
				{
					// When More than one character are different
					imbalance = 2;
				}
			}
		}
		console.log("\n First : " + first + ", Second : " + second);
		if (imbalance == 1)
		{
			console.log("\n Yes ");
		}
		else
		{
			console.log("\n No ");
		}
	}
}
// Start program execution
var task = new Difference();
// Test Cases
task.differBySingleChar("code", "codes");
task.differBySingleChar("man", "woman");
task.differBySingleChar("ok", "kok");
task.differBySingleChar("kok", "oK");
task.differBySingleChar("code", "scodz");
// Case sensitive
task.differBySingleChar("Apple", "apple");
task.differBySingleChar("mango", "mango");
Output
 First : code, Second : codes

 Yes

 First : man, Second : woman

 No

 First : ok, Second : kok

 Yes

 First : kok, Second : oK

 No

 First : code, Second : scodz

 No

 First : Apple, Second : apple

 Yes

 First : mango, Second : mango

 No

Check if two strings differ by one character in python

#  Python 3 program for
#  Check if two strings differ by one character
class Difference :
    # Function which is returns the imbalance character status
    def  skipByOneChar(self, first,  second,  f1,  f2,  n) :
        imbalance = 0
        i = 0
        while (i < n and imbalance <= 1) :
            if (first[f1 + i] != second[f2 + i]) :
                imbalance += 1
            i += 1
        return imbalance
    def differBySingleChar(self, first,  second) :
        # Get the size of given string 
        n = len(first)
        m = len(second)
        imbalance = 0
        if (n == m) :
            # Simple case when both string are same length
            imbalance = self.skipByOneChar(first, second, 0, 0, n)
        elif(n - m == 1 or m - n == 1) :
            # When one string contains extra character
            if (n > m) :
                # Case 2
                # Check if, last character is not match in second string
                imbalance = self.skipByOneChar(first, second, 0, 0, n - 1)
                if (imbalance != 0) :
                    # When case 2 fail
                    # Check if, first character is not match in second string
                    imbalance = self.skipByOneChar(first, second, 1, 0, n - 1)
                if (imbalance == 0) :
                    # Means last character is not match in second string or
                    # Means first character is not match in second string
                    # Only one character are different
                    imbalance = 1
                else :
                    # When More than one character are different
                    imbalance = 2
            else :
                # Case 3
                # Check if, last character is not match in first string
                imbalance = self.skipByOneChar(first, second, 0, 0, m - 1)
                if (imbalance != 0) :
                    # When Case 3 fail
                    # Check if, first character is not match in first string
                    imbalance = self.skipByOneChar(first, second, 0, 1, m - 1)
                if (imbalance == 0) :
                    # Means last character is not match in first string or
                    # Means first character is not match in first string
                    imbalance = 1
                else :
                    # When More than one character are different
                    imbalance = 2
        print("\n First : " + first + ", Second : " + second, end ="")
        if (imbalance == 1) :
            print("\n Yes ")
        else :
            print("\n No ")

if __name__=="__main__":
    task = Difference()
    # Test Cases
    task.differBySingleChar("code", "codes")
    task.differBySingleChar("man", "woman")
    task.differBySingleChar("ok", "kok")
    task.differBySingleChar("kok", "oK")
    task.differBySingleChar("code", "scodz")
    # Case sensitive
    task.differBySingleChar("Apple", "apple")
    task.differBySingleChar("mango", "mango")
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in ruby

#  Ruby program for
#  Check if two strings differ by one character
class Difference
    # Function which is returns the imbalance character status
    def skipByOneChar( first,  second,  f1,  f2,  n)
        imbalance = 0
        i = 0
        while (i < n && imbalance <= 1)
            if (first[f1 + i] != second[f2 + i])
                imbalance += 1
            end
            i += 1
        end
        return imbalance
    end
    def differBySingleChar( first,  second)
        # Get the size of given string 
        n = first.length
        m = second.length
        imbalance = 0
        if (n == m)
            # Simple case when both string are same length
            imbalance = self.skipByOneChar(first, second, 0, 0, n)
        elsif (n - m == 1 || m - n == 1)
            # When one string contains extra character
            if (n > m)
                # Case 2
                # Check if, last character is not match in second string
                imbalance = self.skipByOneChar(first, second, 0, 0, n - 1)
                if (imbalance != 0)
                    # When case 2 fail
                    # Check if, first character is not match in second string
                    imbalance = self.skipByOneChar(first, second, 1, 0, n - 1)
                end
                if (imbalance == 0)
                    # Means last character is not match in second string or
                    # Means first character is not match in second string
                    # Only one character are different
                    imbalance = 1
                else
                    # When More than one character are different
                    imbalance = 2
                end
            else
                # Case 3
                # Check if, last character is not match in first string
                imbalance = self.skipByOneChar(first, second, 0, 0, m - 1)
                if (imbalance != 0)
                    # When Case 3 fail
                    # Check if, first character is not match in first string
                    imbalance = self.skipByOneChar(first, second, 0, 1, m - 1)
                end
                if (imbalance == 0)
                    # Means last character is not match in first string or
                    # Means first character is not match in first string 
                    imbalance = 1
                else
                    # When More than one character are different
                    imbalance = 2
                end
            end
        end
        print("\n First : " + first + ", Second : " + second)
        if (imbalance == 1)
            print("\n Yes ","\n")
        else
            print("\n No ","\n")
        end
    end
    def self.main()
        task = Difference.new()
        # Test Cases
        task.differBySingleChar("code", "codes")
        task.differBySingleChar("man", "woman")
        task.differBySingleChar("ok", "kok")
        task.differBySingleChar("kok", "oK")
        task.differBySingleChar("code", "scodz")
        # Case sensitive
        task.differBySingleChar("Apple", "apple")
        task.differBySingleChar("mango", "mango")
    end
end
Difference.main()
Output
 First : code, Second : codes
 Yes 

 First : man, Second : woman
 No 

 First : ok, Second : kok
 Yes 

 First : kok, Second : oK
 No 

 First : code, Second : scodz
 No 

 First : Apple, Second : apple
 Yes 

 First : mango, Second : mango
 No 

Check if two strings differ by one character in scala

import scala.collection.mutable._;
//    Scala program for
//    Check if two strings differ by one character
class Difference ()
{
    // Function which is returns the imbalance character status
    def skipByOneChar(first : String, second : String, 
                      f1 : Int, f2 : Int, n : Int) : Int=
    {
        var imbalance = 0
        var i = 0
        while (i < n && imbalance <= 1)
        {
            if (first.charAt(f1 + i) != second.charAt(f2 + i))
            {
                imbalance += 1
            }
            i += 1
        }
        return imbalance
    }
    def differBySingleChar(first : String, second : String) : Unit=
    {
        // Get the size of given string 
        var n = first.length()
        var m = second.length()
        var imbalance = 0
        if (n == m)
        {
            // Simple case when both string are same length
            imbalance = skipByOneChar(first, second, 0, 0, n)
        }
        else if (n - m == 1 || m - n == 1)
        {
            // When one string contains extra character
            if (n > m)
            {
                // Case 2
                // Check if, last character is not match in second string
                imbalance = skipByOneChar(
                  first, second, 0, 0, n - 1
                )
                if (imbalance != 0)
                {
                    // When case 2 fail
                    // Check if, first character is not match in second string
                    imbalance = skipByOneChar(
                      first, second, 1, 0, n - 1
                    )
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in second string or
                    // Means first character is not match in second string
                    // Only one character are different
                    imbalance = 1
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2
                }
            }
            else 
            {
                // Case 3
                // Check if, last character is not match in first string
                imbalance = skipByOneChar(
                  first, second, 0, 0, m - 1
                )
                if (imbalance != 0)
                {
                    // When Case 3 fail
                    // Check if, first character is not match in first string
                    imbalance = skipByOneChar(
                      first, second, 0, 1, m - 1
                    )
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in first string or
                    // Means first character is not match in first string
                    imbalance = 1
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2
                }
            }
        }
        print("\n First : " + first + ", Second : " + second)
        if (imbalance == 1)
        {
            println("\n Yes ")
        }
        else 
        {
            println("\n No ")
        }
    }
}

object Main 
{
	def main(args : Array[String]) : Unit=
    {
        var task = new Difference()
        // Test Cases
        task.differBySingleChar("code", "codes")
        task.differBySingleChar("man", "woman")
        task.differBySingleChar("ok", "kok")
        task.differBySingleChar("kok", "oK")
        task.differBySingleChar("code", "scodz")
        // Case sensitive
        task.differBySingleChar("Apple", "apple")
        task.differBySingleChar("mango", "mango")
    }
}
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in swift

//    Swift program for
//    Check if two strings differ by one character
class Difference
{
    // Function which is returns the imbalance character status
    func skipByOneChar(_ first : String, _ second : String, 
                        _ f1 : Int, _ f2 : Int, _ n : Int) -> Int
    {
        var imbalance : Int = 0;
       
		var i : Int = 0;
		while (i < n && imbalance <= 1)
		{
			if (Array(first)[f1 + i] != Array(second)[f2 + i])
			{
			  imbalance += 1;
			}
			i += 1;
		}
        
        return imbalance;
    }
    func differBySingleChar(_ first : String, _ second : String)
    {
        // Get the size of given string 
        let n : Int = first.count;
        let m : Int = second.count;
        var imbalance : Int = 0;
        if (n == m)
        {
            // Simple case when both string are same length
            imbalance = self.skipByOneChar(first,second,0,0,n);
        }
        else if (n - m == 1 || m - n == 1)
        {
            // When one string contains extra character
            if (n > m)
            {
                // Case 2
                // Check if, last character is not match in second string
                imbalance = self.skipByOneChar(first,second,0,0,n - 1);
                if (imbalance != 0)
                {
                    // When case 2 fail
                    // Check if, first character is not match in second string
                    imbalance = self.skipByOneChar(first,second,1,0,n - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in second string or
                    // Means first character is not match in second string
                    // Only one character are different
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
            else 
            {
                // Case 3
                // Check if, last character is not match in first string
                imbalance = self.skipByOneChar(first,second,0,0,m - 1);
                if (imbalance != 0)
                {
                    // When Case 3 fail
                    // Check if, first character is not match in first string
                    imbalance = self.skipByOneChar(first,second,0,1,m - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in first string or
                    // Means first character is not match in first string
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
        }
        print("\n First : " + first + ", Second : " + second,terminator: "");
        if (imbalance == 1)
        {
            print("\n Yes ");
        }
        else 
        {
            print("\n No ");
        }
    }
}

let task : Difference = Difference();
// Test Cases
task.differBySingleChar("code","codes");
task.differBySingleChar("man","woman");
task.differBySingleChar("ok","kok");
task.differBySingleChar("kok","oK");
task.differBySingleChar("code","scodz");
// Case sensitive
task.differBySingleChar("Apple","apple");
task.differBySingleChar("mango","mango");
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No

Check if two strings differ by one character in kotlin

//    Kotlin program for
//    Check if two strings differ by one character
class Difference {
    // Function which is returns the imbalance character status
    fun skipByOneChar(first : String, second : String, 
                       f1 : Int, f2 : Int, n : Int) : Int
    {
        var imbalance : Int = 0;
        var i : Int = 0;
        while (i < n && imbalance <= 1)
        {
            if (first.get(f1 + i) != second.get(f2 + i))
            {
                imbalance += 1;
            }
            i += 1;
        }
        return imbalance;
    }
    fun differBySingleChar(first : String, second : String) : Unit
    {
        // Get the size of given string 
        val n : Int = first.length;
        val m : Int = second.length;
        var imbalance : Int = 0;
        if (n == m)
        {
            // Simple case when both string are same length
            imbalance = this.skipByOneChar(first, second, 0, 0, n);
        }
        else if (n - m == 1 || m - n == 1)
        {
            // When one string contains extra character
            if (n > m)
            {
                // Case 2
                // Check if, last character is not match in second string
                imbalance = this.skipByOneChar(first, second, 0, 0, n - 1);
                if (imbalance != 0)
                {
                    // When case 2 fail
                    // Check if, first character is not match in second string
                    imbalance = this.skipByOneChar(first, second, 1, 0, n - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in second string or
                    // Means first character is not match in second string
                    // Only one character are different
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
            else 
            {
                // Case 3
                // Check if, last character is not match in first string
                imbalance = this.skipByOneChar(first, second, 0, 0, m - 1);
                if (imbalance != 0)
                {
                    // When Case 3 fail
                    // Check if, first character is not match in first string
                    imbalance = this.skipByOneChar(first, second, 0, 1, m - 1);
                }
                if (imbalance == 0)
                {
                    // Means last character is not match in first string or
                    // Means first character is not match in first string
                    imbalance = 1;
                }
                else 
                {
                    // When More than one character are different
                    imbalance = 2;
                }
            }
        }
        print("\n First : " + first + ", Second : " + second);
        if (imbalance == 1)
        {
            println("\n Yes ");
        }
        else 
        {
            println("\n No ");
        }
    }
}
fun main(args : Array<String>) : Unit
{
  val task : Difference = Difference();
  // Test Cases
  task.differBySingleChar("code", "codes");
  task.differBySingleChar("man", "woman");
  task.differBySingleChar("ok", "kok");
  task.differBySingleChar("kok", "oK");
  task.differBySingleChar("code", "scodz");
  // Case sensitive
  task.differBySingleChar("Apple", "apple");
  task.differBySingleChar("mango", "mango");
}
Output
 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : kok, Second : oK
 No

 First : code, Second : scodz
 No

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







Kalkicodeteam     494 Day ago
You are right "Ali".
We have corrected our algorithm. Thank you for your information.
Ali     557 Day ago
wrong answer for:
 code - scodz

* the size of the longest string should pass to the  "skipByOneChar" method and use while instead of for to check boundries for both string