From 17bc81bd8cb8719a6b4a69bd38bdf3f38a656e1d Mon Sep 17 00:00:00 2001 From: "Pedro V.R" Date: Tue, 9 Jan 2024 10:06:19 -0800 Subject: [PATCH 1/3] Added method header for isUnique --- src/Main.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Main.java b/src/Main.java index 930198c..dd9ff28 100644 --- a/src/Main.java +++ b/src/Main.java @@ -11,5 +11,14 @@ public static void main(String[] args) { // 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); + } + + public static boolean isUnique(String word) + { + return false; } } \ No newline at end of file From 29d0cbe7d65f639c4ab70f5414c1a3ad5fe092e0 Mon Sep 17 00:00:00 2001 From: "Pedro V.R" Date: Tue, 9 Jan 2024 10:11:31 -0800 Subject: [PATCH 2/3] Added method header for isUnique --- src/Main.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Main.java b/src/Main.java index dd9ff28..16321c0 100644 --- a/src/Main.java +++ b/src/Main.java @@ -17,6 +17,11 @@ public static void main(String[] args) { System.out.println(result); } + /** + * isUnique checks if all the characters in the string are unique + * @param word + * @return + */ public static boolean isUnique(String word) { return false; From 0131a12a4a4c5639a0072e70e6dd75f12d533ca5 Mon Sep 17 00:00:00 2001 From: "Pedro V.R" Date: Tue, 9 Jan 2024 11:00:39 -0800 Subject: [PATCH 3/3] Added method header for isUnique --- src/Main.java | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Main.java b/src/Main.java index 16321c0..62eafed 100644 --- a/src/Main.java +++ b/src/Main.java @@ -11,19 +11,29 @@ public static void main(String[] args) { // 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); + System.out.println("Is 'string' a unique word: " +isUnique("string")); + System.out.println("Is 'unique' a unique word: " + isUnique("unique")); } - /** - * isUnique checks if all the characters in the string are unique - * @param word - * @return - */ - public static boolean isUnique(String word) - { - return false; - } -} \ No newline at end of file + public static boolean isUnique(String word) throws StringIndexOutOfBoundsException + { + return isUnique(word, 0); + } + + public static boolean isUnique(String word, int index) throws StringIndexOutOfBoundsException + { + char letter = word.charAt(index); + for (int i = 0; i < word.length(); i++) + { + if(letter == word.charAt(i) && i != index) + { + return false; + } + } + if (letter == word.charAt((word.length()-1))) + { + return isUnique(word, index+1); + } + return true; + } + } \ No newline at end of file