Open DevShivmohan opened 1 year ago
wwewwerrtt
2w1e2w1e2r2t
Solution code
/** * * @param input * @return */ public static String getNumberedString(String input){ int length=0; char oldChar=input.toCharArray()[0]; StringBuilder stringBuilder=new StringBuilder(); int index=-1; for(char ch:input.toCharArray()){ index++; if(ch==oldChar){ length++; if(index==input.length()-1) stringBuilder.append(length).append(oldChar); }else { stringBuilder.append(length).append(oldChar); oldChar=ch; length=1; } } return stringBuilder.toString(); }
wweeeeeewwerrtt
2t2r1e2w6e2w
/** * * @param input * @return */ public static String getNumberedString(String input){ int length=0; char oldChar=input.toCharArray()[0]; StringBuilder stringBuilder=new StringBuilder(); int index=-1; for(char ch:input.toCharArray()){ index++; if(ch==oldChar){ length++; if(index==input.length()-1) stringBuilder.append(length).append(oldChar); }else { stringBuilder.append(length).append(oldChar); oldChar=ch; length=1; } } char[] charArray=stringBuilder.reverse().toString().toCharArray(); for(int i=0;i<charArray.length;i++) if(Character.isDigit(charArray[i])) swap(charArray,i,i-1); stringBuilder.delete(0,stringBuilder.length()); for(char ch:charArray) stringBuilder.append(ch); return stringBuilder.toString(); } public static void swap(char[] charArray,int firstIndex,int secondIndex){ char temp=charArray[firstIndex]; charArray[firstIndex]=charArray[secondIndex]; charArray[secondIndex]=temp; }
Write java code to get below strings as a output
wwewwerrtt
then output will be2w1e2w1e2r2t
Solution code
wweeeeeewwerrtt
then output will be2t2r1e2w6e2w
Solution code