Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 643847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:19:38+00:00 2026-05-13T21:19:38+00:00

I’m trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they

  • 0

I’m trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html.
For the given example, they give us the original word, for example TRIT and the target “anagramed” string, TIRT.

Objective: We have to output all the valid sequences of ‘i’ and ‘o’ (push and pop’s, respectively) which produce the target string from the source string.

So, I was thinking of calculate all permutations of “i” and “o” , but cutting this cases:

1) if current permutation begins with an ‘o’, stop checking, since all the of the next permutations will begin with this pop command and popping something from an empty stack is an invalid command.

2) if an ‘o’ command is found in the middle of the checking and there is nothing in the stack, skip that case.

3) if an ‘i’ command is found and there is nothing in the input string, skip that case.

4) if an ‘o’ command is found and currently expected character is not the character just popped out, then skip that case, since this will never reach to the target string.

5) don’t search if the input and target strings have different lengths.

but I think it might get me TLE anyway…

I know the theory: permutations perhaps and backtracking all the way.
I just have too many difficulties implementing it.

could anyone please share with me some code and or ideas please?

P.S.: Any suggestion that may decrease some execution time will be welcome , of course.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:19:38+00:00Added an answer on May 13, 2026 at 9:19 pm

    This first iteration solution is instructive. It’s not the most efficient since it uses String all over the place, but it’s a good place to start.

    import java.util.*;
    
    public class StackAnagram {
    
        static void anagram(String s1, String s2, String stack, String instr) {
            if (s2.isEmpty()) {
                if (s1.isEmpty() && stack.isEmpty()) {
                    System.out.println(instr.trim());
                }
                return;
            }
            if (!s1.isEmpty()) {
                anagram(s1.substring(1), s2, s1.charAt(0) + stack, instr + "i ");
            }
            if (!stack.isEmpty() && stack.charAt(0) == s2.charAt(0)) {
                anagram(s1, s2.substring(1), stack.substring(1), instr + "o ");
            }
        }
    
        static void anagram(String s1, String s2) {
            System.out.println("[");
            anagram(s1, s2, "", "");
            System.out.println("]");
        }
    
        public static void main(String args[]) {
            anagram("madam", "adamm");
            anagram("bahama", "bahama");
            anagram("long", "short");
            anagram("eric", "rice");
            anagram("ericc", "rice");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.