From f77cbe561dbfdd80a1bd1eda58fb205d49e30ea3 Mon Sep 17 00:00:00 2001 From: broy27 <71218815+broy27@users.noreply.github.com> Date: Tue, 19 Jan 2021 22:41:57 +0530 Subject: [PATCH] Update Q5_OneAway.cpp The earlier code would give true for case 5 s1 = "palpet"; s2 = "tpalep"; since the code checks only for the count of character and not their position. --- Chapter1_ArraysAndStrings/Q5_OneAway.cpp | 49 +++++++++++++++--------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/Chapter1_ArraysAndStrings/Q5_OneAway.cpp b/Chapter1_ArraysAndStrings/Q5_OneAway.cpp index 70aeb92..0aa6780 100644 --- a/Chapter1_ArraysAndStrings/Q5_OneAway.cpp +++ b/Chapter1_ArraysAndStrings/Q5_OneAway.cpp @@ -1,25 +1,38 @@ #include -#include +#include +#include using namespace std; class solution{ public: - bool oneAway(string s1, string s2){ - unordered_map m1; - unordered_map m2; - int diff = 0; - - for(auto i : s1) - m1[i]++; - for(auto i : s2) - m2[i]++; - - for(auto i : s1) - if(m1[i]!=m2[i]) - diff++; - - return diff<2?true:false; + bool oneAway(string str1, string str2){ + if(fabs(str1.size() - str2.size()) > 1) + return false; + string s1=str1.size()