Skip to content

Commit cd096a6

Browse files
refactor 471
1 parent e755602 commit cd096a6

File tree

1 file changed

+32
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+32
-27
lines changed

src/main/java/com/fishercoder/solutions/_471.java

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* 471. Encode String with Shortest Length
5+
*
56
Given a non-empty string, encode the string such that its encoded length is the shortest.
67
78
The encoding rule is: k[encoded_string], where the encoded_string inside the square
@@ -41,42 +42,46 @@
4142
*/
4243
public class _471 {
4344

44-
/**credit: https://discuss.leetcode.com/topic/71963/accepted-solution-in-java*/
45-
public String encode(String s) {
46-
String[][] dp = new String[s.length()][s.length()];
45+
public static class Solution1 {
46+
/**
47+
* credit: https://discuss.leetcode.com/topic/71963/accepted-solution-in-java
48+
*/
49+
public String encode(String s) {
50+
String[][] dp = new String[s.length()][s.length()];
4751

48-
for (int l = 0; l < s.length(); l++) {
49-
for (int i = 0; i < s.length() - l; i++) {
50-
int j = i + l;
51-
String substr = s.substring(i, j + 1);
52-
// Checking if string length < 5. In that case, we know that encoding will not help.
53-
if (j - i < 4) {
54-
dp[i][j] = substr;
55-
} else {
56-
dp[i][j] = substr;
57-
// Loop for trying all results that we get after dividing the strings into 2 and combine the results of 2 substrings
58-
for (int k = i; k < j; k++) {
59-
if ((dp[i][k] + dp[k + 1][j]).length() < dp[i][j].length()) {
60-
dp[i][j] = dp[i][k] + dp[k + 1][j];
52+
for (int l = 0; l < s.length(); l++) {
53+
for (int i = 0; i < s.length() - l; i++) {
54+
int j = i + l;
55+
String substr = s.substring(i, j + 1);
56+
// Checking if string length < 5. In that case, we know that encoding will not help.
57+
if (j - i < 4) {
58+
dp[i][j] = substr;
59+
} else {
60+
dp[i][j] = substr;
61+
// Loop for trying all results that we get after dividing the strings into 2 and combine the results of 2 substrings
62+
for (int k = i; k < j; k++) {
63+
if ((dp[i][k] + dp[k + 1][j]).length() < dp[i][j].length()) {
64+
dp[i][j] = dp[i][k] + dp[k + 1][j];
65+
}
6166
}
62-
}
6367

64-
// Loop for checking if string can itself found some pattern in it which could be repeated.
65-
for (int k = 0; k < substr.length(); k++) {
66-
String repeatStr = substr.substring(0, k + 1);
67-
if (repeatStr != null
68-
&& substr.length() % repeatStr.length() == 0
69-
&& substr.replaceAll(repeatStr, "").length() == 0) {
70-
String ss = substr.length() / repeatStr.length() + "[" + dp[i][i + k] + "]";
71-
if (ss.length() < dp[i][j].length()) {
72-
dp[i][j] = ss;
68+
// Loop for checking if string can itself found some pattern in it which could be repeated.
69+
for (int k = 0; k < substr.length(); k++) {
70+
String repeatStr = substr.substring(0, k + 1);
71+
if (repeatStr != null
72+
&& substr.length() % repeatStr.length() == 0
73+
&& substr.replaceAll(repeatStr, "").length() == 0) {
74+
String ss = substr.length() / repeatStr.length() + "[" + dp[i][i + k] + "]";
75+
if (ss.length() < dp[i][j].length()) {
76+
dp[i][j] = ss;
77+
}
7378
}
7479
}
7580
}
7681
}
7782
}
83+
return dp[0][s.length() - 1];
7884
}
79-
return dp[0][s.length() - 1];
8085
}
8186

8287
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy