Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /*
- * @param : the 1st string
- * @param : the 2nd string
- * @return: uncommon characters of given strings
- */
- public String concatenetedString(String s1, String s2) {
- if(s1 == null){
- return s2;
- }
- if(s2 == null){
- return s1;
- }
- int[] map = new int[126];
- StringBuilder sb = new StringBuilder();
- for(int i = 0; i < s2.length(); i++){
- map[s2.charAt(i)] = 1;
- }
- for(int i = 0; i < s1.length(); i++){
- if(map[s1.charAt(i)] == 0){
- sb.append(s1.charAt(i));
- }else{
- map[s1.charAt(i)] = 2;
- }
- }
- for(int i = 0; i < s2.length(); i++){
- if(map[s2.charAt(i)] == 1){
- sb.append(s2.charAt(i));
- }
- }
- return sb.toString();
- }
- };
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 0
- L同学
- 发布于 11/1/2017, 10:26:33 PM
- Initialize result as empty string.
- Push all characters of 2nd string in map with count as 1.
- 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.
- Traverse second string and append all those characters to result whose count is 1.
- 代码
- 评论 0
- /**
- * 本参考程序来自九章算法,由 @L同学 提供。版权所有,转发请注明出处。
- * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
- * - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,Android 项目实战班,
- * - Big Data 项目实战班,算法面试高频题班, 动态规划专题班
- * - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code
- */
- public class Solution {
- /*
- * @param : the 1st string
- * @param : the 2nd string
- * @return: uncommon characters of given strings
- */
- public String concatenetedString(String s1, String s2) {
- // write your code here
- StringBuilder sb = new StringBuilder();
- Map<Character, Integer> map = new HashMap<>();
- for (int i = 0; i < s2.length(); i++) {
- if (!map.containsKey(s2.charAt(i))) {
- map.put(s2.charAt(i), 1);
- }
- }
- for (int i = 0; i < s1.length(); i++) {
- if (!map.containsKey(s1.charAt(i))) {
- sb.append(s1.charAt(i));
- } else {
- map.put(s1.charAt(i), 2);
- }
- }
- for (int i = 0; i < s2.length(); i++) {
- if (map.get(s2.charAt(i)) == 1) {
- sb.append(s2.charAt(i));
- }
- }
- return sb.toString();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment