Check if two strings differ by one character

Here given code implementation process.

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

int skipByOneChar(const char *first , const char *second, int n)
{
	int imbalance = 0;
	int a = 0;
	int b = 0;
	for (int i = 0; i < n && imbalance <= 1; ++i)
	{
		if (first[a] == second[b])
		{
          	// When both string character at position a and b is same
			a++;
			b++;
		}
		else
		{
			a++;
			imbalance++;
		}
	}
	if (imbalance == 0)
	{
		// In case, last character is extra in first string
		return 1;
	}
	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)
	{
		// Case A when both string are equal size
		for (int i = 0; i < n && imbalance <= 1; ++i)
		{
			if (first[i] != second[i])
			{
				imbalance++;
			}
		}
	}
	else if (n - m == 1 || m - n == 1)
	{
		// When one string contains extra character
		if (n > m)
		{
			imbalance = skipByOneChar(first, second, m);
		}
		else
		{
			imbalance = skipByOneChar(second, first, n);
		}
	}
	printf("\n First : %s, Second : %s", first, second);
	if (imbalance == 1)
	{
		printf("\n Yes \n");
	}
	else
	{
		printf("\n No \n");
	}
}
int main(int argc, char const *argv[])
{
	// Test Cases
	differBySingleChar("code", "codes");
	differBySingleChar("man", "woman");
	differBySingleChar("ok", "kok");
	// 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 : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
/*
    Java Program for
    Check if two strings differ by one character
*/
public class Difference
{
	public int skipByOneChar(String first, String second, int n)
	{
		int imbalance = 0;
		int a = 0;
		int b = 0;
		for (int i = 0; i < n && imbalance <= 1; ++i)
		{
			if (first.charAt(a) == second.charAt(b))
			{
				// When both string character at position a and b is same
				a++;
				b++;
			}
			else
			{
				a++;
				imbalance++;
			}
		}
		if (imbalance == 0)
		{
			// In case, last character is extra in first string
			return 1;
		}
		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)
		{
			// Case A when both string are equal size
			for (int i = 0; i < n && imbalance <= 1; ++i)
			{
				if (first.charAt(i) != second.charAt(i))
				{
					imbalance++;
				}
			}
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				imbalance = skipByOneChar(first, second, m);
			}
			else
			{
				imbalance = skipByOneChar(second, first, n);
			}
		}
		System.out.print("\n First : " + first + ", Second : " + second);
		if (imbalance == 1)
		{
			System.out.print("\n Yes \n");
		}
		else
		{
			System.out.print("\n No \n");
		}
	}
	public static void main(String[] args)
	{
		Difference task = new Difference();
		// Test Cases
		task.differBySingleChar("code", "codes");
		task.differBySingleChar("man", "woman");
		task.differBySingleChar("ok", "kok");
		// 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 : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Check if two strings differ by one character
*/
class Difference
{
	public: int skipByOneChar(string first, string second, int n)
	{
		int imbalance = 0;
		int a = 0;
		int b = 0;
		for (int i = 0; i < n && imbalance <= 1; ++i)
		{
			if (first[a] == second[b])
			{
				// When both string character at position a and b is same
				a++;
				b++;
			}
			else
			{
				a++;
				imbalance++;
			}
		}
		if (imbalance == 0)
		{
			// In case, last character is extra in first string
			return 1;
		}
		return imbalance;
	}
	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)
		{
			// Case A when both string are equal size
			for (int i = 0; i < n && imbalance <= 1; ++i)
			{
				if (first[i] != second[i])
				{
					imbalance++;
				}
			}
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				imbalance = this->skipByOneChar(first, second, m);
			}
			else
			{
				imbalance = this->skipByOneChar(second, first, n);
			}
		}
		cout << "\n First : " << first << ", Second : " << second;
		if (imbalance == 1)
		{
			cout << "\n Yes \n";
		}
		else
		{
			cout << "\n No \n";
		}
	}
};
int main()
{
	Difference *task = new Difference();
	// Test Cases
	task->differBySingleChar("code", "codes");
	task->differBySingleChar("man", "woman");
	task->differBySingleChar("ok", "kok");
	// 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 : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
// Include namespace system
using System;
/*
    Csharp Program for
    Check if two strings differ by one character
*/
public class Difference
{
	public int skipByOneChar(String first, String second, int n)
	{
		int imbalance = 0;
		int a = 0;
		int b = 0;
		for (int i = 0; i < n && imbalance <= 1; ++i)
		{
			if (first[a] == second[b])
			{
				// When both string character at position a and b is same
				a++;
				b++;
			}
			else
			{
				a++;
				imbalance++;
			}
		}
		if (imbalance == 0)
		{
			// In case, last character is extra in first string
			return 1;
		}
		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)
		{
			// Case A when both string are equal size
			for (int i = 0; i < n && imbalance <= 1; ++i)
			{
				if (first[i] != second[i])
				{
					imbalance++;
				}
			}
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				imbalance = this.skipByOneChar(first, second, m);
			}
			else
			{
				imbalance = this.skipByOneChar(second, first, n);
			}
		}
		Console.Write("\n First : " + first + ", Second : " + second);
		if (imbalance == 1)
		{
			Console.Write("\n Yes \n");
		}
		else
		{
			Console.Write("\n No \n");
		}
	}
	public static void Main(String[] args)
	{
		Difference task = new Difference();
		// Test Cases
		task.differBySingleChar("code", "codes");
		task.differBySingleChar("man", "woman");
		task.differBySingleChar("ok", "kok");
		// 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 : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
package main

import "fmt"
/*
    Go Program for
    Check if two strings differ by one character
*/

func skipByOneChar(first string, second string, n int) int {
	var imbalance int = 0
	var a int = 0
	var b int = 0
	for i := 0 ; i < n && imbalance <= 1 ; i++ {
		if first[a] == second[b] {
			// When both string character at position a and b is same
			a++
			b++
		} else {
			a++
			imbalance++
		}
	}
	if imbalance == 0 {
		// In case, last character is extra in first string
		return 1
	}
	return imbalance
}
func differBySingleChar(first, 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 {
		// Case A when both string are equal size
		for i := 0 ; i < n && imbalance <= 1 ; i++ {
			if first[i] != second[i] {
				imbalance++
			}
		}
	} else if n - m == 1 || m - n == 1 {
		// When one string contains extra character
		if n > m {
			imbalance = skipByOneChar(first, second, m)
		} else {
			imbalance = skipByOneChar(second, first, n)
		}
	}
	fmt.Print("\n First : ", first, ", Second : ", second)
	if imbalance == 1 {
		fmt.Print("\n Yes \n")
	} else {
		fmt.Print("\n No \n")
	}
}
func main() {

	// Test Cases
	differBySingleChar("code", "codes")
	differBySingleChar("man", "woman")
	differBySingleChar("ok", "kok")
	// 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 : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
<?php
/*
    Php Program for
    Check if two strings differ by one character
*/
class Difference
{
	public	function skipByOneChar($first, $second, $n)
	{
		$imbalance = 0;
		$a = 0;
		$b = 0;
		for ($i = 0; $i < $n && $imbalance <= 1; ++$i)
		{
			if ($first[$a] == $second[$b])
			{
				// When both string character at position a and b is same
				$a++;
				$b++;
			}
			else
			{
				$a++;
				$imbalance++;
			}
		}
		if ($imbalance == 0)
		{
			// In case, last character is extra in first string
			return 1;
		}
		return $imbalance;
	}
	public	function differBySingleChar($first, $second)
	{
		// Get the size of given string 
		$n = strlen($first);
		$m = strlen($second);
		$imbalance = 0;
		if ($n == $m)
		{
			// Case A when both string are equal size
			for ($i = 0; $i < $n && $imbalance <= 1; ++$i)
			{
				if ($first[$i] != $second[$i])
				{
					$imbalance++;
				}
			}
		}
		else if ($n - $m == 1 || $m - $n == 1)
		{
			// When one string contains extra character
			if ($n > $m)
			{
				$imbalance = $this->skipByOneChar($first, $second, $m);
			}
			else
			{
				$imbalance = $this->skipByOneChar($second, $first, $n);
			}
		}
		echo("\n First : ".$first.
			", Second : ".$second);
		if ($imbalance == 1)
		{
			echo("\n Yes \n");
		}
		else
		{
			echo("\n No \n");
		}
	}
}

function main()
{
	$task = new Difference();
	// Test Cases
	$task->differBySingleChar("code", "codes");
	$task->differBySingleChar("man", "woman");
	$task->differBySingleChar("ok", "kok");
	// Case sensitive
	$task->differBySingleChar("Apple", "apple");
	$task->differBySingleChar("mango", "mango");
}
main();

Output

 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
/*
    Node JS Program for
    Check if two strings differ by one character
*/
class Difference
{
	skipByOneChar(first, second, n)
	{
		var imbalance = 0;
		var a = 0;
		var b = 0;
		for (var i = 0; i < n && imbalance <= 1; ++i)
		{
			if (first.charAt(a) == second.charAt(b))
			{
				// When both string character at position a and b is same
				a++;
				b++;
			}
			else
			{
				a++;
				imbalance++;
			}
		}
		if (imbalance == 0)
		{
			// In case, last character is extra in first string
			return 1;
		}
		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)
		{
			// Case A when both string are equal size
			for (var i = 0; i < n && imbalance <= 1; ++i)
			{
				if (first.charAt(i) != second.charAt(i))
				{
					imbalance++;
				}
			}
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				imbalance = this.skipByOneChar(first, second, m);
			}
			else
			{
				imbalance = this.skipByOneChar(second, first, n);
			}
		}
		process.stdout.write("\n First : " + first + ", Second : " + second);
		if (imbalance == 1)
		{
			process.stdout.write("\n Yes \n");
		}
		else
		{
			process.stdout.write("\n No \n");
		}
	}
}

function main()
{
	var task = new Difference();
	// Test Cases
	task.differBySingleChar("code", "codes");
	task.differBySingleChar("man", "woman");
	task.differBySingleChar("ok", "kok");
	// Case sensitive
	task.differBySingleChar("Apple", "apple");
	task.differBySingleChar("mango", "mango");
}
main();

Output

 First : code, Second : codes
 Yes

 First : man, Second : woman
 No

 First : ok, Second : kok
 Yes

 First : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
#    Python 3 Program for
#    Check if two strings differ by one character
class Difference :
	def skipByOneChar(self, first, second, n) :
		imbalance = 0
		a = 0
		b = 0
		i = 0
		while (i < n and imbalance <= 1) :
			if (first[a] == second[b]) :
				#  When both string character at position a and b is same
				a += 1
				b += 1
			else :
				a += 1
				imbalance += 1
			
			i += 1
		
		if (imbalance == 0) :
			#  In case, last character is extra in first string
			return 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) :
			i = 0
			#  Case A when both string are equal size
			while (i < n and imbalance <= 1) :
				if (first[i] != second[i]) :
					imbalance += 1
				
				i += 1
			
		elif (n - m == 1 or m - n == 1) :
			#  When one string contains extra character
			if (n > m) :
				imbalance = self.skipByOneChar(first, second, m)
			else :
				imbalance = self.skipByOneChar(second, first, n)
			
		
		print("\n First :", first ,", Second :", second, end = "")
		if (imbalance == 1) :
			print("\n Yes ")
		else :
			print("\n No ")
		
	

def main() :
	task = Difference()
	#  Test Cases
	task.differBySingleChar("code", "codes")
	task.differBySingleChar("man", "woman")
	task.differBySingleChar("ok", "kok")
	#  Case sensitive
	task.differBySingleChar("Apple", "apple")
	task.differBySingleChar("mango", "mango")

if __name__ == "__main__": main()

Output

 First : code , Second : codes
 Yes

 First : man , Second : woman
 No

 First : ok , Second : kok
 Yes

 First : Apple , Second : apple
 Yes

 First : mango , Second : mango
 No
#    Ruby Program for
#    Check if two strings differ by one character
class Difference 
	def skipByOneChar(first, second, n) 
		imbalance = 0
		a = 0
		b = 0
		i = 0
		while (i < n && imbalance <= 1) 
			if (first[a] == second[b]) 
				#  When both string character at position a and b is same
				a += 1
				b += 1
			else
 
				a += 1
				imbalance += 1
			end

			i += 1
		end

		if (imbalance == 0) 
			#  In case, last character is extra in first string
			return 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) 
			i = 0
			#  Case A when both string are equal size
			while (i < n && imbalance <= 1) 
				if (first[i] != second[i]) 
					imbalance += 1
				end

				i += 1
			end

		elsif (n - m == 1 || m - n == 1) 
			#  When one string contains extra character
			if (n > m) 
				imbalance = self.skipByOneChar(first, second, m)
			else
 
				imbalance = self.skipByOneChar(second, first, n)
			end

		end

		print("\n First : ", first ,", Second : ", second)
		if (imbalance == 1) 
			print("\n Yes \n")
		else
 
			print("\n No \n")
		end

	end

end

def main() 
	task = Difference.new()
	#  Test Cases
	task.differBySingleChar("code", "codes")
	task.differBySingleChar("man", "woman")
	task.differBySingleChar("ok", "kok")
	#  Case sensitive
	task.differBySingleChar("Apple", "apple")
	task.differBySingleChar("mango", "mango")
end

main()

Output

 First : code, Second : codes
 Yes 

 First : man, Second : woman
 No 

 First : ok, Second : kok
 Yes 

 First : Apple, Second : apple
 Yes 

 First : mango, Second : mango
 No 
import scala.collection.mutable._;
/*
    Scala Program for
    Check if two strings differ by one character
*/
class Difference()
{
	def skipByOneChar(first: String, second: String, n: Int): Int = {
		var imbalance: Int = 0;
		var a: Int = 0;
		var b: Int = 0;
		var i: Int = 0;
		while (i < n && imbalance <= 1)
		{
			if (first.charAt(a) == second.charAt(b))
			{
				// When both string character at position a and b is same
				a += 1;
				b += 1;
			}
			else
			{
				a += 1;
				imbalance += 1;
			}
			i += 1;
		}
		if (imbalance == 0)
		{
			// In case, last character is extra in first string
			return 1;
		}
		return imbalance;
	}
	def differBySingleChar(first: String, second: String): Unit = {
		// Get the size of given string 
		var n: Int = first.length();
		var m: Int = second.length();
		var imbalance: Int = 0;
		if (n == m)
		{
			var i: Int = 0;
			// Case A when both string are equal size
			while (i < n && imbalance <= 1)
			{
				if (first.charAt(i) != second.charAt(i))
				{
					imbalance += 1;
				}
				i += 1;
			}
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				imbalance = skipByOneChar(first, second, m);
			}
			else
			{
				imbalance = skipByOneChar(second, first, n);
			}
		}
		print("\n First : " + first + ", Second : " + second);
		if (imbalance == 1)
		{
			print("\n Yes \n");
		}
		else
		{
			print("\n No \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Difference = new Difference();
		// Test Cases
		task.differBySingleChar("code", "codes");
		task.differBySingleChar("man", "woman");
		task.differBySingleChar("ok", "kok");
		// 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 : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No
import Foundation;
/*
    Swift 4 Program for
    Check if two strings differ by one character
*/
class Difference
{
	func skipByOneChar(_ first: [Character], _ second: [Character], _ n: Int) -> Int
	{
		var imbalance: Int = 0;
		var a: Int = 0;
		var b: Int = 0;
		var i: Int = 0;
		while (i < n && imbalance <= 1)
		{
			if (first[a] == second[b])
			{
				// When both string character at position a and b is same
				a += 1;
				b += 1;
			}
			else
			{
				a += 1;
				imbalance += 1;
			}
			i += 1;
		}
		if (imbalance == 0)
		{
			// In case, last character is extra in first string
			return 1;
		}
		return imbalance;
	}
	func differBySingleChar(_ text1: String, _ text2: String)
	{
        let first = Array(text1);
      	let second = Array(text2);
		// Get the size of given string 
		let n: Int = first.count;
		let m: Int = second.count;
		var imbalance: Int = 0;
		if (n == m)
		{
			var i: Int = 0;
			// Case A when both string are equal size
			while (i < n && imbalance <= 1)
			{
				if (first[i]  != second[i])
				{
					imbalance += 1;
				}
				i += 1;
			}
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				imbalance = self.skipByOneChar(first, second, m);
			}
			else
			{
				imbalance = self.skipByOneChar(second, first, n);
			}
		}
		print("\n First : ", text1 ,", Second : ", text2, terminator: "");
		if (imbalance == 1)
		{
			print("\n Yes ");
		}
		else
		{
			print("\n No ");
		}
	}
}
func main()
{
	let task: Difference = Difference();
	// Test Cases
	task.differBySingleChar("code", "codes");
	task.differBySingleChar("man", "woman");
	task.differBySingleChar("ok", "kok");
	// Case sensitive
	task.differBySingleChar("Apple", "apple");
	task.differBySingleChar("mango", "mango");
}
main();

Output

 First :  code , Second :  codes
 Yes

 First :  man , Second :  woman
 No

 First :  ok , Second :  kok
 Yes

 First :  Apple , Second :  apple
 Yes

 First :  mango , Second :  mango
 No
/*
    Kotlin Program for
    Check if two strings differ by one character
*/
class Difference
{
	fun skipByOneChar(first: String, second: String, n: Int): Int
	{
		var imbalance: Int = 0;
		var a: Int = 0;
		var b: Int = 0;
		var i: Int = 0;
		while (i < n && imbalance <= 1)
		{
			if (first.get(a) == second.get(b))
			{
				// When both string character at position a and b is same
				a += 1;
				b += 1;
			}
			else
			{
				a += 1;
				imbalance += 1;
			}
			i += 1;
		}
		if (imbalance == 0)
		{
			// In case, last character is extra in first string
			return 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)
		{
			var i: Int = 0;
			// Case A when both string are equal size
			while (i < n && imbalance <= 1)
			{
				if (first.get(i) != second.get(i))
				{
					imbalance += 1;
				}
				i += 1;
			}
		}
		else if (n - m == 1 || m - n == 1)
		{
			// When one string contains extra character
			if (n > m)
			{
				imbalance = this.skipByOneChar(first, second, m);
			}
			else
			{
				imbalance = this.skipByOneChar(second, first, n);
			}
		}
		print("\n First : " + first + ", Second : " + second);
		if (imbalance == 1)
		{
			print("\n Yes \n");
		}
		else
		{
			print("\n No \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Difference = Difference();
	// Test Cases
	task.differBySingleChar("code", "codes");
	task.differBySingleChar("man", "woman");
	task.differBySingleChar("ok", "kok");
	// 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 : Apple, Second : apple
 Yes

 First : mango, Second : mango
 No


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







© 2021, kalkicode.com, All rights reserved