Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q151 #54

Merged
merged 3 commits into from
Sep 6, 2024
Merged

Q151 #54

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions solutions/q151/q151_guide.md
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 26 additions & 0 deletions solutions/q151/q151_solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Solution {
public string ReverseWords(string s) {
List<string> 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();
}
}
19 changes: 19 additions & 0 deletions solutions/q151/q151_solution.kt
Original file line number Diff line number Diff line change
@@ -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
}
}