From 93a413b2f652f960945270099fb3ec8049390b0c Mon Sep 17 00:00:00 2001 From: Lvcios Date: Mon, 10 Jan 2022 21:55:10 -0600 Subject: [PATCH 1/3] linq solution for chapter 1 exercise 1 --- .../Code/Chapter 1/Question1_1.cs | 13 ++++ Tests/Chapter 1/Test1_1.cs | 70 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/CrackingTheCodingInterview/Code/Chapter 1/Question1_1.cs b/CrackingTheCodingInterview/Code/Chapter 1/Question1_1.cs index b2a892a..b568b1f 100644 --- a/CrackingTheCodingInterview/Code/Chapter 1/Question1_1.cs +++ b/CrackingTheCodingInterview/Code/Chapter 1/Question1_1.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; namespace Code { @@ -49,5 +50,17 @@ public static bool AreAllCharactersUniqueNoAdditionalMemory(string input) return true; } + + // Space: O(n) + // Time: O(1) + public static bool AreAllCharactersUniqueLinQSolution(string input) + { + if (string.IsNullOrEmpty(input)) + { + return true; + } + + return input.Distinct().ToArray().Length == input.Length; + } } } diff --git a/Tests/Chapter 1/Test1_1.cs b/Tests/Chapter 1/Test1_1.cs index b1866a0..91ff0fc 100644 --- a/Tests/Chapter 1/Test1_1.cs +++ b/Tests/Chapter 1/Test1_1.cs @@ -34,6 +34,20 @@ public void ImplementationTwo_NoDuplicates_ReturnsTrue() Assert.AreEqual(expected, actual); } + [TestMethod] + public void ImplementationThree_NoDuplicates_ReturnsTrue() + { + // Arrange + const bool expected = true; + const string input = "abc"; + + // Act + var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input); + + // Assert + Assert.AreEqual(expected, actual); + } + [TestMethod] public void ImplementationOne_Duplicates_ReturnsFalse() { @@ -62,6 +76,20 @@ public void ImplementationTwo_Duplicates_ReturnsFalse() Assert.AreEqual(expected, actual); } + [TestMethod] + public void ImplementationThree_Duplicates_ReturnsFalse() + { + // Arrange + const bool expected = false; + const string input = "aba"; + + // Act + var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input); + + // Assert + Assert.AreEqual(expected, actual); + } + [TestMethod] public void ImplementationOne_CasingDifference_ReturnsTrue() { @@ -90,6 +118,20 @@ public void ImplementationTwo_CasingDifference_ReturnsTrue() Assert.AreEqual(expected, actual); } + [TestMethod] + public void ImplementationThree_CasingDifference_ReturnsTrue() + { + // Arrange + const bool expected = true; + const string input = "Aa"; + + // Act + var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input); + + // Assert + Assert.AreEqual(expected, actual); + } + [TestMethod] public void ImplementationOne_NullString_ReturnsTrue() { @@ -118,6 +160,20 @@ public void ImplementationTwo_NullString_ReturnsTrue() Assert.AreEqual(expected, actual); } + [TestMethod] + public void ImplementationThree_NullString_ReturnsTrue() + { + // Arrange + const bool expected = true; + const string input = null; + + // Act + var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input); + + // Assert + Assert.AreEqual(expected, actual); + } + [TestMethod] public void ImplementationOne_EmptyString_ReturnsTrue() { @@ -145,5 +201,19 @@ public void ImplementationTwo_EmptyString_ReturnsTrue() // Assert Assert.AreEqual(expected, actual); } + + [TestMethod] + public void ImplementationThree_EmptyString_ReturnsTrue() + { + // Arrange + const bool expected = true; + var input = string.Empty; + + // Act + var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input); + + // Assert + Assert.AreEqual(expected, actual); + } } } From d7004437c9972df9ad19fa25ac1226bde71bea5c Mon Sep 17 00:00:00 2001 From: Lvcios Date: Tue, 11 Jan 2022 20:55:01 -0600 Subject: [PATCH 2/3] solution by summing --- .../Code/Chapter 1/Question1_2.cs | 25 +++++++++++++++++++ Tests/Chapter 1/Test1_2.cs | 4 +++ 2 files changed, 29 insertions(+) diff --git a/CrackingTheCodingInterview/Code/Chapter 1/Question1_2.cs b/CrackingTheCodingInterview/Code/Chapter 1/Question1_2.cs index 4993809..d63f336 100644 --- a/CrackingTheCodingInterview/Code/Chapter 1/Question1_2.cs +++ b/CrackingTheCodingInterview/Code/Chapter 1/Question1_2.cs @@ -77,5 +77,30 @@ public static bool AreStringsPermutationNoSort(string string1, string string2) return true; } + + // Space: O(N) + // Time: O(N) + public static bool AreStringPermutationsIntValues(string string1, string string2) + { + if (string.IsNullOrEmpty(string1) || string.IsNullOrEmpty(string2)) + { + throw new ArgumentException("Input strings cannot be null or empty"); + } + + if (string1.Length != string2.Length) + { + return false; + } + + int summyString1 = 0; + int summyString2 = 0; + for (int i = 0; i < string1.Length; i++) + { + summyString1 = summyString1 + string1[i]; + summyString2 = summyString2 + string2[i]; + } + + return summyString1 == summyString2; + } } } diff --git a/Tests/Chapter 1/Test1_2.cs b/Tests/Chapter 1/Test1_2.cs index 20f3a69..7ca13d6 100644 --- a/Tests/Chapter 1/Test1_2.cs +++ b/Tests/Chapter 1/Test1_2.cs @@ -13,10 +13,12 @@ public void BasicTest() // Permutations ValidateResult("abc", "abc", true); ValidateResult("abc", "bca", true); + ValidateResult("abcdefg", "bcadefg", true); // Not permutations ValidateResult("abc", "abca", false); ValidateResult("abc", "xyz", false); + ValidateResult("abc", "axa", false); } [TestMethod] @@ -59,12 +61,14 @@ private static void ValidateResult(string str1, string str2, bool expectedResult { Assert.AreEqual(expectedResult, Question1_2.AreStringsPermutation(str1, str2)); Assert.AreEqual(expectedResult, Question1_2.AreStringsPermutationNoSort(str1, str2)); + Assert.AreEqual(expectedResult, Question1_2.AreStringPermutationsIntValues(str1, str2)); } private static void ValidateResult(string str1, string str2, Type expectedException) { TestHelpers.AssertExceptionThrown(() => { Question1_2.AreStringsPermutation(str1, str2); }, expectedException); TestHelpers.AssertExceptionThrown(() => { Question1_2.AreStringsPermutationNoSort(str1, str2); }, expectedException); + TestHelpers.AssertExceptionThrown(() => { Question1_2.AreStringPermutationsIntValues(str1, str2); }, expectedException); } } } From 232b86daf57e9346c7c71f5e84f205ce3eeeb835 Mon Sep 17 00:00:00 2001 From: Lvcios Date: Thu, 13 Jan 2022 22:43:15 -0600 Subject: [PATCH 3/3] another O(n) solution for Question1_3 --- .../Code/Chapter 1/Question1_3.cs | 34 +++++++++++++++++++ Tests/Chapter 1/Test1_3.cs | 4 +++ 2 files changed, 38 insertions(+) diff --git a/CrackingTheCodingInterview/Code/Chapter 1/Question1_3.cs b/CrackingTheCodingInterview/Code/Chapter 1/Question1_3.cs index c424d52..e800b9b 100644 --- a/CrackingTheCodingInterview/Code/Chapter 1/Question1_3.cs +++ b/CrackingTheCodingInterview/Code/Chapter 1/Question1_3.cs @@ -50,5 +50,39 @@ public static void ReplaceSpaces(char[] inputString, int length) } } } + + // Space: O(N) + // Time: O(N) + public static string ReplaceSpaces(char[] inputString) + { + if (inputString == null) + { + throw new ArgumentNullException(nameof(inputString), "Value cannot be null"); + } + + char[] outputString = new char[inputString.Length * 3]; + int outputIndex = 0; + for (int i = 0; i < inputString.Length; i++) + { + if (char.IsWhiteSpace(inputString[i])) + { + outputString[outputIndex++] = '%'; + outputString[outputIndex++] = '2'; + outputString[outputIndex++] = '0'; + } + else + { + outputString[outputIndex++] = inputString[i]; + } + } + + char[] finalString = new char[outputIndex]; + for (int i = 0; i < outputIndex; i++) + { + finalString[i] = outputString[i]; + } + + return new string(finalString); + } } } diff --git a/Tests/Chapter 1/Test1_3.cs b/Tests/Chapter 1/Test1_3.cs index 5437ae2..1393251 100644 --- a/Tests/Chapter 1/Test1_3.cs +++ b/Tests/Chapter 1/Test1_3.cs @@ -40,6 +40,8 @@ public void InvalidInputsTest() // Null input string str = null; TestHelpers.AssertExceptionThrown(() => { Question1_3.ReplaceSpaces(str, 0); }, typeof(ArgumentNullException)); + + TestHelpers.AssertExceptionThrown(() => { Question1_3.ReplaceSpaces(str); }, typeof(ArgumentNullException)); } private static void ValideResult(string input, string expectedResult, int? inputLength = null) @@ -58,6 +60,8 @@ private static void ValideResult(string input, string expectedResult, int? input { Assert.AreEqual(expectedResult[i], str[i]); } + + Assert.AreEqual(Question1_3.ReplaceSpaces(str), expectedResult); } } }