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

Q4 07 Build order. #8

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5cfeb01
Use abstract base Question class
xavierjohn Mar 21, 2017
9f42546
Use hashset to check if characters in a string are unique
xavierjohn Mar 21, 2017
ee7cee1
Use dictionary to check permutation
xavierjohn Mar 21, 2017
56e7977
URLify
xavierjohn Mar 21, 2017
214f49a
Bug fix Palindrome Permutation
xavierjohn Mar 22, 2017
c8a0b76
Converted to .net core
xavierjohn Mar 27, 2017
f3463f3
Check balanced tree
xavierjohn Apr 24, 2017
bfaaf76
16.19 Pond size
xavierjohn Apr 25, 2017
3a14a9a
Create Minimal BST from Sorted Unique Array
xavierjohn Apr 25, 2017
58d964d
Replace value In Immutable Tree
xavierjohn Apr 25, 2017
1e72225
Boggle
xavierjohn Apr 26, 2017
0970afc
Q16_02_Word_Frequence
xavierjohn Apr 26, 2017
2401d85
Q16_04_Tic_Tac_Toe CQRS style
xavierjohn Apr 26, 2017
a387a93
Q16_06 Smallest Difference between pair in two array.
xavierjohn Apr 26, 2017
78c0a48
Convert Int to English
xavierjohn Apr 27, 2017
f74dcde
Q4_03_List_of_Depths
xavierjohn Apr 27, 2017
c10bd58
Is Balanced BST
xavierjohn Apr 27, 2017
5fec3a6
Is Valid BST
xavierjohn Apr 27, 2017
80fa5f6
Successor Node.
xavierjohn Apr 27, 2017
7946670
Lowest Common Ancestor with none BST.
xavierjohn Apr 27, 2017
d34c32c
Check Sub Tree
xavierjohn Apr 27, 2017
87302b8
Use dictionary in Trie Node instead of List
xavierjohn Apr 28, 2017
ea11caf
Merge
xavierjohn Sep 14, 2017
05c6170
Given a binary search tree with distinct elements,
xavierjohn Sep 25, 2017
4ba950e
merge
xavierjohn Sep 25, 2017
2b7df9e
Merge remote-tracking branch 'refs/remotes/careercup/master'
xavierjohn Sep 26, 2017
b772439
Q4 07 Build order.
xavierjohn Sep 27, 2017
6d5a732
minor update
xavierjohn Sep 28, 2017
d73e4d6
Label the rotate matrix more intuitively.
xavierjohn Sep 28, 2017
08369ea
Update code to use a multidimensional array for matrix instead of jag…
xavierjohn Sep 28, 2017
f5eeb23
Switch to .net core 2.0
xavierjohn Jul 24, 2018
8031956
Chapter 17, Question 12 BiNode
xavierjohn Oct 21, 2018
216ae65
BiNode 2nd solution.
xavierjohn Oct 22, 2018
1799d61
BiNode Solution 3
xavierjohn Oct 22, 2018
2faa43d
Add unit test for IsUnique
xavierjohn Jan 9, 2019
e6ca765
minor update
xavierjohn Jul 22, 2021
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
2 changes: 1 addition & 1 deletion Ch 01. Arrays and Strings/Ch 01. Arrays and Strings.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Chapter01</RootNamespace>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,113 +1,117 @@
using ctci.Contracts;
using System;

namespace Chapter01
{
public class Q1_05_One_Away_A : Question
{
public static bool OneEditReplace(String s1, String s2)
{
bool foundDifference = false;
for (int i = 0; i < s1.Length; i++)
{
if (s1[i] != s2[i])
{
if (foundDifference)
{
return false;
}

foundDifference = true;
}
}
return true;
}

/* Check if you can insert a character into s1 to make s2. */

public static bool OneEditInsert(String s1, String s2)
{
int index1 = 0;
int index2 = 0;
while (index2 < s2.Length && index1 < s1.Length)
{
if (s1[index1] != s2[index2])
{
if (index1 != index2)
{
return false;
}
index2++;
}
else {
index1++;
index2++;
}
}
return true;
}

public static bool OneEditAway(String first, String second)
{
if (first.Length == second.Length)
{
return OneEditReplace(first, second);
}
else if (first.Length + 1 == second.Length)
{
return OneEditInsert(first, second);
}
else if (first.Length - 1 == second.Length)
{
return OneEditInsert(second, first);
}
return false;
}

public static bool OneEditAway2(String first, String second)
{
/* Length checks. */
if (Math.Abs(first.Length - second.Length) > 1)
{
return false;
}

/* Get shorter and longer string.*/
String s1 = first.Length < second.Length ? first : second;
String s2 = first.Length < second.Length ? second : first;

int index1 = 0;
int index2 = 0;
bool foundDifference = false;
while (index2 < s2.Length && index1 < s1.Length)
{
if (s1[index1] != s2[index2])
{
/* Ensure that this is the first difference found.*/
if (foundDifference) return false;
foundDifference = true;
if (s1.Length == s2.Length)
{ // On replace, move shorter pointer
index1++;
}
}
else {
index1++; // If matching, move shorter pointer
}
index2++; // Always move pointer for longer string
}
return true;
}

public override void Run()
{
String a = "pse";
String b = "pale";
bool isOneEdit = OneEditAway(a, b);
Console.WriteLine("{0}, {1}: {2}", a, b, isOneEdit);

bool isOneEdit2 = OneEditAway2(a, b);
Console.WriteLine("{0}, {1}: {2}", a, b, isOneEdit2);
}
}
using ctci.Contracts;
using System;

namespace Chapter01
{
public class Q1_05_One_Edit_Away : Question
{
public override void Run()
{
String a = "pse";
String b = "pale";
var c = "ple";
var d = "pele";
Console.WriteLine($"{a}, {b}: {OneEditAway(a, b)} / {OneEditAwayBook(a, b)}");
Console.WriteLine($"{b}, {c}: {OneEditAway(b, c)} / {OneEditAwayBook(b, c)}");
Console.WriteLine($"{b}, {d}: {OneEditAway(b, d)} / {OneEditAwayBook(b, d)}");


}
public static bool OneEditReplace(String s1, String s2)
{
bool foundDifference = false;
for (int i = 0; i < s1.Length; i++)
{
if (s1[i] != s2[i])
{
if (foundDifference)
{
return false;
}

foundDifference = true;
}
}
return true;
}

/* Check if you can insert a character into s1 to make s2. */

public static bool OneEditInsert(String s1, String s2)
{
int index1 = 0;
int index2 = 0;
while (index2 < s2.Length && index1 < s1.Length)
{
if (s1[index1] != s2[index2])
{
if (index1 != index2)
{
return false;
}
index2++;
}
else
{
index1++;
index2++;
}
}
return true;
}

public static bool OneEditAway(String first, String second)
{
if (first.Length == second.Length)
{
return OneEditReplace(first, second);
}
else if (first.Length + 1 == second.Length)
{
return OneEditInsert(first, second);
}
else if (first.Length - 1 == second.Length)
{
return OneEditInsert(second, first);
}
return false;
}

public static bool OneEditAwayBook(String first, String second)
{
/* Length checks. */
if (Math.Abs(first.Length - second.Length) > 1)
{
return false;
}

/* Get shorter and longer string.*/
String shorter = first.Length < second.Length ? first : second;
String longer = first.Length < second.Length ? second : first;

int indexS = 0;
int indexL = 0;
bool foundDifference = false;
while (indexL < longer.Length && indexS < shorter.Length)
{
if (shorter[indexS] != longer[indexL])
{
/* Ensure that this is the first difference found.*/
if (foundDifference) return false;
foundDifference = true;
if (shorter.Length == longer.Length)
{ // On replace, move shorter pointer
indexS++;
}
}
else
{
indexS++; // If matching, move shorter pointer
}
indexL++; // Always move pointer for longer string
}
return true;
}

}
}
22 changes: 7 additions & 15 deletions Ch 01. Arrays and Strings/Q1_01_Is_Unique.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
using ctci.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Chapter01
{
public class Q1_01_Is_Unique : Question
{
private bool IsUniqueChars(string str)
public bool IsUniqueChars(string str)
{
if (str.Length > 256)
{
return false;
}
throw new ArgumentException("String has to be less than 256 characters");

var checker = 0;
for (var i = 0; i < str.Length; i++)
{
var val = str[i] - 'a';

if ((checker & (1 << val)) > 0)
{
return false;
}
if ((checker & (1 << val)) > 0) return false;
checker |= (1 << val);
}

return true;
}

private bool IsUniqueChars2(String str)
public bool IsUniqueChars2(String str)
{
var hashset = new HashSet<char>();
foreach(var c in str)
foreach (var c in str)
{
if (hashset.Contains(c)) return false;
hashset.Add(c);
}

return true;
}

public override void Run()
{
string[] words = { "abcde", "hello", "apple", "kite", "padle" };

foreach (var word in words)
foreach (var word in new string[] { "abcde", "hello", "apple", "kite", "padle" })
{
Console.WriteLine(word + ": " + IsUniqueChars(word) + " " + IsUniqueChars2(word));
}
Expand Down
9 changes: 2 additions & 7 deletions Ch 01. Arrays and Strings/Q1_02_Check_Permutation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ctci.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Chapter01
{
Expand All @@ -9,27 +10,21 @@ public class Q1_02_Check_Permutation : Question
private bool IsPermutation(string original, string valueToTest)
{
if (original.Length != valueToTest.Length)
{
return false;
}

var originalAsArray = original.ToCharArray();
Array.Sort(originalAsArray);
original = new string(originalAsArray);

var valueToTestAsArray = valueToTest.ToCharArray();
Array.Sort(valueToTestAsArray);
valueToTest = new string(valueToTestAsArray);

return original.Equals(valueToTest);
return originalAsArray.SequenceEqual(valueToTestAsArray);
}

private bool IsPermutation2(string original, string valueToTest)
{
if (original.Length != valueToTest.Length)
{
return false;
}

var letterCount = new Dictionary<char, int>();

Expand Down
32 changes: 17 additions & 15 deletions Ch 01. Arrays and Strings/Q1_07_Rotate_Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,44 @@ namespace Chapter01
{
public class Q1_07_Rotate_Matrix : Question
{
private void Rotate(int[][] matrix, int n)
private void Rotate(int[,] matrix, int n)
{
for (var layer = 0; layer < n / 2; ++layer)
{
var first = layer;
var last = n - 1 - layer;

for (var i = first; i < last; ++i)
{
var offset = i - first;
var top = matrix[first][i]; // save top
var top = matrix[first, i]; // save top, left

// left -> top
matrix[first][i] = matrix[last - offset][first];
// top, left <- bottom, left
matrix[first, i] = matrix[last - offset, first];

// bottom -> left
matrix[last - offset][first] = matrix[last][last - offset];
// bottom, left <- bottom, right
matrix[last - offset, first] = matrix[last, last - offset];

// right -> bottom
matrix[last][last - offset] = matrix[i][last];
// bottom, right <- top, right
matrix[last, last - offset] = matrix[i, last];

// top -> right
matrix[i][last] = top; // right <- saved top
// top, right <- top
matrix[i, last] = top;
}
}
}

public override void Run()
{
const int size = 3;

var matrix = AssortedMethods.RandomMatrix(size, size, 0, 9);
var matrix = new[,]
{
{1,2,3 },
{4,5,6 },
{7,8,9 }
};

AssortedMethods.PrintMatrix(matrix);

Rotate(matrix, size);
Rotate(matrix, matrix.GetLength(0));
Console.WriteLine();
AssortedMethods.PrintMatrix(matrix);
}
Expand Down
Loading