zoukankan      html  css  js  c++  java
  • 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";
    
        }
    }
    

      

  • 相关阅读:
    hadoop的namenode故障处理方法
    hadoop的checkpoint检查时间参数设置
    hadoop配置历史服务器&&配置日志聚集
    hadoop_批量命令脚本&同步文件脚本
    查看centos操作系统、java_jdk、hadoop位数
    hadoop的namenode启动失败
    shell_语法
    Mycat入门配置_读写分离配置
    VM虚拟机克隆_修改网络
    ssh免密登陆及时间设置
  • 原文地址:https://www.cnblogs.com/passion-sky/p/8568244.html
Copyright © 2011-2022 走看看