diff --git a/Week 08/id_161/ReverWords b/Week 08/id_161/ReverWords new file mode 100644 index 000000000..27bf71b88 --- /dev/null +++ b/Week 08/id_161/ReverWords @@ -0,0 +1,18 @@ +class Solution { + public String reverseOnlyLetters(String S) { + Stack letters = new Stack(); + for (char c: S.toCharArray()) + if (Character.isLetter(c)) + letters.push(c); + + StringBuilder ans = new StringBuilder(); + for (char c: S.toCharArray()) { + if (Character.isLetter(c)) + ans.append(letters.pop()); + else + ans.append(c); + } + + return ans.toString(); + } +} diff --git a/Week 08/id_161/UniqueChars b/Week 08/id_161/UniqueChars new file mode 100644 index 000000000..479ea0860 --- /dev/null +++ b/Week 08/id_161/UniqueChars @@ -0,0 +1,18 @@ +class Solution { + public int firstUniqChar(String s) { + HashMap count = new HashMap(); + int n = s.length(); + // build hash map : character and how often it appears + for (int i = 0; i < n; i++) { + char c = s.charAt(i); + count.put(c, count.getOrDefault(c, 0) + 1); + } + + // find the index + for (int i = 0; i < n; i++) { + if (count.get(s.charAt(i)) == 1) + return i; + } + return -1; + } +} \ No newline at end of file