Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e81a0e

Browse files
solves #2451: Odd String Difference in java
1 parent 2c45527 commit 9e81a0e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@
775775
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times) | [![Java](assets/java.png)](src/NumberOfValidClockTimes.java) | |
776776
| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) | [![Java](assets/java.png)](src/LargestPositiveIntegerThatExistsWithItsNegative.java) | |
777777
| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict) | [![Java](assets/java.png)](src/DetermineIfTwoEventsHaveConflict.java) | |
778-
| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference) | | |
778+
| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference) | [![Java](assets/java.png)](src/OddStringDifference.java) | |
779779
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three) | | |
780780
| 2460 | [Apply Operations to an Array](https://leetcode.com/problems/apply-operations-to-an-array) | | |
781781
| 2465 | [Number of Distinct Averages](https://leetcode.com/problems/number-of-distinct-averages) | | |

‎src/OddStringDifference.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// https://leetcode.com/problems/odd-string-difference
2+
// T: O(|words| * |words[i]|)
3+
// S: O(|words[i]|)
4+
5+
import java.util.Arrays;
6+
7+
public class OddStringDifference {
8+
public String oddString(String[] words) {
9+
final int[] diff1 = stringDiffArray(words[0]);
10+
final int[] diff2 = stringDiffArray(words[1]);
11+
12+
if (Arrays.equals(diff1, diff2)) {
13+
for (int index = 2 ; index < words.length ; index++) {
14+
final int[] diff = stringDiffArray(words[index]);
15+
if (!Arrays.equals(diff1, diff)) return words[index];
16+
}
17+
} else {
18+
final int[] diff3 = stringDiffArray(words[2]);
19+
if (Arrays.equals(diff1, diff3)) return words[1];
20+
return words[0];
21+
}
22+
23+
return words[0];
24+
}
25+
26+
private int[] stringDiffArray(String word) {
27+
final int[] array = new int[word.length() - 1];
28+
for (int index = 0 ; index < word.length() - 1 ; index++) {
29+
array[index] = word.charAt(index) - word.charAt(index + 1);
30+
}
31+
return array;
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /