Find if an array contains a string with one mismatch - GeeksforGeeks

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
 
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++
Java
Python3
C#
Javascript
// C++ program to find if given string is present// with one mismatch.#include <bits/stdc++.h>using namespace std; bool check(vector<string> 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 codeint main(){    vector<string> 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;}
import java.util.*; // Java program to find if// given string is present// with one mismatch.class GFG{     static boolean check(Vector<String> 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<String> 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 */
# 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 codeif __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# program to find if// given string is present// with one mismatch.using System;   using System.Collections.Generic;public class GFG{      static bool check(List<String> 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<String> s = new List<String>();        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
<script>    // Javascript program to find if    // given string is present    // with one mismatch.         function check(list, s)    {        let n = list.length;            // If the array is empty        if (n == 0)        {            return false;        }            for (let i = 0; i < n; i++)        {                // If sizes are same            if (list[i].length != s.length)            {                continue;            }                let diff = false;            for (let j = 0; j < 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;    }         let s = [];    s.push("bana");    s.push("apple");    s.push("banacb");    s.push("bonanza");    s.push("banamf");     document.write(check(s, "banana") == true ? 1 : 0);     </script>
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.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
sahilshelangia
princiraj1992
29AjayKumar
rameshtravel07
Arrays
Strings
Arrays
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Building Heap from Array
Reversal algorithm for array rotation
Program to find sum of elements in a given array
Reverse a string in Java
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types
Python program to check if a string is palindrome or not