Lexicographic Order in Java

Lexicographic Order Problem

Write java method to compare two strings lexicographically. The method should return 0 if the two strings
are Lexicographically equal. It should return 1 if the first string comes before the second string in the dictionary. It should return -1 if the first string comes after the second string in the dictionary. If the two strings share the same prefix then it should return 1 if the first string is shorter than the second string otherwise it should return 1. Here are few examples

Solution

This is straight forward problem. All you need is character by character comparison but you need to keep track which string is the shorter one in order not to exceed the array limits.

Code

Here is the Java code to do that

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *