Skip to content

Commit 5f4587e

Browse files
authored
Merge pull request #4250 from AmruthaPariprolu/feature/2451
2451 added
2 parents b4480a3 + 459e79e commit 5f4587e

File tree

1 file changed

+330
-0
lines changed

1 file changed

+330
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
---
2+
id: Odd-String-Difference
3+
title: Odd String Difference Problem
4+
sidebar_label: 2451-Odd String Difference
5+
tags: [dsa, leetcode]
6+
description: Problem solution of Odd String Difference Problem
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
You are given an array of equal-length strings words. Assume that the length of each string is n.
14+
15+
Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25.
16+
17+
For example, for the string "acb", the difference integer array is [2 - 0, 1 - 2] = [2, -1].
18+
All the strings in words have the same difference integer array, except one. You should find that string.
19+
20+
Return the string in words that has different difference integer array.
21+
### Examples
22+
23+
#### Example 1
24+
```
25+
Input: words = ["aaa","bob","ccc","ddd"]
26+
Output: "bob"
27+
Explanation: All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
28+
```
29+
30+
### Constraints
31+
32+
- `3 <= words.length <= 100`
33+
- `n == words[i].length`
34+
- `2 <= n <= 20`
35+
- `words[i] consists of lowercase English letters.`
36+
37+
## Solution of Given Problem
38+
39+
### Intuition and Approach
40+
41+
The problem can be solved using a brute force approach or an optimized Technique.
42+
43+
<Tabs>
44+
<tabItem value="Brute Force" label="Brute Force">
45+
46+
### Approach 1:Brute Force (Naive)
47+
48+
49+
Brute Force Approach:
50+
Convert each string in the words array to its corresponding difference integer array.
51+
Compare the difference arrays to find the one that is different from the others.
52+
#### Codes in Different Languages
53+
54+
<Tabs>
55+
<TabItem value="C++" label="C++" default>
56+
<SolutionAuthor name="@AmruthaPariprolu"/>
57+
58+
```cpp
59+
#include <iostream>
60+
#include <vector>
61+
#include <string>
62+
63+
std::vector<int> getDifferenceArray(const std::string& word) {
64+
std::vector<int> difference;
65+
for (int i = 0; i < word.length() - 1; ++i) {
66+
difference.push_back(word[i + 1] - word[i]);
67+
}
68+
return difference;
69+
}
70+
71+
std::string oddStringOut(const std::vector<std::string>& words) {
72+
std::vector<std::vector<int>> differences;
73+
for (const auto& word : words) {
74+
differences.push_back(getDifferenceArray(word));
75+
}
76+
77+
for (int i = 0; i < differences.size(); ++i) {
78+
int count = 0;
79+
for (int j = 0; j < differences.size(); ++j) {
80+
if (differences[i] == differences[j]) {
81+
count++;
82+
}
83+
}
84+
if (count == 1) {
85+
return words[i];
86+
}
87+
}
88+
return "";
89+
}
90+
91+
int main() {
92+
std::vector<std::string> words = {"adc", "wzy", "abc"};
93+
std::cout << oddStringOut(words) << std::endl; // Output: "abc"
94+
return 0;
95+
}
96+
97+
```
98+
</TabItem>
99+
<TabItem value="Java" label="Java">
100+
<SolutionAuthor name="@AmruthaPariprolu"/>
101+
102+
```java
103+
import java.util.*;
104+
105+
public class OddStringOut {
106+
public static List<Integer> getDifferenceArray(String word) {
107+
List<Integer> difference = new ArrayList<>();
108+
for (int i = 0; i < word.length() - 1; i++) {
109+
difference.add(word.charAt(i + 1) - word.charAt(i));
110+
}
111+
return difference;
112+
}
113+
114+
public static String oddStringOut(String[] words) {
115+
List<List<Integer>> differences = new ArrayList<>();
116+
for (String word : words) {
117+
differences.add(getDifferenceArray(word));
118+
}
119+
120+
for (int i = 0; i < differences.size(); i++) {
121+
int count = 0;
122+
for (int j = 0; j < differences.size(); j++) {
123+
if (differences.get(i).equals(differences.get(j))) {
124+
count++;
125+
}
126+
}
127+
if (count == 1) {
128+
return words[i];
129+
}
130+
}
131+
return "";
132+
}
133+
134+
public static void main(String[] args) {
135+
String[] words = {"adc", "wzy", "abc"};
136+
System.out.println(oddStringOut(words)); // Output: "abc"
137+
}
138+
}
139+
140+
```
141+
142+
143+
</TabItem>
144+
<TabItem value="Python" label="Python">
145+
<SolutionAuthor name="@AmruthaPariprolu"/>
146+
147+
```python
148+
def get_difference_array(word):
149+
return [ord(word[i + 1]) - ord(word[i]) for i in range(len(word) - 1)]
150+
151+
def odd_string_out(words):
152+
differences = [get_difference_array(word) for word in words]
153+
154+
for i in range(len(differences)):
155+
count = sum(differences[i] == differences[j] for j in range(len(differences)))
156+
if count == 1:
157+
return words[i]
158+
return ""
159+
160+
words = ["adc", "wzy", "abc"]
161+
print(odd_string_out(words)) # Output: "abc"
162+
163+
```
164+
165+
</TabItem>
166+
</Tabs>
167+
168+
169+
### Complexity Analysis
170+
171+
- Time Complexity: $O(m*n)$
172+
- where m is the number of strings and n is the length of each string.
173+
- Space Complexity: $O(m * (n-1))$
174+
- for storing the difference arrays.
175+
</tabItem>
176+
<tabItem value="Optimized approach" label="Optimized approach">
177+
178+
### Approach 2: Optimized approach
179+
180+
Optimized Approach:
181+
Calculate the difference arrays.
182+
Identify the unique one by comparing with the first two arrays and using the majority principle.
183+
Compare the first two difference arrays:
184+
- If they are equal, then the different one must be different from this common difference array.
185+
- If they are not equal, compare the third one to decide which of the first two is different.
186+
Return the corresponding string.
187+
#### Code in Different Languages
188+
189+
<Tabs>
190+
<TabItem value="C++" label="C++" default>
191+
<SolutionAuthor name="@AmruthaPariprolu"/>
192+
193+
```cpp
194+
#include <iostream>
195+
#include <vector>
196+
#include <string>
197+
198+
std::vector<int> getDifferenceArray(const std::string& word) {
199+
std::vector<int> difference;
200+
for (int i = 0; i < word.length() - 1; ++i) {
201+
difference.push_back(word[i + 1] - word[i]);
202+
}
203+
return difference;
204+
}
205+
206+
std::string oddStringOut(const std::vector<std::string>& words) {
207+
auto diff1 = getDifferenceArray(words[0]);
208+
auto diff2 = getDifferenceArray(words[1]);
209+
210+
if (diff1 == diff2) {
211+
for (int i = 2; i < words.size(); ++i) {
212+
if (getDifferenceArray(words[i]) != diff1) {
213+
return words[i];
214+
}
215+
}
216+
} else {
217+
auto diff3 = getDifferenceArray(words[2]);
218+
return (diff3 == diff1) ? words[1] : words[0];
219+
}
220+
return "";
221+
}
222+
223+
int main() {
224+
std::vector<std::string> words = {"adc", "wzy", "abc"};
225+
std::cout << oddStringOut(words) << std::endl; // Output: "abc"
226+
return 0;
227+
}
228+
229+
```
230+
</TabItem>
231+
<TabItem value="Java" label="Java">
232+
<SolutionAuthor name="@AmruthaPariprolu"/>
233+
234+
```java
235+
import java.util.*;
236+
237+
public class OddStringOut {
238+
public static List<Integer> getDifferenceArray(String word) {
239+
List<Integer> difference = new ArrayList<>();
240+
for (int i = 0; i < word.length() - 1; i++) {
241+
difference.add(word.charAt(i + 1) - word.charAt(i));
242+
}
243+
return difference;
244+
}
245+
246+
public static String oddStringOut(String[] words) {
247+
List<Integer> diff1 = getDifferenceArray(words[0]);
248+
List<Integer> diff2 = getDifferenceArray(words[1]);
249+
250+
if (diff1.equals(diff2)) {
251+
for (int i = 2; i < words.length; i++) {
252+
if (!getDifferenceArray(words[i]).equals(diff1)) {
253+
return words[i];
254+
}
255+
}
256+
} else {
257+
List<Integer> diff3 = getDifferenceArray(words[2]);
258+
return diff3.equals(diff1) ? words[1] : words[0];
259+
}
260+
return "";
261+
}
262+
263+
public static void main(String[] args) {
264+
String[] words = {"adc", "wzy", "abc"};
265+
System.out.println(oddStringOut(words)); // Output: "abc"
266+
}
267+
}
268+
269+
```
270+
271+
272+
</TabItem>
273+
<TabItem value="Python" label="Python">
274+
<SolutionAuthor name="@AmruthaPariprolu"/>
275+
276+
```python
277+
def get_difference_array(word):
278+
return [ord(word[i + 1]) - ord(word[i]) for i in range(len(word) - 1)]
279+
280+
def odd_string_out(words):
281+
diff1 = get_difference_array(words[0])
282+
diff2 = get_difference_array(words[1])
283+
284+
if diff1 == diff2:
285+
for i in range(2, len(words)):
286+
if get_difference_array(words[i]) != diff1:
287+
return words[i]
288+
else:
289+
diff3 = get_difference_array(words[2])
290+
return words[1] if diff3 == diff1 else words[0]
291+
return ""
292+
293+
words = ["adc", "wzy", "abc"]
294+
print(odd_string_out(words)) # Output: "abc"
295+
296+
```
297+
298+
</TabItem>
299+
</Tabs>
300+
301+
#### Complexity Analysis
302+
303+
- Time Complexity: $O(m*n)$
304+
- where m is the number of strings and n is the length of each string.
305+
- Space Complexity: $O(n)$
306+
- for storing the difference array temporarily.
307+
</tabItem>
308+
</Tabs>
309+
310+
311+
## Video Explanation of Given Problem
312+
313+
<LiteYouTubeEmbed
314+
id="uByUrGsnfX0"
315+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
316+
title="Problem Explanation | Solution | Approach"
317+
poster="maxresdefault"
318+
webp
319+
/>
320+
321+
---
322+
323+
<h2>Authors:</h2>
324+
325+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
326+
{['AmruthaPariprolu'].map(username => (
327+
<Author key={username} username={username} />
328+
))}
329+
</div>
330+

0 commit comments

Comments
 (0)