Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
paypulse
AlgorithmStudy_Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmStudy_Java
/
src
/
hackerrank
/
SeparatetheNumbers.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
55 lines (47 loc) · 1.78 KB
master
Breadcrumbs
AlgorithmStudy_Java
/
src
/
hackerrank
/
SeparatetheNumbers.java
Copy path
Top
File metadata and controls
Code
Blame
55 lines (47 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
45
46
47
48
49
50
51
52
53
54
55
package
hackerrank
;
import
java
.
util
.
Arrays
;
public
class
SeparatetheNumbers
{
static
void
separateNumbers
(
String
s
) {
//아래 제약사항을 충족하면 YES와 가장 작은 숫자를 리턴, 충족하지않으면 NO를 리턴한다.
//1. 다음요소는 이전요소보다 항상 1이 더 커야한다.
//2. 요소는 0로 시작해서는 안 된다.
//3. s안의 시퀀스를 재배열할 수 없다.
String
subStr
=
""
;
boolean
isVaild
=
false
;
//subString을 0부터 시작하니까 i는 1부터 시작하고 s 길이의 반을 넘을 수 없다.
for
(
int
i
=
1
;
i
<=
s
.
length
() /
2
;
i
++) {
subStr
=
s
.
substring
(
0
,
i
);
Long
num
=
Long
.
parseLong
(
subStr
);
//0부터 i까지 자른 String을 vaildStr변수에 담고 vaildStr를 1씩증가시킨값을 누적합한다.
String
vaildStr
=
subStr
;
while
(
vaildStr
.
length
() <
s
.
length
()) {
vaildStr
+=
Long
.
toString
(++
num
);
}
//누적합한 vaildStr과 s를 비교하여 일치하면 isVaild= true바꿔주고
// 가장 작은 숫자를 리턴해야하기때문에 for문을 바로 종료한다.
if
(
s
.
equals
(
vaildStr
)) {
isVaild
=
true
;
break
;
}
}
System
.
out
.
println
(
isVaild
?
"YES "
+
subStr
:
"NO"
);
}
public
static
void
main
(
String
[]
args
) {
/*
separateNumbers("1234");
System.out.print("ans: YES 1\n");
separateNumbers("91011");
System.out.print("ans: YES 9\n");
*/
separateNumbers
(
"99100"
);
System
.
out
.
print
(
"ans: YES 99
\n
"
);
separateNumbers
(
"101103"
);
System
.
out
.
print
(
"ans: NO
\n
"
);
separateNumbers
(
"010203"
);
System
.
out
.
print
(
"ans: NO
\n
"
);
separateNumbers
(
"13"
);
System
.
out
.
print
(
"ans: NO
\n
"
);
separateNumbers
(
"1"
);
System
.
out
.
print
(
"ans: NO
\n
"
);
}
}
You can’t perform that action at this time.