From 97dd8f62d35c1b5d66426c4667d399c1c938b3d1 Mon Sep 17 00:00:00 2001 From: Pramod Gupta Date: Sun, 9 Sep 2018 17:51:47 +0530 Subject: [PATCH] Replaces String concatenation with StringBuilder --- company/adobe/MajorityElement.java | 11 ++++++----- company/apple/ReverseWordsInAString.java | 8 ++++---- company/bloomberg/ReverseWordsInAString.java | 8 ++++---- company/microsoft/ReverseWordsInAString.java | 8 ++++---- company/snapchat/ReverseWordsInAString.java | 8 ++++---- company/yelp/ReverseWordsInAString.java | 8 ++++---- .../chapter-one-arrays-and-strings/IsRotation.java | 2 +- leetcode/array/MajorityElement.java | 9 +++++---- leetcode/string/ReverseWordsInAString.java | 9 +++++---- 9 files changed, 37 insertions(+), 34 deletions(-) diff --git a/company/adobe/MajorityElement.java b/company/adobe/MajorityElement.java index 4fbb79b6..7054c486 100644 --- a/company/adobe/MajorityElement.java +++ b/company/adobe/MajorityElement.java @@ -9,10 +9,11 @@ public int majorityElement(int[] nums) { HashMap map = new HashMap(); for(int current: nums) { - if(map.containsKey(current) && map.get(current) + 1 > nums.length / 2) { - return current; - } else if(map.containsKey(current)) { - map.put(current, map.get(current) + 1); + if (map.containsKey(current)) { + if (map.get(current) + 1 > nums.length / 2) + return current; + else + map.put(current, map.get(current) + 1); } else { map.put(current, 1); } @@ -21,4 +22,4 @@ public int majorityElement(int[] nums) { //no majority element exists return -1; } -} +} \ No newline at end of file diff --git a/company/apple/ReverseWordsInAString.java b/company/apple/ReverseWordsInAString.java index aab6fa48..a8529c14 100644 --- a/company/apple/ReverseWordsInAString.java +++ b/company/apple/ReverseWordsInAString.java @@ -6,12 +6,12 @@ public class ReverseWordsInAString { public String reverseWords(String s) { String[] words = s.trim().split("\\s+"); - String result = ""; + if(words.length==1) return s; + StringBuilder sb = new StringBuilder(); for(int i = words.length - 1; i > 0; i--) { - result += words[i] + " "; + sb.append(words[i]).append(" "); } - - return result + words[0]; + return sb.append(words[0]).toString(); } } diff --git a/company/bloomberg/ReverseWordsInAString.java b/company/bloomberg/ReverseWordsInAString.java index aab6fa48..a8529c14 100644 --- a/company/bloomberg/ReverseWordsInAString.java +++ b/company/bloomberg/ReverseWordsInAString.java @@ -6,12 +6,12 @@ public class ReverseWordsInAString { public String reverseWords(String s) { String[] words = s.trim().split("\\s+"); - String result = ""; + if(words.length==1) return s; + StringBuilder sb = new StringBuilder(); for(int i = words.length - 1; i > 0; i--) { - result += words[i] + " "; + sb.append(words[i]).append(" "); } - - return result + words[0]; + return sb.append(words[0]).toString(); } } diff --git a/company/microsoft/ReverseWordsInAString.java b/company/microsoft/ReverseWordsInAString.java index aab6fa48..a8529c14 100644 --- a/company/microsoft/ReverseWordsInAString.java +++ b/company/microsoft/ReverseWordsInAString.java @@ -6,12 +6,12 @@ public class ReverseWordsInAString { public String reverseWords(String s) { String[] words = s.trim().split("\\s+"); - String result = ""; + if(words.length==1) return s; + StringBuilder sb = new StringBuilder(); for(int i = words.length - 1; i > 0; i--) { - result += words[i] + " "; + sb.append(words[i]).append(" "); } - - return result + words[0]; + return sb.append(words[0]).toString(); } } diff --git a/company/snapchat/ReverseWordsInAString.java b/company/snapchat/ReverseWordsInAString.java index aab6fa48..a8529c14 100644 --- a/company/snapchat/ReverseWordsInAString.java +++ b/company/snapchat/ReverseWordsInAString.java @@ -6,12 +6,12 @@ public class ReverseWordsInAString { public String reverseWords(String s) { String[] words = s.trim().split("\\s+"); - String result = ""; + if(words.length==1) return s; + StringBuilder sb = new StringBuilder(); for(int i = words.length - 1; i > 0; i--) { - result += words[i] + " "; + sb.append(words[i]).append(" "); } - - return result + words[0]; + return sb.append(words[0]).toString(); } } diff --git a/company/yelp/ReverseWordsInAString.java b/company/yelp/ReverseWordsInAString.java index aab6fa48..a8529c14 100644 --- a/company/yelp/ReverseWordsInAString.java +++ b/company/yelp/ReverseWordsInAString.java @@ -6,12 +6,12 @@ public class ReverseWordsInAString { public String reverseWords(String s) { String[] words = s.trim().split("\\s+"); - String result = ""; + if(words.length==1) return s; + StringBuilder sb = new StringBuilder(); for(int i = words.length - 1; i > 0; i--) { - result += words[i] + " "; + sb.append(words[i]).append(" "); } - - return result + words[0]; + return sb.append(words[0]).toString(); } } diff --git a/cracking-the-coding-interview/chapter-one-arrays-and-strings/IsRotation.java b/cracking-the-coding-interview/chapter-one-arrays-and-strings/IsRotation.java index 9e84ab6a..94975988 100644 --- a/cracking-the-coding-interview/chapter-one-arrays-and-strings/IsRotation.java +++ b/cracking-the-coding-interview/chapter-one-arrays-and-strings/IsRotation.java @@ -8,7 +8,7 @@ public boolean isRotation(String s1, String s2) { /*check that s1 and s2 are equal length and not empty */ if(len == s2.length() && len > 0) { /* concatenate s1 and s1 within new buffer */ - String s1s1 = s1 + s1; + String s1s1 = new StringBuilder().append(s1).append(s2).toString(); return isSubstring(s1s1, s2); } diff --git a/leetcode/array/MajorityElement.java b/leetcode/array/MajorityElement.java index 4fbb79b6..662b8496 100644 --- a/leetcode/array/MajorityElement.java +++ b/leetcode/array/MajorityElement.java @@ -9,10 +9,11 @@ public int majorityElement(int[] nums) { HashMap map = new HashMap(); for(int current: nums) { - if(map.containsKey(current) && map.get(current) + 1 > nums.length / 2) { - return current; - } else if(map.containsKey(current)) { - map.put(current, map.get(current) + 1); + if (map.containsKey(current)) { + if (map.get(current) + 1 > nums.length / 2) + return current; + else + map.put(current, map.get(current) + 1); } else { map.put(current, 1); } diff --git a/leetcode/string/ReverseWordsInAString.java b/leetcode/string/ReverseWordsInAString.java index 78ddaeeb..a8529c14 100644 --- a/leetcode/string/ReverseWordsInAString.java +++ b/leetcode/string/ReverseWordsInAString.java @@ -6,11 +6,12 @@ public class ReverseWordsInAString { public String reverseWords(String s) { String[] words = s.trim().split("\\s+"); - String result = ""; + if(words.length==1) return s; + StringBuilder sb = new StringBuilder(); for(int i = words.length - 1; i > 0; i--) { - result += words[i] + " "; + sb.append(words[i]).append(" "); } - - return result + words[0]; + return sb.append(words[0]).toString(); } } +