Skip to content

Commit

Permalink
Merge pull request #8 from ele7ija/master
Browse files Browse the repository at this point in the history
My Q1.1 and Q1.3 solutions
  • Loading branch information
krlv authored Oct 21, 2021
2 parents c701e0b + 594f99f commit a4da997
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ch01/01_is_unique.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch01

import "strings"

// IsUnique determine if a string has all unique chars
// This solution is based on HashTable (Go map) data structure
func IsUnique(s string) bool {
Expand Down Expand Up @@ -30,3 +32,20 @@ func IsUniqueVanilla(s string) bool {

return true
}

// IsUniqueBits determines if a string has all unique chars (only works for ASCII)
// This solution doesn't utilize any additional data structures and has O(n) time complexity
func IsUniqueBits(s string) bool {

s = strings.ToLower(s)
var vector int32
for _, rune := range s {
index := rune - 'a'
mask := int32(1 << index)
if (vector & mask) == mask {
return false
}
vector = vector | mask
}
return true
}
10 changes: 10 additions & 0 deletions ch01/01_is_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ func TestIsUniqueVanilla(t *testing.T) {
t.Error("'uniqke' is a unique string")
}
}

func TestIsUniqueBits(t *testing.T) {
if IsUniqueBits("unique") {
t.Error("'unique' is not a unique string")
}

if !IsUniqueBits("uniqke") {
t.Error("'uniqke' is a unique string")
}
}
10 changes: 10 additions & 0 deletions ch01/03_urlify.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch01

import "regexp"

// URLifyn returns the string with HTML encoded spacese
// Expects real string length as a second parameter
func URLifyn(s string, n int) string {
Expand Down Expand Up @@ -55,3 +57,11 @@ func URLify(s string) string {

return string(runes)
}

// UrlifyRegex returns the s string with HTML-encoded space characters.
func UrlifyRegex(s string) string {

exp := regexp.MustCompile(" ")
return exp.ReplaceAllString(s, "%20")

}
7 changes: 7 additions & 0 deletions ch01/03_urlify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ func TestUrlify(t *testing.T) {
t.Errorf("Expected \"%s\" after ULRification, got \"%s\"", expected, actual)
}
}

func TestUrlifyRegex(t *testing.T) {
actual, expected := UrlifyRegex("slug url"), "slug%20url"
if actual != expected {
t.Errorf("Expected \"%s\" after ULRification, got \"%s\"", expected, actual)
}
}

0 comments on commit a4da997

Please sign in to comment.