Find if an array contains a string with one mismatch | C ++ program to find if given string is present with one mismatch . ; If the array is empty ; If sizes are same ; If first mismatch ; Second mismatch ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( vector < string > list , string s ) { int n = ( int ) list . size ( ) ; if ( n == 0 ) return false ; for ( int i = 0 ; i < n ; i ++ ) { 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 ( ! diff ) diff = true ; else { diff = false ; break ; } } } if ( diff ) return true ; } return false ; } 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 ; }