Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 47114f3

Browse files
author
cd155
committed
add 1.1 isUnique
1 parent 26b6908 commit 47114f3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/Array.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Array where
2+
3+
{-
4+
1.1
5+
Implement an algorithm to determine if a string has all unique
6+
characters. What if you cannot use additional data structures?
7+
-}
8+
isUniq :: Eq a => [a] -> Bool
9+
isUniq xs = isUniqHelper xs []
10+
11+
isUniqHelper :: Eq a => [a] -> [a] -> Bool
12+
isUniqHelper [] _ = True
13+
isUniqHelper (x:xs) dict
14+
| x `elem` dict = False
15+
| otherwise = isUniqHelper xs (x:dict)
16+
17+
-- check unique without structure
18+
isUniqBF :: Eq a => [a] -> Bool
19+
isUniqBF [] = True
20+
isUniqBF (x:xs) = isUniqBFHelper x xs && isUniqBF xs
21+
22+
-- compare an element with a list
23+
isUniqBFHelper :: Eq a => a -> [a] -> Bool
24+
isUniqBFHelper _ [] = True
25+
isUniqBFHelper c (x:xs) = (c /= x) && isUniqBFHelper c xs
26+
27+
{-
28+
isUniqBF code animation:
29+
30+
r1:
31+
abc
32+
a bc => a /= b && a /= c
33+
34+
r2:
35+
bc
36+
b c => b /= c
37+
38+
final: r1 && r2
39+
-}

0 commit comments

Comments
 (0)