Find if an array contains a string with one mismatch

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 code
int 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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 */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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


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 contribute.geeksforgeeks.org or mail your article to contribute@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.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.