Sorting a Sentence (LeetCode 1859) | Solution with examples | Detail explanation | Study Algorithms

preview_player
Показать описание
Whenever you are given a sentence string, the best way to iterate over each word is to take advantage of the space (“ “) character. This will help you to separate out different words. We take the help of this idea to solve this easy problem on LeetCode. Watch the video to understand how a HashSet can help you optimize your solution with time. All of this with examples and a dry-run of code in JAVA

Chapters:
00:00 - Intro
00:54 - Problem statement and description
03:00 - How to approach the problem?
04:36 - Solving for efficiency
07:04 - Dry-run of Code
09:33 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

🎥 My Recording Gear:

💻 Get Social 💻

#hashset #leetcode #algorithms
Рекомендации по теме
Комментарии
Автор

Thank you so much for nice explanation

LuckyAndDad
Автор

Alternate solution
class Solution {
public String sortSentence(String s) {
String[] words = s.split(" ");
String[] result = new String[words.length];
for (String word : words) {
int pos = (int) (word.charAt(word.length() - 1) - '0');
result[pos - 1] = word.substring(0, word.length() - 1);
}
return String.join(" ", result);
}
}

ahulanand
Автор

Thank you so much sir for bringing great content for us.

mohammedilyas
Автор

var sortSentence = function(s) {
let arr = s.split(' ');
arr.sort((a, b) => a[a.length-1] - b[b.length-1]);
return arr.join(' ').replaceAll(/\d/g, "");
};
TC: O(nlogN) SC: O(N)

soraubhsahu
Автор

Sir hashmap do not print data in sorting order of keys so this will create a problem i think

bipinsingh
Автор

S=input().strip().split()
n=len(S)
res=[""]*n

for i in S:
i=int(i[-1])
res[i-1]=i[:-1]

print(" ".join(res))


Sir, this is my python code.. but I am not getting how to take input of that sentence.. can u explain me

kalpakchincholkar
Автор

Please Nikhil Change your channel name With previous one because Study Algorithm describe your work. I have purchase a paid course instead of watching live class I always prefer to watch your video

deepika