Created
October 23, 2021 16:20
-
-
Save Shivani13121007/1207f21fd2844595c900d010f9577492 to your computer and use it in GitHub Desktop.
Sorting the Sentence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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