careercup3

/*Design an algorithm and write code to remove the duplicate characters in a string
without using any additional buffer NOTE: One or two additional variables are fine
An extra copy of the array is not
FOLLOW UP

Write the test cases for this method*/


#include <iostream>
#include <string>
#include <algorithm>
using namespace std; 
  
int partition(string str, int b, int e){
	int j = b + 1;
	for (int i = b + 1; i <= e; i++){
		if(str[i] < str[b]){
		  swap(str[i],str[j]);
		  j++;
		}else if (str[i] == str[b]) return -1;
	}
	swap(str[j],str[b]);
	return j;
}

bool judge_qs(string str,int b, int e){
	if(b < e){
		int p = partition(str,b,e);
		if (p == -1 || judge_qs(str,b,p-1) == false || judge_qs(str,p+1,e) == false) 
			return false;
   }
   return true;
}

int main() 
{ 
  string str;
  cin>>str;
  cout<<judge_qs(str,0,str.length()-1)<<endl;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章