Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike

2 Answers

0 like 0 dislike
 
Best answer

Images of ques

image
image

 

by Expert (44,360 points)
0 like 0 dislike

Optimal solution in O(n) time using O(1) memory with two pointers, quite straightforward method. Not used that we could have only 'a','b','c' in the string.

 

int minString(string s){
    
    int i=0,j=s.length()-1;
    for(;i<j && s[i]==s[j];){
        char d=s[i];
        while(i<=j && s[i]==d) i++;
        while(i<=j && s[j]==d) j--;
    }
    return j-i+1;
}
    
int main(void){
    
    cout<<minString("aabcccabba")<<endl;
    cout<<minString("aacbcca")<<endl;
    
    return 0;
}
/* output:
4
1
*/
by Expert (44,360 points)
...