Click here to Skip to main content
15,946,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Lexicographical minimal string--

Suppose I have two input strings 'JACK' and 'DENIAL'. Now as per the problem I want a lexicographical minimal string as an output like this (in python)--
DAJACKNIEL

But am getting, based on my code--
JNJLADAAANAIAEALCDCNCICECLKNK

Am learning python and tried the below code..

What I have tried:

Python
def string_comp(str1, str2):
    empty_str = ''
    for i in range(len(str1)):
        for j in range(len(str2)):
            if str1[i:] < str2[j:]:
                empty_str += str1[i]
                empty_str += str2[j]
    empty_str = empty_str[:-1]
    if i < (len(str1)):
        empty_str += str1[i: -1]
    if j < (len(str2)):
        empty_str += str2[j: -1]
    return empty_str


Kindly, correct me.
Posted
Updated 22-Apr-16 8:13am
v2

1 solution

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Run the program line by line, and see where and why it stops matching your expectations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900