From addd3ec1d96974432df05ac90d621bd9d5f4e395 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Mon, 19 Sep 2022 19:35:44 +0530 Subject: [PATCH 1/4] feat: Added Algo first unique char in a string. --- DIRECTORY.md | 1 + String/FirstUniqueCharacter.js | 21 +++++++++++++++++++++ String/test/FirstUniqueCharacter.test.js | 9 +++++++++ 3 files changed, 31 insertions(+) create mode 100644 String/FirstUniqueCharacter.js create mode 100644 String/test/FirstUniqueCharacter.test.js diff --git a/DIRECTORY.md b/DIRECTORY.md index f2d0bf4fab..2eec03b7b6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -299,6 +299,7 @@ * [CountVowels](String/CountVowels.js) * [CreatePermutations](String/CreatePermutations.js) * [DiceCoefficient](String/DiceCoefficient.js) + * [FirstUniqueCharacter](String/FirstUniqueCharacter.js) * [FormatPhoneNumber](String/FormatPhoneNumber.js) * [GenerateGUID](String/GenerateGUID.js) * [HammingDistance](String/HammingDistance.js) diff --git a/String/FirstUniqueCharacter.js b/String/FirstUniqueCharacter.js new file mode 100644 index 0000000000..b3b613c766 --- /dev/null +++ b/String/FirstUniqueCharacter.js @@ -0,0 +1,21 @@ +/** + * @function firstUniqChar + * @description Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. + * @param {String} str - The input string + * @return {Number} - The index of first unique character. + * @example firstUniqChar("javascript") => 0 + * @example firstUniqChar(""sesquipedalian"") => 3 + * @example firstUniqChar("aabb") => -1 + */ + +const firstUniqChar = (str) => { + if (typeof str !== 'string') { + throw new TypeError('Argument should be string') + } + for (let i = 0; i < str.length; ++i) { + if (str.indexOf(str[i]) === str.lastIndexOf(str[i])) return i + } + return -1 +} + +export { firstUniqChar } diff --git a/String/test/FirstUniqueCharacter.test.js b/String/test/FirstUniqueCharacter.test.js new file mode 100644 index 0000000000..b2bc465018 --- /dev/null +++ b/String/test/FirstUniqueCharacter.test.js @@ -0,0 +1,9 @@ +import { firstUniqChar } from '../FirstUniqueCharacter' + +describe('firstUniqChar', () => { + it('locates the index of first unique character in the string', () => { + expect(firstUniqChar('javascript')).toEqual(0) + expect(firstUniqChar('sesquipedalian')).toEqual(3) + expect(firstUniqChar('aabb')).toEqual(-1) + }) +}) From c446fbc4a15a55bf0c85aaeb53b2a1ed5426c3f7 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Mon, 19 Sep 2022 23:42:28 +0530 Subject: [PATCH 2/4] Optimised algo to linear time complexity --- String/FirstUniqueCharacter.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/String/FirstUniqueCharacter.js b/String/FirstUniqueCharacter.js index b3b613c766..804944e30b 100644 --- a/String/FirstUniqueCharacter.js +++ b/String/FirstUniqueCharacter.js @@ -1,6 +1,6 @@ /** * @function firstUniqChar - * @description Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. + * @description Given a string str, find the first non-repeating character in it and return its index. If it does not exist, return -1. * @param {String} str - The input string * @return {Number} - The index of first unique character. * @example firstUniqChar("javascript") => 0 @@ -12,8 +12,12 @@ const firstUniqChar = (str) => { if (typeof str !== 'string') { throw new TypeError('Argument should be string') } - for (let i = 0; i < str.length; ++i) { - if (str.indexOf(str[i]) === str.lastIndexOf(str[i])) return i + const count = new Map() + for (const char of str) { + count[char] ? count[char]++ : count[char] = 1 + } + for (let i = 0; i < str.length; i++) { + if (count[str[i]] === 1) return i } return -1 } From fd7e29e5e159c33694590c5eb141609accdf0e47 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Mon, 19 Sep 2022 23:44:29 +0530 Subject: [PATCH 3/4] removed double quotes --- String/FirstUniqueCharacter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/FirstUniqueCharacter.js b/String/FirstUniqueCharacter.js index 804944e30b..2974f08033 100644 --- a/String/FirstUniqueCharacter.js +++ b/String/FirstUniqueCharacter.js @@ -4,7 +4,7 @@ * @param {String} str - The input string * @return {Number} - The index of first unique character. * @example firstUniqChar("javascript") => 0 - * @example firstUniqChar(""sesquipedalian"") => 3 + * @example firstUniqChar("sesquipedalian") => 3 * @example firstUniqChar("aabb") => -1 */ From 0bd36044318529e3c075ff2e828f35643c1b8e7d Mon Sep 17 00:00:00 2001 From: 10kartik Date: Tue, 20 Sep 2022 00:21:41 +0530 Subject: [PATCH 4/4] Review changes: if-else logic --- String/FirstUniqueCharacter.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/String/FirstUniqueCharacter.js b/String/FirstUniqueCharacter.js index 2974f08033..1bacd83070 100644 --- a/String/FirstUniqueCharacter.js +++ b/String/FirstUniqueCharacter.js @@ -13,8 +13,13 @@ const firstUniqChar = (str) => { throw new TypeError('Argument should be string') } const count = new Map() + for (const char of str) { - count[char] ? count[char]++ : count[char] = 1 + if (!count[char]) { + count[char] = 1 + } else { + count[char]++ + } } for (let i = 0; i < str.length; i++) { if (count[str[i]] === 1) return i