-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaStrings.cpp
47 lines (42 loc) · 1.18 KB
/
MetaStrings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Coding Ninjas
// https://www.codingninjas.com/studio/problems/meta-strings_1089556
// Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are not considered here as Meta strings.
// Examples:
// Input : str1 = "geeks"
// str2 = "keegs"
// Output : Yes
// By just swapping 'k' and 'g' in any of string,
// both will become same.
// Input : str1 = "rsting"
// str2 = "string
// Output : No
#include <bits/stdc++.h>
bool checkMeta(string &str1, string &str2)
{
if(str1.size() != str2.size()){
return false;
}
if(str1 == str2){
return false;
}
int flag = 0;
int index = 0;
char c;
for(int i = 0; i < str1.size(); i++){
if(str1[i] != str2[i] && flag == 0){
c = str1[i];
flag = 1;
index = i;
}
else if(flag == 1 && str1[i] != str2[i]){
char temp = str1[i];
str1[i] = c;
str1[index] = temp;
break;
}
}
if(str1 == str2){
return true;
}
return false;
}