# Find if an array contains a string with one mismatch

• Difficulty Level : Medium
• Last Updated : 28 May, 2021

Given a string and array of strings, find whether the array contains a string with one character difference from the given string. Array may contain strings of different lengths.
Examples:

```Input : str = "banana"
arr[] = {"bana", "apple", "banaba",
bonanzo", "banamf"}
Output :True
Explanation:-There is only a one character difference
between banana and banaba

Input : str = "banana"
arr[] = {"bana", "apple", "banabb", bonanzo",
"banamf"}
Output : False```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

We traverse through given string and check for every string in arr. Follow the two steps as given below for every string contained in arr:-
1) Check whether the string contained in arr is of the same length as the target string.
2) If yes, then check if there is only one character mismatch, if yes then return true else return false.

## C++

 `// C++ program to find if given string is present``// with one mismatch.``#include ``using` `namespace` `std;` `bool` `check(vector list, string s)``{``    ``int` `n = (``int``)list.size();` `    ``// If the array is empty   ``    ``if` `(n == 0)``        ``return` `false``;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// If sizes are same``        ``if` `(list[i].size() != s.size())``            ``continue``;`` ` `        ``bool` `diff = ``false``;``        ``for` `(``int` `j = 0; j < (``int``)list[i].size(); j++) {` `            ``if` `(list[i][j] != s[j]) {``    ` `                ``// If first mismatch  ``                ``if` `(!diff)``                    ``diff = ``true``;` `                ``// Second mismatch``                ``else` `{``                    ``diff = ``false``;``                    ``break``;``                ``}``            ``}``        ``}` `        ``if` `(diff)``            ``return` `true``;``    ``}` `    ``return` `false``;``}` `// Driver code``int` `main()``{``    ``vector s;``    ``s.push_back(``"bana"``);``    ``s.push_back(``"apple"``);``    ``s.push_back(``"banacb"``);``    ``s.push_back(``"bonanza"``);``    ``s.push_back(``"banamf"``);` `    ``cout << check(s, ``"banana"``);``    ``return` `0;``}`

## Java

 `import` `java.util.*;` `// Java program to find if``// given string is present``// with one mismatch.``class` `GFG``{` `    ``static` `boolean` `check(Vector list, String s)``    ``{``        ``int` `n = (``int``) list.size();` `        ``// If the array is empty``        ``if` `(n == ``0``)``        ``{``            ``return` `false``;``        ``}` `        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{` `            ``// If sizes are same``            ``if` `(list.get(i).length() != s.length())``            ``{``                ``continue``;``            ``}` `            ``boolean` `diff = ``false``;``            ``for` `(``int` `j = ``0``; j < (``int``) list.get(i).length(); j++)``            ``{` `                ``if` `(list.get(i).charAt(j) != s.charAt(j))``                ``{` `                    ``// If first mismatch``                    ``if` `(!diff)``                    ``{``                        ``diff = ``true``;``                    ``}``                    ` `                    ``// Second mismatch``                    ``else``                    ``{``                        ``diff = ``false``;``                        ``break``;``                    ``}``                ``}``            ``}` `            ``if` `(diff) {``                ``return` `true``;``            ``}``        ``}` `        ``return` `false``;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``Vector s = ``new` `Vector<>();``        ``s.add(``"bana"``);``        ``s.add(``"apple"``);``        ``s.add(``"banacb"``);``        ``s.add(``"bonanza"``);``        ``s.add(``"banamf"``);` `        ``System.out.println(check(s, ``"banana"``) == ``true` `? ``1` `: ``0``);``    ``}``}` `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python 3 program to find if given``# string is present with one mismatch.` `def` `check(``list``, s):``    ``n ``=` `len``(``list``)` `    ``# If the array is empty``    ``if` `(n ``=``=` `0``):``        ``return` `False` `    ``for` `i ``in` `range``(``0``, n, ``1``):``        ` `        ``# If sizes are same``        ``if` `(``len``(``list``[i]) !``=` `len``(s)):``            ``continue` `        ``diff ``=` `False``        ``for` `j ``in` `range``(``0``, ``len``(``list``[i]), ``1``):``            ``if` `(``list``[i][j] !``=` `s[j]):` `                ``# If first mismatch``                ``if` `(diff ``=``=` `False``):``                    ``diff ``=` `True` `                ``# Second mismatch``                ``else``:``                    ``diff ``=` `False``                    ``break``                ` `        ``if` `(diff):``            ``return` `True` `    ``return` `False` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``s ``=` `[]``    ``s.append(``"bana"``)``    ``s.append(``"apple"``)``    ``s.append(``"banacb"``)``    ``s.append(``"bonanza"``)``    ``s.append(``"banamf"``)` `    ``print``(``int``(check(s, ``"banana"``)))` `# This code is contributed by``# Sahil_shelangia`

## C#

 `// C# program to find if``// given string is present``// with one mismatch.``using` `System;   ``using` `System.Collections.Generic;``public` `class` `GFG``{`` ` `    ``static` `bool` `check(List list, String s)``    ``{``        ``int` `n = (``int``) list.Count;`` ` `        ``// If the array is empty``        ``if` `(n == 0)``        ``{``            ``return` `false``;``        ``}`` ` `        ``for` `(``int` `i = 0; i < n; i++)``        ``{`` ` `            ``// If sizes are same``            ``if` `(list[i].Length != s.Length)``            ``{``                ``continue``;``            ``}`` ` `            ``bool` `diff = ``false``;``            ``for` `(``int` `j = 0; j < (``int``) list[i].Length; j++)``            ``{`` ` `                ``if` `(list[i][j] != s[j])``                ``{`` ` `                    ``// If first mismatch``                    ``if` `(!diff)``                    ``{``                        ``diff = ``true``;``                    ``}``                     ` `                    ``// Second mismatch``                    ``else``                    ``{``                        ``diff = ``false``;``                        ``break``;``                    ``}``                ``}``            ``}`` ` `            ``if` `(diff) {``                ``return` `true``;``            ``}``        ``}`` ` `        ``return` `false``;``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``List s = ``new` `List();``        ``s.Add(``"bana"``);``        ``s.Add(``"apple"``);``        ``s.Add(``"banacb"``);``        ``s.Add(``"bonanza"``);``        ``s.Add(``"banamf"``);`` ` `        ``Console.WriteLine(check(s, ``"banana"``) == ``true` `? 1 : 0);``    ``}``}``// This code has been contributed by 29AjayKumar`

## Javascript

 ``

Output:

`0`

This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.