-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55120a0
commit 78def55
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/page-16/1657. Determine if Two Strings Are Close/closeStrings.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { closeStrings } from './closeStrings'; | ||
|
||
describe('1657. Determine if Two Strings Are Close', () => { | ||
test('closeStrings', () => { | ||
expect(closeStrings('abc', 'bca')).toBe(true); | ||
expect(closeStrings('a', 'aa')).toBe(false); | ||
expect(closeStrings('cabbba', 'abbccc')).toBe(true); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
src/page-16/1657. Determine if Two Strings Are Close/closeStrings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
type CloseStrings = (word1: string, word2: string) => boolean; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const closeStrings: CloseStrings = (word1, word2) => { | ||
if (word1.length !== word2.length) return false; | ||
|
||
const getCharFrequency = (word: string) => { | ||
const frequencyMap = new Map<string, number>(); | ||
|
||
for (const char of word) { | ||
frequencyMap.set(char, (frequencyMap.get(char) || 0) + 1); | ||
} | ||
|
||
return frequencyMap; | ||
}; | ||
|
||
const frequency1 = getCharFrequency(word1); | ||
const frequency2 = getCharFrequency(word2); | ||
|
||
const keys1 = Array.from(frequency1.keys()); | ||
const keys2 = Array.from(frequency2.keys()); | ||
|
||
// Both strings must have the same set of unique characters | ||
if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key))) { | ||
return false; | ||
} | ||
|
||
const values1 = Array.from(frequency1.values()).sort((a, b) => a - b); | ||
const values2 = Array.from(frequency2.values()).sort((a, b) => a - b); | ||
|
||
// Both strings must have the same sorted frequencies of characters | ||
return values1.every((value, index) => value === values2[index]); | ||
}; |