Browser tab content: Problem List
0
Debugging...
Run
Submit
00:00:00
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Debugger
Debugger
Code
Note
Note
Testcase
Testcase
Test Result
245. Shortest Word Distance III
Medium
Topics
Companies

Given an array of strings wordsDict and two strings that already exist in the array word1 and word2, return the shortest distance between the occurrence of these two words in the list.

Note that word1 and word2 may be the same. It is guaranteed that they represent two individual words in the list.

 

Example 1:

Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding"
Output: 1


Example 2:

Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "makes"
Output: 3


 

Constraints:

1 <= wordsDict.length <= 105
1 <= wordsDict[i].length <= 10
wordsDict[i] consists of lowercase English letters.
word1 and word2 are in wordsDict.
Seen this question in a real interview before?
1/5
Yes
No
Accepted
84.8K
Submissions
144.5K
Acceptance Rate
58.7%
Topics
Array
String
Companies
0 - 6 months
LinkedIn
2
6 months ago
Palantir Technologies
2
Similar Questions
Shortest Word Distance
Easy
Shortest Word Distance II
Medium
Discussion (7)
Choose a type
Preview
Comment
💡 Discussion Rules

1. Please don't post any solutions in this discussion.

2. The problem discussion is for asking questions about the problem or for sharing tips - anything except for solutions.

3. If you'd like to share your solution for feedback and ideas, please head to the solutions tab and post it there.

Sort by:Best
No comments yet.
1
Copyright ©️ 2024 LeetCode All rights reserved
506
7
2 Online
Java
Auto
1
2
3
4
5
class Solution {
    public int shortestWordDistance(String[] wordsDict, String 
word1, String word2) {
        
    }
}
Restored from local
Ln 3, Col 9
You must run your code first

Employees Earning More Than Their Managers - LeetCode

Problem List
Tag
171. Excel Sheet Column Number

Easy

172. Factorial Trailing Zeroes

Med.

173. Binary Search Tree Iterator

Med.

174. Dungeon Game

Hard

175. Combine Two Tables

Easy

176. Second Highest Salary

Med.

177. Nth Highest Salary

Med.

178. Rank Scores

Med.

179. Largest Number

Med.

180. Consecutive Numbers

Med.

181. Employees Earning More Than Their Managers

Easy

182. Duplicate Emails

Easy

183. Customers Who Never Order

Easy

184. Department Highest Salary

Med.

185. Department Top Three Salaries

Hard

186. Reverse Words in a String II

Med.

187. Repeated DNA Sequences

Med.

188. Best Time to Buy and Sell Stock IV

Hard

189. Rotate Array

Med.

190. Reverse Bits

Easy

191. Number of 1 Bits

Easy

192. Word Frequency

Med.

193. Valid Phone Numbers

Easy

194. Transpose File

Med.

195. Tenth Line

Easy

196. Delete Duplicate Emails

Easy

197. Rising Temperature

Easy

198. House Robber

Med.

199. Binary Tree Right Side View

Med.

200. Number of Islands

Med.

201. Bitwise AND of Numbers Range

Med.

202. Happy Number

Easy

203. Remove Linked List Elements

Easy

204. Count Primes

Med.

205. Isomorphic Strings

Easy

206. Reverse Linked List

Easy

207. Course Schedule

Med.

208. Implement Trie (Prefix Tree)

Med.

209. Minimum Size Subarray Sum

Med.

210. Course Schedule II

Med.

211. Design Add and Search Words Data Structure

Med.

212. Word Search II

Hard

213. House Robber II

Med.

214. Shortest Palindrome

Hard

215. Kth Largest Element in an Array

Med.

216. Combination Sum III

Med.

217. Contains Duplicate

Easy

218. The Skyline Problem

Hard

219. Contains Duplicate II

Easy

220. Contains Duplicate III

Hard

221. Maximal Square

Med.

222. Count Complete Tree Nodes

Easy

223. Rectangle Area

Med.

224. Basic Calculator

Hard

225. Implement Stack using Queues

Easy

226. Invert Binary Tree

Easy

227. Basic Calculator II

Med.

228. Summary Ranges

Easy

229. Majority Element II

Med.

230. Kth Smallest Element in a BST

Med.

231. Power of Two

Easy

232. Implement Queue using Stacks

Easy

233. Number of Digit One

Hard

234. Palindrome Linked List

Easy

235. Lowest Common Ancestor of a Binary Search Tree

Med.

236. Lowest Common Ancestor of a Binary Tree

Med.

237. Delete Node in a Linked List

Med.

238. Product of Array Except Self

Med.

239. Sliding Window Maximum

Hard

240. Search a 2D Matrix II

Med.

241. Different Ways to Add Parentheses

Med.

242. Valid Anagram

Easy

243. Shortest Word Distance

Easy

244. Shortest Word Distance II

Med.

245. Shortest Word Distance III

Med.

246. Strobogrammatic Number

Easy

247. Strobogrammatic Number II

Med.

248. Strobogrammatic Number III

Hard

249. Group Shifted Strings

Med.

250. Count Univalue Subtrees

Med.

251. Flatten 2D Vector

Med.

252. Meeting Rooms

Easy

253. Meeting Rooms II

Med.

254. Factor Combinations

Med.

255. Verify Preorder Sequence in Binary Search Tree

Med.

256. Paint House

Med.

257. Binary Tree Paths

Easy

258. Add Digits

Easy

259. 3Sum Smaller

Med.

260. Single Number III

Med.

261. Graph Valid Tree

Med.

262. Trips and Users

Hard

263. Ugly Number

Easy

264. Ugly Number II

Med.

265. Paint House II

Hard

266. Palindrome Permutation

Easy

267. Palindrome Permutation II

Med.

268. Missing Number

Easy

269. Alien Dictionary

Hard

270. Closest Binary Search Tree Value

Easy

271. Encode and Decode Strings

Med.

272. Closest Binary Search Tree Value II

Hard

273. Integer to English Words

Hard

274. H-Index

Med.

275. H-Index II

Med.

276. Paint Fence

Med.

277. Find the Celebrity

Med.

278. First Bad Version

Easy

279. Perfect Squares

Med.

280. Wiggle Sort

Med.

281. Zigzag Iterator

Med.

282. Expression Add Operators

Hard

283. Move Zeroes

Easy

284. Peeking Iterator

Med.

285. Inorder Successor in BST

Med.

286. Walls and Gates

Med.

287. Find the Duplicate Number

Med.

288. Unique Word Abbreviation

Med.

289. Game of Life

Med.

290. Word Pattern

Easy

291. Word Pattern II

Med.

292. Nim Game

Easy

293. Flip Game

Easy

294. Flip Game II

Med.

295. Find Median from Data Stream

Hard

296. Best Meeting Point

Hard

297. Serialize and Deserialize Binary Tree

Hard

298. Binary Tree Longest Consecutive Sequence

Med.

299. Bulls and Cows

Med.

300. Longest Increasing Subsequence

Med.

301. Remove Invalid Parentheses

Hard

302. Smallest Rectangle Enclosing Black Pixels

Hard

303. Range Sum Query - Immutable

Easy

304. Range Sum Query 2D - Immutable

Med.

305. Number of Islands II

Hard

306. Additive Number

Med.

307. Range Sum Query - Mutable

Med.

308. Range Sum Query 2D - Mutable

Hard

309. Best Time to Buy and Sell Stock with Cooldown

Med.

310. Minimum Height Trees

Med.

311. Sparse Matrix Multiplication

Med.

312. Burst Balloons

Hard

313. Super Ugly Number

Med.

314. Binary Tree Vertical Order Traversal

Med.

315. Count of Smaller Numbers After Self

Hard

316. Remove Duplicate Letters

Med.

317. Shortest Distance from All Buildings

Hard

318. Maximum Product of Word Lengths

Med.

319. Bulb Switcher

Med.

320. Generalized Abbreviation

Med.

Remove distracting text, extract the programming question, provide simple, common, and optimized Java solution for it.