sweet1cris

Untitled

Feb 10th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1.  
  2. public class Solution {
  3.     /*
  4.      * @param : the 1st string
  5.      * @param : the 2nd string
  6.      * @return: uncommon characters of given strings
  7.      */
  8.     public String concatenetedString(String s1, String s2) {
  9.         if(s1 == null){
  10.             return s2;
  11.         }
  12.         if(s2 == null){
  13.             return s1;
  14.         }
  15.        
  16.         int[] map = new int[126];
  17.         StringBuilder sb = new StringBuilder();
  18.         for(int i = 0; i < s2.length(); i++){
  19.             map[s2.charAt(i)] = 1;
  20.         }
  21.        
  22.         for(int i = 0; i < s1.length(); i++){
  23.             if(map[s1.charAt(i)] == 0){
  24.                 sb.append(s1.charAt(i));
  25.             }else{
  26.                 map[s1.charAt(i)] = 2;
  27.             }
  28.         }
  29.        
  30.         for(int i = 0; i < s2.length(); i++){
  31.             if(map[s2.charAt(i)] == 1){
  32.                 sb.append(s2.charAt(i));
  33.             }
  34.         }
  35.         return sb.toString();
  36.     }
  37. };
  38. 1
  39. 2
  40. 3
  41. 4
  42. 5
  43. 6
  44. 7
  45. 8
  46. 9
  47. 10
  48. 11
  49. 12
  50. 13
  51. 14
  52. 15
  53. 16
  54. 17
  55. 18
  56. 19
  57. 20
  58. 21
  59. 22
  60. 23
  61. 24
  62. 25
  63. 26
  64. 27
  65. 28
  66. 29
  67. 30
  68. 31
  69. 32
  70. 33
  71. 34
  72. 35
  73. 36
  74. 37
  75. 38
  76. 39
  77. 40
  78. 41
  79. 42
  80. 43
  81. 44
  82. 0
  83. L同学
  84. 发布于 11/1/2017, 10:26:33 PM
  85. Initialize result as empty string.
  86. Push all characters of 2nd string in map with count as 1.
  87. Traverse first string and append all those characters to result that are not present in map. Characters that are present in map, make count 2.
  88. Traverse second string and append all those characters to result whose count is 1.
  89.  
  90. 代码
  91. 评论 0
  92. /**
  93. * 本参考程序来自九章算法,由 @L同学 提供。版权所有,转发请注明出处。
  94. * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
  95. * - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,Android 项目实战班,
  96. * - Big Data 项目实战班,算法面试高频题班, 动态规划专题班
  97. * - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code
  98. */
  99.  
  100. public class Solution {
  101.     /*
  102.      * @param : the 1st string
  103.      * @param : the 2nd string
  104.      * @return: uncommon characters of given strings
  105.      */
  106.     public String concatenetedString(String s1, String s2) {
  107.         // write your code here
  108.         StringBuilder sb = new StringBuilder();
  109.         Map<Character, Integer> map = new HashMap<>();
  110.        
  111.         for (int i = 0; i < s2.length(); i++) {
  112.             if (!map.containsKey(s2.charAt(i))) {
  113.                 map.put(s2.charAt(i), 1);
  114.             }
  115.         }
  116.        
  117.         for (int i = 0; i < s1.length(); i++) {
  118.             if (!map.containsKey(s1.charAt(i))) {
  119.                 sb.append(s1.charAt(i));
  120.             } else {
  121.                 map.put(s1.charAt(i), 2);
  122.             }
  123.         }
  124.        
  125.         for (int i = 0; i < s2.length(); i++) {
  126.             if (map.get(s2.charAt(i)) == 1) {
  127.                 sb.append(s2.charAt(i));
  128.             }
  129.         }
  130.        
  131.         return sb.toString();
  132.     }
  133. };
Advertisement
Add Comment
Please, Sign In to add comment