leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

reverse-words-in-a-string.dart (820B)


      1 //Given a string s reverse each word within it without changing
      2 //the order that the words appear. Then return the new string.
      3 //Input: This is an example
      4 //Output: sihT si na elpmaxe
      5 //My solution splits the words into their own strings
      6 //then reverses them and places them into the output string
      7 //appending on a space at the end that was removed when splitting
      8 //the initial string.
      9 //Time: 985ms Beats: 19.51%
     10 //Memory: 175.4MB Beats: 29.27%
     11 
     12 class Solution {
     13   String reverseWords(String s) {
     14       List<String> words = s.split(" ");
     15       String ret_str = "";
     16       for(int i = 0 ; i < words.length ; ++i){
     17         for(int x = words[i].length - 1 ; x >= 0 ; --x){
     18           ret_str += words[i][x];
     19         }
     20         if(i + 1 < words.length){
     21           ret_str += " ";
     22         }
     23       }
     24       return ret_str;
     25   }
     26 }