Given a string s and an array of strings arr[] of size n, check whether there exists any string in arr[] that has the same length as s and differs from s at exactly one position. Return true if such a string exists; otherwise, return false.
Examples:
Input: arr[] = ["bana", "apple", "banaba", "bonaba"], s = "banana"
Output: true
Explanation: "banana" and "banaba" have the same length and differ at exactly one position.Input: arr[] = ["bana", "apple", "banaba", "bonanzo"], s = "apple"
Output: false
Explanation: No string in the array differs from "apple" by exactly one character.
Table of Content
Character Comparison - O(n × |s|) Time and O(1) Space
The idea is to compare every string in arr[] with s. If their lengths are different, they cannot differ by exactly one character. Otherwise, count the number of mismatched positions. If any string has exactly one mismatch, return true.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool isStringExist(string& s, vector<string>& arr) {
for (string& word : arr) {
if (word.size() != s.size()) continue;
int diff = 0;
// Count all mismatched characters.
for (int i = 0; i < s.size(); i++) {
if (word[i] != s[i]) diff++;
}
if (diff == 1) return true;
}
return false;
}
int main() {
vector<string> arr = {"bana", "apple", "banaba", "bonanzo"};
string s = "banana";
cout << (isStringExist(s, arr) ? "true" : "false") << endl;
s = "apple";
cout << (isStringExist(s, arr) ? "true" : "false") << endl;
return 0;
}
class GFG {
static boolean isStringExist(String s, String[] arr) {
for (String word : arr) {
if (word.length() != s.length())
continue;
int diff = 0;
// Count all mismatched characters.
for (int i = 0; i < s.length(); i++) {
if (word.charAt(i) != s.charAt(i))
diff++;
}
if (diff == 1)
return true;
}
return false;
}
public static void main(String[] args) {
String[] arr = {"bana", "apple", "banaba", "bonanzo"};
String s = "banana";
System.out.println(isStringExist(s, arr) ? "true" : "false");
s = "apple";
System.out.println(isStringExist(s, arr) ? "true" : "false");
}
}
def isStringExist(s, arr):
for word in arr:
if len(word) != len(s):
continue
diff = 0
# Count all mismatched characters.
for i in range(len(s)):
if word[i] != s[i]:
diff += 1
if diff == 1:
return True
return False
if __name__ == "__main__":
arr = ["bana", "apple", "banaba", "bonanzo"]
s = "banana"
print("true" if isStringExist(s, arr) else "false")
s = "apple"
print("true" if isStringExist(s, arr) else "false")
using System;
class GFG {
static bool isStringExist(string s, string[] arr) {
foreach (string word in arr) {
if (word.Length != s.Length)
continue;
int diff = 0;
// Count all mismatched characters.
for (int i = 0; i < s.Length; i++) {
if (word[i] != s[i])
diff++;
}
if (diff == 1)
return true;
}
return false;
}
static void Main() {
string[] arr = {"bana", "apple", "banaba", "bonanzo"};
string s = "banana";
Console.WriteLine(isStringExist(s, arr) ? "true" : "false");
s = "apple";
Console.WriteLine(isStringExist(s, arr) ? "true" : "false");
}
}
function isStringExist(s, arr) {
for (let word of arr) {
if (word.length !== s.length)
continue;
let diff = 0;
// Count all mismatched characters.
for (let i = 0; i < s.length; i++) {
if (word[i] !== s[i])
diff++;
}
if (diff === 1)
return true;
}
return false;
}
function main() {
let arr = ["bana", "apple", "banaba", "bonanzo"];
let s = "banana";
console.log(isStringExist(s, arr) ? "true" : "false");
s = "apple";
console.log(isStringExist(s, arr) ? "true" : "false");
}
main();
Output
true false
Using Sorting - O(n log n × |s|) Time and O(1) Space
The idea is to first sort the array of strings. Sorting arranges strings in lexicographical order, which places similar strings closer together. After sorting, compare each string with s. For every string having the same length as s, count the number of mismatched characters. If the mismatch count is exactly 1, return true.
#include <bits/stdc++.h>
using namespace std;
bool isStringExist(string& s, vector<string>& arr) {
// Sort the array of strings.
sort(arr.begin(), arr.end());
for (string& word : arr) {
// Skip strings with different lengths.
if (word.size() != s.size())
continue;
int diff = 0;
// Count all mismatched characters.
for (int i = 0; i < s.size(); i++) {
if (word[i] != s[i])
diff++;
}
// If exactly one mismatch is found.
if (diff == 1)
return true;
}
return false;
}
int main() {
vector<string> arr = {"bana", "apple", "banaba", "bonanzo"};
string s = "banana";
cout << (isStringExist(s, arr) ? "true" : "false") << endl;
s = "apple";
cout << (isStringExist(s, arr) ? "true" : "false") << endl;
return 0;
}
import java.util.Arrays;
class GFG {
static boolean isStringExist(String s, String[] arr) {
// Sort the array of strings.
Arrays.sort(arr);
for (String word : arr) {
// Skip strings with different lengths.
if (word.length() != s.length())
continue;
int diff = 0;
// Count all mismatched characters.
for (int i = 0; i < s.length(); i++) {
if (word.charAt(i) != s.charAt(i))
diff++;
}
// If exactly one mismatch is found.
if (diff == 1)
return true;
}
return false;
}
public static void main(String[] args) {
String[] arr = {"bana", "apple", "banaba", "bonanzo"};
String s = "banana";
System.out.println(isStringExist(s, arr) ? "true" : "false");
s = "apple";
System.out.println(isStringExist(s, arr) ? "true" : "false");
}
}
def isStringExist(s, arr):
# Sort the array of strings.
arr.sort()
for word in arr:
# Skip strings with different lengths.
if len(word) != len(s):
continue
diff = 0
# Count all mismatched characters.
for i in range(len(s)):
if word[i] != s[i]:
diff += 1
# If exactly one mismatch is found.
if diff == 1:
return True
return False
if __name__ == "__main__":
arr = ["bana", "apple", "banaba", "bonanzo"]
s = "banana"
print("true" if isStringExist(s, arr) else "false")
s = "apple"
print("true" if isStringExist(s, arr) else "false")
using System;
class GFG {
static bool isStringExist(string s, string[] arr) {
// Sort the array of strings.
Array.Sort(arr);
foreach (string word in arr) {
// Skip strings with different lengths.
if (word.Length != s.Length)
continue;
int diff = 0;
// Count all mismatched characters.
for (int i = 0; i < s.Length; i++) {
if (word[i] != s[i])
diff++;
}
// If exactly one mismatch is found.
if (diff == 1)
return true;
}
return false;
}
static void Main() {
string[] arr = {"bana", "apple", "banaba", "bonanzo"};
string s = "banana";
Console.WriteLine(isStringExist(s, arr) ? "true" : "false");
s = "apple";
Console.WriteLine(isStringExist(s, arr) ? "true" : "false");
}
}
function isStringExist(s, arr) {
// Sort the array of strings.
arr.sort();
for (let word of arr) {
// Skip strings with different lengths.
if (word.length !== s.length)
continue;
let diff = 0;
// Count all mismatched characters.
for (let i = 0; i < s.length; i++) {
if (word[i] !== s[i])
diff++;
}
// If exactly one mismatch is found.
if (diff === 1)
return true;
}
return false;
}
// Driver code
let arr = ["bana", "apple", "banaba", "bonanzo"];
let s = "banana";
console.log(isStringExist(s, arr) ? "true" : "false");
s = "apple";
console.log(isStringExist(s, arr) ? "true" : "false");
Output
true false