Please Help! C# strange WA on "a b"


  • 0
    D
    public class Solution {
        public string ReverseWords(string s) {
            var sc = s.Split(new char[]{' '}).Select(p=>p.Trim()).ToArray();
            Array.Reverse(sc);
            return string.Join(" ", sc).Trim();
        }
    }
    

    gettting:

    Input:
    " a b "
    Output:
    "b a"
    Expected:
    "b a"

  • 1
    H
    s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
    

  • 0
    H

    You can also use

    string[] splittedString = s.Split(' ').Where(h => !string.IsNullOrWhiteSpace(h)).Select(p=>p.Trim()).ToArray();

Log in to reply
 

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