개발/Programming
[ Leetcode ] 214. Shortest Palindrome - Java
쩨이엠
2021. 1. 28. 10:28
728x90
반응형
Given a string s, you can convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
Example 1:
Input: s = "aacecaaa"
Output: "aaacecaaa"
Example 2:
Input: s = "abcd"
Output: "dcbabcd"
Constraints:
- 0 <= s.length <= 5 * 104
- s consists of lowercase English letters only.
Solution
class Solution {
public String shortestPalindrome(String s) {
String newString = "";
int length = s.length();
for(int i=0; i< length; i++){
newString = s.charAt(i)+newString;
}
String subString = "";
for(int i=0; i<length; i++){
subString = newString.substring(i);
if(s.startsWith(subString)){
newString = newString.substring(0, i);
break;
}
}
return newString+s;
}
}
처음으로 Hard 풀어서 신났는데 0ms에 몰려있는걸보니 내가 왜 프로그래머스 효율성이 0인지를 알겠다...
알고리즘 공부합시다...반성반성
728x90
반응형