-
Notifications
You must be signed in to change notification settings - Fork 25
Feat: added content to Lesson13.java - James Capparell #439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5629699
Feat: added content to Lesson13.java
29cc80c
fix: gradle command edit
a1637c4
Feat: added content to lesson13.ts
c22c3d0
fix: lesson13.ts edits to pass npm testing
b3ec7aa
fix: simplified code and made shorter
1ebdea8
fix: simplified code and made shorter (java)
4e0fee1
chore: removed unnecessary lines of code for java and ts file
5ac4271
chore: removing Lines for java file
3d439d3
chore: line deletions for java file
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 16 additions & 5 deletions
21
lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java
This file contains hidden or 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 |
---|---|---|
@@ -1,12 +1,23 @@ | ||
package com.codedifferently.lesson13; | ||
|
||
import java.util.HashMap; | ||
|
||
public class Lesson13 { | ||
|
||
/** | ||
* Provide the solution to LeetCode 3146 here: | ||
* https://leetcode.com/problems/permutation-difference-between-two-strings | ||
*/ | ||
public int findPermutationDifference(String s, String t) { | ||
return 0; | ||
HashMap<Character, Integer> stringS = new HashMap<>(); | ||
HashMap<Character, Integer> stringT = new HashMap<>(); | ||
int diffSum = 0; | ||
for (int i = 0; i < t.length(); i++) { | ||
stringS.put(s.charAt(i), i); | ||
stringT.put(t.charAt(i), i); | ||
} | ||
for (Character key : stringS.keySet()) { | ||
int value1 = stringS.get(key); | ||
int value2 = stringT.get(key); | ||
int difference = Math.abs(value1 - value2); | ||
diffSum += difference; | ||
} | ||
return diffSum; | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,18 @@ | ||
/** | ||
* Provide the solution to LeetCode 3146 here: | ||
* https://leetcode.com/problems/permutation-difference-between-two-strings | ||
*/ | ||
export function findPermutationDifference(s: string, t: string): number { | ||
return 0; | ||
} | ||
const stringS = new Map<string, number>(); | ||
const stringT = new Map<string, number>(); | ||
let diffSum = 0; | ||
for (let i = 0; i < t.length; i++) { | ||
stringS.set(s.charAt(i), i); | ||
stringT.set(t.charAt(i), i); | ||
} | ||
for (const key of stringS.keys()) { | ||
if (stringT.has(key)) { | ||
const value1 = stringS.get(key)!; // Non-null assertion because we checked with has() | ||
const value2 = stringT.get(key)!; // Non-null assertion | ||
const difference = Math.abs(value1 - value2); | ||
diffSum += difference; | ||
} | ||
} | ||
return diffSum; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error is saying you can't use
!
and instead need to do something different. The documentation for the error (https://typescript-eslint.io/rules/no-non-null-assertion/) gives you an example of what you can do instead.Re-writing as
stringS.get(key) ?? -1
would work perfectly fine.Unfortunately, the TypeScript compiler isn't smart enough yet to figure out that the has check you did on line 10 means that the check on line 11 is safe. There's probably an open feature request for this.