• Topcoder711B


    Problem Statement

        

    You are given the Strings s and t. Both strings have the same length.

    You are allowed to modify s. In each step you may choose two valid indices i and j such that i > j, and change s[i] to s[j]. For example, if s = "abc", you may choose i=2 and j=0, which will change s to "aba".

    Return "Possible" (quotes for clarity) if you can change s into t by a sequence of zero or more steps. Otherwise, return "Impossible".

    Note that the return values are case-sensitive.

    Definition

        
    Class: StringTransform
    Method: isPossible
    Parameters: String, String
    Returns: String
    Method signature: String isPossible(String s, String t)
    (be sure your method is public)

    Limits

        
    Time limit (s): 2.000
    Memory limit (MB): 512
    Stack limit (MB): 512

    Constraints

    - s will contain between 1 and 1,000 characters, inclusive.
    - s and t will be of equal length.
    - s will contain only lowercase English letters.
    - t will contain only lowercase English letters.

    Examples

    0)  
        
    "abc"
    "aba"
    Returns: "Possible"
    This is the example from the problem statement. We can change s into t by changing s[2] to s[0].
    1)  
        
    "abc"
    "bbc"
    Returns: "Impossible"
    Note that in each step i must be greater than j. You are not allowed to choose i=0 and j=1.
    2)  
        
    "topcoder"
    "topcoder"
    Returns: "Possible"
    The two strings are equal, so we don't have to make any changes.
    3)  
        
    "rdmcxnnbbe"
    "rdrrxrnxbe"
    Returns: "Possible"
     
    4)  
        
    "rdmcxnnbbe"
    "rdqrxrnxbe"
    Returns: "Impossible"
    import java.util.*;
    public class StringTransform {
       
        public static String isPossible(String s,String t){
            Set<Character> set = new HashSet<Character>();
          set.clear();
            for(int i=0;i<s.length();++i) {
               
                if (s.charAt(i) != t.charAt(i)) {
                    if (!set.contains(t.charAt(i)))
                        return "Impossible";
                }
                    set.add(s.charAt(i));
                    
                }
            
            return "Possible";
    
        }
    }
    

      

  • 相关阅读:
    test
    设置section的距离
    UISearchBar的应用
    Centos6.6 系统优化
    Linux系统之间文件传输 scp 命令
    MySQL 数据库安装
    AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件
    Ubuntu 16.04 Chrome浏览器安装flash player插件
    Centos6.6 yum源更新
    qume-kvm 命令管理
  • 原文地址:https://www.cnblogs.com/passion-sky/p/8568244.html
Copyright © 2020-2023  润新知