Skip to content

Commit

Permalink
#33 Will retry with altered output
Browse files Browse the repository at this point in the history
  • Loading branch information
debojyoti-majumder committed Jun 23, 2019
1 parent b2bf0b0 commit fc86bae
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 2019Q2/leetcode767.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,60 @@ namespace Leetcode767 {
class Solution {
private:
string _inputString;

// if returned -1 then the replacment character is not found
int getReplacementCharPos(size_t pos) {
auto retVal{-1};
auto sz {_inputString.size() };

for( size_t i=0; i<sz; i++ ) {
if( _inputString[pos] != _inputString[i] && pos != i ) {
if( i == 0 || i == sz - 1 ) {
retVal = i;
break;
}

auto& ch1 { _inputString[i-1] };
auto& ch2 { _inputString[i+1] };

if( ch1 != ch2 ) {
retVal = i;
break;
}
}
}

return retVal;
}

public:
string reorganizeString(string S) {
string retValue {""};
_inputString = S;

// If the string size is leq 2 then there is no option
// to swap the string
auto sz { _inputString.size() };
if( sz <= 2 ) return S;

for( size_t i=0; i<sz-1; i++ ) {
if( _inputString[i] == _inputString[i+1] ) {
auto pos { getReplacementCharPos(i) };

// Should return empty string
if( pos == -1 ) {
retValue = "";
break;
}

// Swaping to break sequence like "kk"
auto tmp { _inputString[i+1] };
_inputString[i+1] = _inputString[pos];
_inputString[pos] = tmp;
retValue = _inputString;
}
}

return retValue;
}
};
Expand All @@ -78,5 +127,6 @@ namespace Leetcode767 {
GTEST_TEST(Leet767, TLECase1) {
Solution s;
ASSERT_NE(s.reorganizeString("kkkkzrkatkwpkkkktrq"), "");
ASSERT_THAT(s.reorganizeString("baaba"),"ababa");
}
}

0 comments on commit fc86bae

Please sign in to comment.