We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
3 test cases failing, any idea what am I missing here.
classResult{/* * Complete the 'biggerIsGreater' function below. * * The function is expected to return a STRING. * The function accepts STRING w as parameter. */staticclassTuple{Characterc;intpos;Tuple(Characterc,intpos){this.c=c;this.pos=pos;}}publicstaticStringbiggerIsGreater(Stringw){// Write your code hereList<Character>alphaArray="abcdefghijklmnopqrstuvwxyz".chars().mapToObj(c->(char)c).collect(toList());List<Tuple>tupleList=newArrayList<>();w.chars().mapToObj(c->(char)c).collect(toList()).forEach(c->tupleList.add(newTuple(c,alphaArray.indexOf(c))));for(inttoCompareIndex=tupleList.size()-1;toCompareIndex>0;toCompareIndex--){for(intcompareWIthIndex=toCompareIndex-1;compareWIthIndex>=0;compareWIthIndex--){if(tupleList.get(toCompareIndex).pos>tupleList.get(compareWIthIndex).pos){Stringword=w.substring(0,compareWIthIndex);word=word+tupleList.get(toCompareIndex).c;tupleList.get(toCompareIndex).c=tupleList.get(compareWIthIndex).c;tupleList.get(toCompareIndex).pos=tupleList.get(compareWIthIndex).pos;intfinalCompareWIthIndex=compareWIthIndex;List<Character>lastChars=tupleList.stream().filter(x->tupleList.indexOf(x)>finalCompareWIthIndex).map(t->t.c).collect(Collectors.toList()).stream().sorted().collect(toList());word=word+lastChars.stream().map(e->e.toString()).reduce((acc,e)->acc+e).get();returnword;}}}return"no answer";}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
3 test cases failing, any idea what am I missing here.