diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..1acf042 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..31b9ffb --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# isUnique + +### Description +This repository holds my submission to an assignment for SDEV301 Systems Programming at Green River College. The purpose of this assignment was to review Github forking and provide a light introduction back into algorithms (using Java). + +`isUnique` is an algorithm that takes in a given string and returns `true` if all characters in the string are unique and `false` if they are not. + +For example: +- "cat" would return `true`, as all characters in "cat" are unique. +- "catch" would return `false`, as the character "c" is occurs twice. + +### How to Run the Project +This code can be run as-is with the IntelliJ IDEA IDE from Jetbrains, as long as [Java has been installed](https://www.oracle.com/java/technologies/downloads/) locally. diff --git a/src/Main.java b/src/Main.java index 930198c..936a69c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,47 @@ -//TIP To Run code, press or -// click the icon in the gutter. +/** + * SDEV 301: Systems Programming + * + * @author Kendrick Hang, Tien Han + * @version 1.0 + */ + +import java.util.ArrayList; + public class Main { public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); + System.out.print("Hello and welcome!"); for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . System.out.println("i = " + i); } + + //Quick Test + boolean result = isUnique("always"); + System.out.println(result); + boolean result2 = isUnique("cat"); + System.out.println(result2); + } + + //Another way to do this is to use indexOf("a") and lastIndexOf("a") + //If they both return different indexes, then it is not unique + //Another way to do this is the "inchworm" technique, where we look at a character + //And search through the rest of the string to see if it repeats + /** + * isUnique checks if all characters in a given string are unique + * @param word the given string to test + * @return true if string is unique and false if not unique + */ + public static boolean isUnique(String word) { + ArrayList characters = new ArrayList<>(); + + for (int i = 0; i < word.length(); i++) { + Character character = word.charAt(i); + if (characters.contains(character)) { + return false; + } else { + characters.add(character); + } + } + return true; } } \ No newline at end of file