Skip to content

Instantly share code, notes, and snippets.

@Shivani13121007
Created October 23, 2021 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shivani13121007/1207f21fd2844595c900d010f9577492 to your computer and use it in GitHub Desktop.
Save Shivani13121007/1207f21fd2844595c900d010f9577492 to your computer and use it in GitHub Desktop.
Sorting the Sentence
class Solution {
public String sortSentence(String s) {
String[] arr = s.split(" ");
String[] res = new String[arr.length];
String fa = "";
for(int i=0;i<arr.length;i++)
{
String str = arr[i];
String toAdd = "";
for(int j=0;j<str.length();j++)
{
char ch = str.charAt(j);
if(Character.isDigit(ch))
{
int idx = ch - '0' -1;
res[idx] = toAdd;
}
else{
toAdd+=ch;
}
}
}
for(int i=0;i<res.length;i++)
{
fa+=(i==res.length - 1)?res[i]:res[i]+" ";
}
return fa;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment