C# O(n) runtime with regex


  • 0
    G
        public string ReverseWords(string s) {
            string[] words = Regex.Split(s.Trim(), @"\s+");
            StringBuilder sb = new StringBuilder();
            for (int i = words.Length - 1; i >= 0; i--) {
                sb.Append(words[i].Trim() + " ");
            }
            return sb.ToString().Trim();
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.