From f0abd96402cdb6d3d6eb8a2664fe55c695ec4942 Mon Sep 17 00:00:00 2001 From: Carson <92652800+carsonSgit@users.noreply.github.com> Date: Fri, 6 Sep 2024 12:31:42 -0400 Subject: [PATCH 1/3] feat: Add q151_solution.cs Not optimal for sure... --- solutions/q151/q151_solution.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solutions/q151/q151_solution.cs diff --git a/solutions/q151/q151_solution.cs b/solutions/q151/q151_solution.cs new file mode 100644 index 0000000..645f32f --- /dev/null +++ b/solutions/q151/q151_solution.cs @@ -0,0 +1,26 @@ +public class Solution { + public string ReverseWords(string s) { + List words = s.Split(' ').ToList(); + + words.Reverse(); + StringBuilder sb = new StringBuilder(); + foreach(var w in words){ + sb.Append(w + " "); + } + + StringBuilder test = new StringBuilder(); + for(int i=0; i< sb.Length -1; i++){ + char currentChar = sb[i]; + if(char.IsWhiteSpace(currentChar)){ + if (i == sb.Length - 1 || !char.IsWhiteSpace(sb[i + 1])) { + test.Append(currentChar); + } + } + else { + test.Append(currentChar); + } + } + + return test.ToString().Trim(); + } +} From 32533cdfe0bd98a807128cec01f1bd2c7cd03afd Mon Sep 17 00:00:00 2001 From: Carson <92652800+carsonSgit@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:01:25 -0400 Subject: [PATCH 2/3] feat: Add q151_solution.kt --- solutions/q151/q151_solution.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solutions/q151/q151_solution.kt diff --git a/solutions/q151/q151_solution.kt b/solutions/q151/q151_solution.kt new file mode 100644 index 0000000..efdb1e3 --- /dev/null +++ b/solutions/q151/q151_solution.kt @@ -0,0 +1,19 @@ +class Solution { + fun reverseWords(s: String): String { + // s.split ( ' ' ) -> get rid of all whitespace + // map the s split variables to an iterable object, trimming any extra whitespace + // ensure no empty strings get added to the array + var wordArray = s.split(" ").map{it.toString().trim()}.filter{it != ""} + + var answer = "" + // iterate through the string array created in line one, all the way down to var 1 (decreasing order) + for(i in wordArray.size - 1 downTo 1) { + answer += wordArray[i] + answer += " " + } + + // add in the ignored first array item + answer += wordArray[0] + return answer + } +} From 50156302b4026e82a952c9e0446f71612c05fd6a Mon Sep 17 00:00:00 2001 From: Carson <92652800+carsonSgit@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:07:58 -0400 Subject: [PATCH 3/3] feat: Add q151_guide.md --- solutions/q151/q151_guide.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 solutions/q151/q151_guide.md diff --git a/solutions/q151/q151_guide.md b/solutions/q151/q151_guide.md new file mode 100644 index 0000000..93c5038 --- /dev/null +++ b/solutions/q151/q151_guide.md @@ -0,0 +1,17 @@ +# Guide for Reverse Words in a String + +## Takeaways +> I love the short form that kotlin offers. StringBuilders are very easy to forget. + +## Guide (`Kotlin`) +1. Create an `array` based off of the `s` parameter value. + - Split at any white spaces (" "). + - Map the array to convert all iterable values to strings and trim the excess white space. + - Filter out any empty strings. +2. Declare your default answer variable to be an empty string. +3. Enter a for loop with the following condition: Iterate through the string array created in line one, all the way down to var 1 (decreasing order) + - Append each iteration of the `wordArray[i]` to your `answer` string variable. + - Append a whitespace to the answer variable. +4. Before returning your `answer`, add the first ignored array item + - We use a decreasing order and stop at 1 to avoid the extra operation of adding a whitespace to the end of the answer. +5. Return your `answer` string.