LeetCode 744 | Find Smallest Letter Greater Than Target | Java | Easy | Binary Search | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 744 β Find Smallest Letter Greater Than Target using Binary Search Upper Bound in Java.
We are given a non-decreasing sorted array of characters and a target character. We need to find the smallest character strictly greater than the target. If no such character exists in the array, the problem requires wrapping around and returning the very first character of the array. Since the input array is already sorted, we can avoid a linear search and directly locate the answer in logarithmic time.
We implement an optimal binary search equivalent to finding the upper bound of target. At each midpoint, if the character is less than or equal to target, we advance the left pointer; otherwise, we narrow down the right boundary. Upon search termination, we return letters[left % letters.length], which naturally handles the wrap-around case to letters[0] in a single clean expression without extra conditions.
π₯ Problem Highlights
β Given array is already sorted in non-decreasing order
β Problem directly translates to an Upper Bound binary search
β Eliminates linear search overhead by halving search space in O(log N)
β Gracefully handles duplicate characters in the sorted array
β Modulo arithmetic (left % n) effortlessly implements wrap-around
β Time Complexity: O(log N) | Auxiliary Space Complexity: O(1)
β±οΈ TIMESTAMPS
00:00 Intro
00:54 Problem Understanding
01:51 Key Observation
03:29 Binary Search Upper Bound Approach
04:31 Java Code Walkthrough
05:40 Execution & Test Cases
07:08 Time Complexity Analysis
07:52 Space Complexity Analysis
08:27 Outro & Wrap-up
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Binary Search Patterns
Upper Bound Algorithm
Array Search Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why does letters[left % letters.length] correctly handle the wrap-around condition when no character is strictly greater than target?
1οΈβ£ The modulo operator returns the median element when left equals n
2οΈβ£ When no element is greater, left equals letters.length, and letters.length % letters.length evaluates to 0, returning letters[0]
3οΈβ£ Binary search forces left to be -1 when target is not found
4οΈβ£ Modulo arithmetic reverses the sorted character array
#LeetCode744 #LeetCode #FindSmallestLetterGreaterThanTarget #Java #JavaDSA #BinarySearch #UpperBound #Array #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 744 β Find Smallest Letter Greater Than Target using Binary Search Upper Bound in Java.
We are given a non-decreasing sorted array of characters and a target character. We need to find the smallest character strictly greater than the target. If no such character exists in the array, the problem requires wrapping around and returning the very first character of the array. Since the input array is already sorted, we can avoid a linear search and directly locate the answer in logarithmic time.
We implement an optimal binary search equivalent to finding the upper bound of target. At each midpoint, if the character is less than or equal to target, we advance the left pointer; otherwise, we narrow down the right boundary. Upon search termination, we return letters[left % letters.length], which naturally handles the wrap-around case to letters[0] in a single clean expression without extra conditions.
π₯ Problem Highlights
β Given array is already sorted in non-decreasing order
β Problem directly translates to an Upper Bound binary search
β Eliminates linear search overhead by halving search space in O(log N)
β Gracefully handles duplicate characters in the sorted array
β Modulo arithmetic (left % n) effortlessly implements wrap-around
β Time Complexity: O(log N) | Auxiliary Space Complexity: O(1)
β±οΈ TIMESTAMPS
00:00 Intro
00:54 Problem Understanding
01:51 Key Observation
03:29 Binary Search Upper Bound Approach
04:31 Java Code Walkthrough
05:40 Execution & Test Cases
07:08 Time Complexity Analysis
07:52 Space Complexity Analysis
08:27 Outro & Wrap-up
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/31-January/744-Find-Smallest-Letter-Greater-Than-Target/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Binary Search Patterns
Upper Bound Algorithm
Array Search Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why does letters[left % letters.length] correctly handle the wrap-around condition when no character is strictly greater than target?
1οΈβ£ The modulo operator returns the median element when left equals n
2οΈβ£ When no element is greater, left equals letters.length, and letters.length % letters.length evaluates to 0, returning letters[0]
3οΈβ£ Binary search forces left to be -1 when target is not found
4οΈβ£ Modulo arithmetic reverses the sorted character array
#LeetCode744 #LeetCode #FindSmallestLetterGreaterThanTarget #Java #JavaDSA #BinarySearch #UpperBound #Array #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
