Skip to main content

Command Palette

Search for a command to run...

Find Smallest Letter Greater Than Target

Published
1 min read
Find Smallest Letter Greater Than Target
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.

Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.

LeetCode Problem - 744: Link | Click Here

class Solution {
    // Method to find the smallest letter greater than the target letter
    public char nextGreatestLetter(char[] letters, char target) {
        // Get the length of the letters array
        int len = letters.length;

        // Initialize the search range and result variable
        int low = 0;
        int high = len - 1;
        char result = '-';

        // Binary search loop to find the smallest letter greater than the target
        while (low <= high) {
            int mid = (low + high) / 2;

            // Check if the middle letter is greater than the target
            if (letters[mid] > target) {
                high = mid - 1;    // Update the search range
                result = letters[mid];  // Update the result with the current letter
            } else {
                low = mid + 1;     // Update the search range
            }
        }

        // Return the result if found, otherwise, return the first letter in the array
        return (result == '-') ? letters[0] : result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.