Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add @imkaka as a contributor * day4 * Day 7 py+cpp
- Loading branch information
1 parent
dcdef31
commit 6ff806e
Showing
3 changed files
with
235 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* @author : imkaka | ||
* @date : 28/12/2018 | ||
*/ | ||
|
||
#include<iostream> | ||
#include<string> | ||
|
||
using namespace std; | ||
|
||
string isOneEditDistanceAway(string str1, string str2){ | ||
|
||
int mi = str1.size() <= str2.size() ? str1.size() : str2.size(); | ||
int ma = str1.size() > str2.size() ? str1.size() : str2.size(); | ||
if( (ma - mi) > 1){ | ||
return " No! "; | ||
} | ||
|
||
string sho = str1.size() < str2.size() ? str1 : str2; | ||
string lon = str1.size() > str2.size() ? str1 : str2; | ||
|
||
bool diff = false; | ||
int id1 = 0; | ||
int id2 = 0; | ||
|
||
while(id2 < lon.size() && id1 < sho.size()){ | ||
|
||
if(sho[id1] != lon[id2]) { | ||
|
||
if(diff) return "No!"; | ||
diff = true; | ||
|
||
if(sho.size() == lon.size()){ | ||
id1++; | ||
} | ||
}else{ | ||
id1++; | ||
} | ||
id2++; | ||
} | ||
return "Yes!"; | ||
} | ||
|
||
int main(){ | ||
|
||
string str1 , str2; | ||
|
||
cout << "Enter two strings with space in between: "; | ||
cin >> str1 >> str2; | ||
|
||
cout << str1 << " " << str2 << " => " <<isOneEditDistanceAway(str1, str2) << endl; | ||
cout << "======= Hard Coded Strings ======= " << endl; | ||
cout << "pale" << " pal" << " " << " => " <<isOneEditDistanceAway("pale", "pal") << endl; | ||
cout << "bake" << " cake" << " " << " => " <<isOneEditDistanceAway("bake", "cake") << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
@author : imkaka | ||
@date : 28/12/2018 | ||
# Merges the Insert/Remove and Replace Operation in One. | ||
# Insert and Remove are Identical Operation logically. | ||
""" | ||
|
||
import sys | ||
import math | ||
|
||
|
||
def oneEditDistance(str1, str2): | ||
if(abs(len(str1) - len(str2)) > 1): | ||
return False | ||
short = str1 if len(str1) < len(str2) else str2 | ||
longg = str1 if len(str1) > len(str2) else str2 | ||
|
||
id1 = 0 | ||
id2 = 0 | ||
flag = False | ||
|
||
while(id2 < len(longg) and id1 < len(short)): | ||
if(short[id1] != longg[id2]): | ||
if(flag): | ||
return False | ||
flag = True | ||
if(len(short) == len(longg)): | ||
id1 += 1 | ||
else: | ||
id1 += 1 | ||
id2 += 1 | ||
return True | ||
|
||
|
||
def main(): | ||
strings = input().split(' ') | ||
str1 = strings[0] | ||
str2 = strings[1] | ||
|
||
print(f"{str1} , {str2} : {oneEditDistance(str1, str2)}.") | ||
|
||
print(f" {'abcd'} , {'acd'} : {oneEditDistance('abcd', 'acd')}.") | ||
print(f"{'cake'} , {'bake'} : {oneEditDistance('cake', 'bake')}.") | ||
print(f"{'sue'} , {'chikuu'}: {oneEditDistance('sue', 'chikuu')}.") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters