GeeksforGeeks » Interview Questions
Amazon Interview Question for Software Engineer/Developer about Strings
(10 posts)-
Given a string consisting of a, b and c's, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if 'a' and 'c' are adjacent, they can replaced with 'b'.
What is the smallest string which can result by applying this operation repeatedly?
-
can you give an example??
-
If the third character can be anything then in that case if we find any 2 adjacent characters same then the whole string could be converged to just 1 element.
-
sorry for d above i mistook the question
-
#include<stdio.h> #include<conio.h> #include<string.h> int top=-1; int count=0; char st[10]; void push(char s) { st[++top]=s; while(top>=1 && (st[top]!=st[top-1])) { if(st[top]=='a') { if(st[top-1]=='b') st[--top]='c'; else st[--top]='b'; continue; } if(st[top]=='b') { if(st[top-1]=='c') st[--top]='a'; else st[--top]='c'; continue; } if(st[top-1]=='a') st[--top]='b'; else st[--top]='a'; } } void main() { char *s; int i=0; char st[10]; clrscr(); scanf("%s", s); for(i=0;i<strlen(s);i++) push(s[i]); printf("%d", top+1); getch(); } -
#include <stdio.h> #include <string.h> void reduce(char *str){ if(!str) return; char *p = str, *q = str; int s = 'a' + 'b' + 'c'; if(*q) { for(q++; *q; q++){ if(*p != *q){ *p = s - (*p + *q); while((p - 1) >= str && *p != *(p - 1)){ *(p - 1) = s - (*p + *(p - 1)); p--; } }else{ *++p = *q; } } *++p = 0; } } int main(){ char str[256]; scanf("%s", str); reduce(str); printf("%s\n", str); return 0; } -
@Sambasiva: You solution is wrong it fails for bcaa.
-
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Vector; public class StringRed { static int Func(String v) { if(v.length()==0) return 0; if(v.length()==1) return 1; char rep='a'; char s[]; StringBuffer b=new StringBuffer(v); int i=0; do { s=b.toString().toCharArray(); if(s.length==1) break; if(s[i]!=s[i+1]) { rep= ((s[i]=='a'||s[i]=='b')&&(s[i+1]=='b'||s[i+1]=='a'))?'c': (((s[i]=='b'||s[i]=='c')&&(s[i+1]=='c'||s[i+1]=='b'))?'a': (((s[i]=='a'||s[i]=='c')&&(s[i+1]=='c'||s[i+1]=='a'))?'b':'')); b.replace(i, i+2,String.valueOf(rep)); i=0; } else { i++; } }while(i!=s.length-1); return b.length(); } public static void main(String args[]) throws IOException { int count=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { count=Integer.parseInt(br.readLine()); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Vector<String> v=new Vector<String>(); while(count!=0) { String s=br.readLine(); if(s.length()<=100) { v.add(s); count--; } } for(int i=0;i<v.size();i++) System.out.println(Func(v.elementAt(i))); } } -
let f be function which gives min value
then,
f(1)=1f(2)={ 1 , if s[1]==s[2]
2, otherwise
}f(3)={ 1, if any two character is same
2, if all r different
3, if all are same
}f(n)= min ( f(s[1..2])+f( character(s) results fron f(s[1...2])+ s[3...n]) , //considering string of len 2
f(s[1..3])+f( character(s) results from f(s[1...3])+ s[4...n]) //cons str of len3}
-
*
f(2)={ 1 , if s[1]!=s[2]
2, otherwise
}
Reply
You must log in to post.