package com.eric.leetcode

class MaxAreaOfIsland {
    private lateinit var grid: Array<IntArray>
    private lateinit var seen: Array<BooleanArray>
    private var count = 0

    var dr = intArrayOf(1, -1, 0, 0)
    var dc = intArrayOf(0, 0, 1, -1)

    private fun getArea(r: Int, c: Int): Int {
        count++
        if (r < 0 || r >= grid.size || c < 0 || c >= grid[0].size ||
                seen[r][c] || grid[r][c] == 0)
            return 0
        seen[r][c] = true
        return 1 + getArea(r + 1, c) + getArea(r - 1, c) + getArea(r, c - 1) + getArea(r, c + 1)
    }

    fun maxAreaOfIsland(grid: Array<IntArray>): Int {
        this.grid = grid
        seen = Array(grid.size) { BooleanArray(grid[0].size) }
        var ans = 0
        for (r in grid.indices) {
            for (c in grid[0].indices) {
                count = 0
                ans = maxOf(ans, getArea(r, c))
                println("r: $r, c: $c, count: $count")
            }
        }
        return ans
    }

    fun maxAreaOfIsland2(grid: Array<IntArray>): Int {
        this.grid = grid
        seen = Array(grid.size) { BooleanArray(grid[0].size) }
        var queue = mutableListOf<IntArray>()
        var ans = 0
        for (r in grid.indices) {
            for (c in grid[0].indices) {
                if (!seen[r][c] && grid[r][c] == 1) {
                    var shape = 0
                    seen[r][c] = true
                    queue.add(intArrayOf(r, c))
                        while (queue.isNotEmpty()) {
                            var node = queue.removeAt(0)
                            var rIndex = node[0]
                            var cIndex = node[1]
                            shape++
                            for (k in 0..3) {
                                val nr = rIndex + dr[k]
                                val nc = cIndex + dc[k]
                                if (nr in grid.indices &&
                                        nc in grid[0].indices &&
                                        grid[nr][nc] == 1 && !seen[nr][nc]) {
                                    queue.add(intArrayOf(nr, nc))
                                    seen[nr][nc] = true
                                }
                            }
                        }
                    ans = maxOf(ans, shape)
                }
            }
        }
        return ans
    }
}

fun main(args: Array<String>) {
    val grid = arrayOf(
            intArrayOf(0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0),
            intArrayOf(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0),
            intArrayOf(0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0),
            intArrayOf(0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0),
            intArrayOf(0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0),
            intArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0),
            intArrayOf(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0),
            intArrayOf(0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0))
    val grid2 = arrayOf(intArrayOf(1, 1))
    val maxAreaOfIsland = MaxAreaOfIsland()
    println("result: " + maxAreaOfIsland.maxAreaOfIsland2(grid))
}

Compiled from "MaxAreaOfIsland.kt"
public final class com.eric.leetcode.MaxAreaOfIslandKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        8
       8: anewarray     #17                 // class "[I"
      11: astore_2
      12: aload_2
      13: iconst_0
      14: bipush        13
      16: newarray       int
      18: astore_3
      19: aload_3
      20: iconst_0
      21: iconst_0
      22: iastore
      23: aload_3
      24: iconst_1
      25: iconst_0
      26: iastore
      27: aload_3
      28: iconst_2
      29: iconst_1
      30: iastore
      31: aload_3
      32: iconst_3
      33: iconst_0
      34: iastore
      35: aload_3
      36: iconst_4
      37: iconst_0
      38: iastore
      39: aload_3
      40: iconst_5
      41: iconst_0
      42: iastore
      43: aload_3
      44: bipush        6
      46: iconst_0
      47: iastore
      48: aload_3
      49: bipush        7
      51: iconst_1
      52: iastore
      53: aload_3
      54: bipush        8
      56: iconst_0
      57: iastore
      58: aload_3
      59: bipush        9
      61: iconst_0
      62: iastore
      63: aload_3
      64: bipush        10
      66: iconst_0
      67: iastore
      68: aload_3
      69: bipush        11
      71: iconst_0
      72: iastore
      73: aload_3
      74: bipush        12
      76: iconst_0
      77: iastore
      78: aload_3
      79: aastore
      80: aload_2
      81: iconst_1
      82: bipush        13
      84: newarray       int
      86: astore_3
      87: aload_3
      88: iconst_0
      89: iconst_0
      90: iastore
      91: aload_3
      92: iconst_1
      93: iconst_0
      94: iastore
      95: aload_3
      96: iconst_2
      97: iconst_0
      98: iastore
      99: aload_3
     100: iconst_3
     101: iconst_0
     102: iastore
     103: aload_3
     104: iconst_4
     105: iconst_0
     106: iastore
     107: aload_3
     108: iconst_5
     109: iconst_0
     110: iastore
     111: aload_3
     112: bipush        6
     114: iconst_0
     115: iastore
     116: aload_3
     117: bipush        7
     119: iconst_1
     120: iastore
     121: aload_3
     122: bipush        8
     124: iconst_1
     125: iastore
     126: aload_3
     127: bipush        9
     129: iconst_1
     130: iastore
     131: aload_3
     132: bipush        10
     134: iconst_0
     135: iastore
     136: aload_3
     137: bipush        11
     139: iconst_0
     140: iastore
     141: aload_3
     142: bipush        12
     144: iconst_0
     145: iastore
     146: aload_3
     147: aastore
     148: aload_2
     149: iconst_2
     150: bipush        13
     152: newarray       int
     154: astore_3
     155: aload_3
     156: iconst_0
     157: iconst_0
     158: iastore
     159: aload_3
     160: iconst_1
     161: iconst_1
     162: iastore
     163: aload_3
     164: iconst_2
     165: iconst_1
     166: iastore
     167: aload_3
     168: iconst_3
     169: iconst_0
     170: iastore
     171: aload_3
     172: iconst_4
     173: iconst_1
     174: iastore
     175: aload_3
     176: iconst_5
     177: iconst_0
     178: iastore
     179: aload_3
     180: bipush        6
     182: iconst_0
     183: iastore
     184: aload_3
     185: bipush        7
     187: iconst_0
     188: iastore
     189: aload_3
     190: bipush        8
     192: iconst_0
     193: iastore
     194: aload_3
     195: bipush        9
     197: iconst_0
     198: iastore
     199: aload_3
     200: bipush        10
     202: iconst_0
     203: iastore
     204: aload_3
     205: bipush        11
     207: iconst_0
     208: iastore
     209: aload_3
     210: bipush        12
     212: iconst_0
     213: iastore
     214: aload_3
     215: aastore
     216: aload_2
     217: iconst_3
     218: bipush        13
     220: newarray       int
     222: astore_3
     223: aload_3
     224: iconst_0
     225: iconst_0
     226: iastore
     227: aload_3
     228: iconst_1
     229: iconst_1
     230: iastore
     231: aload_3
     232: iconst_2
     233: iconst_0
     234: iastore
     235: aload_3
     236: iconst_3
     237: iconst_0
     238: iastore
     239: aload_3
     240: iconst_4
     241: iconst_1
     242: iastore
     243: aload_3
     244: iconst_5
     245: iconst_1
     246: iastore
     247: aload_3
     248: bipush        6
     250: iconst_0
     251: iastore
     252: aload_3
     253: bipush        7
     255: iconst_0
     256: iastore
     257: aload_3
     258: bipush        8
     260: iconst_1
     261: iastore
     262: aload_3
     263: bipush        9
     265: iconst_1
     266: iastore
     267: aload_3
     268: bipush        10
     270: iconst_1
     271: iastore
     272: aload_3
     273: bipush        11
     275: iconst_0
     276: iastore
     277: aload_3
     278: bipush        12
     280: iconst_0
     281: iastore
     282: aload_3
     283: aastore
     284: aload_2
     285: iconst_4
     286: bipush        13
     288: newarray       int
     290: astore_3
     291: aload_3
     292: iconst_0
     293: iconst_0
     294: iastore
     295: aload_3
     296: iconst_1
     297: iconst_1
     298: iastore
     299: aload_3
     300: iconst_2
     301: iconst_0
     302: iastore
     303: aload_3
     304: iconst_3
     305: iconst_0
     306: iastore
     307: aload_3
     308: iconst_4
     309: iconst_1
     310: iastore
     311: aload_3
     312: iconst_5
     313: iconst_1
     314: iastore
     315: aload_3
     316: bipush        6
     318: iconst_0
     319: iastore
     320: aload_3
     321: bipush        7
     323: iconst_0
     324: iastore
     325: aload_3
     326: bipush        8
     328: iconst_1
     329: iastore
     330: aload_3
     331: bipush        9
     333: iconst_1
     334: iastore
     335: aload_3
     336: bipush        10
     338: iconst_1
     339: iastore
     340: aload_3
     341: bipush        11
     343: iconst_0
     344: iastore
     345: aload_3
     346: bipush        12
     348: iconst_0
     349: iastore
     350: aload_3
     351: aastore
     352: aload_2
     353: iconst_5
     354: bipush        13
     356: newarray       int
     358: astore_3
     359: aload_3
     360: iconst_0
     361: iconst_0
     362: iastore
     363: aload_3
     364: iconst_1
     365: iconst_0
     366: iastore
     367: aload_3
     368: iconst_2
     369: iconst_0
     370: iastore
     371: aload_3
     372: iconst_3
     373: iconst_0
     374: iastore
     375: aload_3
     376: iconst_4
     377: iconst_0
     378: iastore
     379: aload_3
     380: iconst_5
     381: iconst_0
     382: iastore
     383: aload_3
     384: bipush        6
     386: iconst_0
     387: iastore
     388: aload_3
     389: bipush        7
     391: iconst_0
     392: iastore
     393: aload_3
     394: bipush        8
     396: iconst_0
     397: iastore
     398: aload_3
     399: bipush        9
     401: iconst_0
     402: iastore
     403: aload_3
     404: bipush        10
     406: iconst_1
     407: iastore
     408: aload_3
     409: bipush        11
     411: iconst_0
     412: iastore
     413: aload_3
     414: bipush        12
     416: iconst_0
     417: iastore
     418: aload_3
     419: aastore
     420: aload_2
     421: bipush        6
     423: bipush        13
     425: newarray       int
     427: astore_3
     428: aload_3
     429: iconst_0
     430: iconst_0
     431: iastore
     432: aload_3
     433: iconst_1
     434: iconst_0
     435: iastore
     436: aload_3
     437: iconst_2
     438: iconst_0
     439: iastore
     440: aload_3
     441: iconst_3
     442: iconst_0
     443: iastore
     444: aload_3
     445: iconst_4
     446: iconst_0
     447: iastore
     448: aload_3
     449: iconst_5
     450: iconst_0
     451: iastore
     452: aload_3
     453: bipush        6
     455: iconst_0
     456: iastore
     457: aload_3
     458: bipush        7
     460: iconst_1
     461: iastore
     462: aload_3
     463: bipush        8
     465: iconst_1
     466: iastore
     467: aload_3
     468: bipush        9
     470: iconst_1
     471: iastore
     472: aload_3
     473: bipush        10
     475: iconst_0
     476: iastore
     477: aload_3
     478: bipush        11
     480: iconst_0
     481: iastore
     482: aload_3
     483: bipush        12
     485: iconst_0
     486: iastore
     487: aload_3
     488: aastore
     489: aload_2
     490: bipush        7
     492: bipush        13
     494: newarray       int
     496: astore_3
     497: aload_3
     498: iconst_0
     499: iconst_0
     500: iastore
     501: aload_3
     502: iconst_1
     503: iconst_0
     504: iastore
     505: aload_3
     506: iconst_2
     507: iconst_0
     508: iastore
     509: aload_3
     510: iconst_3
     511: iconst_0
     512: iastore
     513: aload_3
     514: iconst_4
     515: iconst_0
     516: iastore
     517: aload_3
     518: iconst_5
     519: iconst_0
     520: iastore
     521: aload_3
     522: bipush        6
     524: iconst_0
     525: iastore
     526: aload_3
     527: bipush        7
     529: iconst_1
     530: iastore
     531: aload_3
     532: bipush        8
     534: iconst_1
     535: iastore
     536: aload_3
     537: bipush        9
     539: iconst_0
     540: iastore
     541: aload_3
     542: bipush        10
     544: iconst_0
     545: iastore
     546: aload_3
     547: bipush        11
     549: iconst_0
     550: iastore
     551: aload_3
     552: bipush        12
     554: iconst_0
     555: iastore
     556: aload_3
     557: aastore
     558: aload_2
     559: astore_1
     560: iconst_1
     561: anewarray     #17                 // class "[I"
     564: astore_3
     565: aload_3
     566: iconst_0
     567: iconst_2
     568: newarray       int
     570: astore        4
     572: aload         4
     574: iconst_0
     575: iconst_1
     576: iastore
     577: aload         4
     579: iconst_1
     580: iconst_1
     581: iastore
     582: aload         4
     584: aastore
     585: aload_3
     586: astore_2
     587: new           #19                 // class com/eric/leetcode/MaxAreaOfIsland
     590: dup
     591: invokespecial #23                 // Method com/eric/leetcode/MaxAreaOfIsland."<init>":()V
     594: astore_3
     595: new           #25                 // class java/lang/StringBuilder
     598: dup
     599: invokespecial #26                 // Method java/lang/StringBuilder."<init>":()V
     602: ldc           #28                 // String result:
     604: invokevirtual #32                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     607: aload_3
     608: aload_1
     609: invokevirtual #36                 // Method com/eric/leetcode/MaxAreaOfIsland.maxAreaOfIsland2:([[I)I
     612: invokevirtual #39                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     615: invokevirtual #43                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     618: getstatic     #49                 // Field java/lang/System.out:Ljava/io/PrintStream;
     621: swap
     622: invokevirtual #55                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     625: return
}

Compiled from "MaxAreaOfIsland.kt"
public final class com.eric.leetcode.MaxAreaOfIsland {
  private int[][] grid;

  private boolean[][] seen;

  private int count;

  private int[] dr;

  private int[] dc;

  public com.eric.leetcode.MaxAreaOfIsland();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_4
       6: newarray       int
       8: astore_1
       9: aload_1
      10: iconst_0
      11: iconst_1
      12: iastore
      13: aload_1
      14: iconst_1
      15: iconst_m1
      16: iastore
      17: aload_1
      18: iconst_2
      19: iconst_0
      20: iastore
      21: aload_1
      22: iconst_3
      23: iconst_0
      24: iastore
      25: aload_1
      26: putfield      #12                 // Field dr:[I
      29: aload_0
      30: iconst_4
      31: newarray       int
      33: astore_1
      34: aload_1
      35: iconst_0
      36: iconst_0
      37: iastore
      38: aload_1
      39: iconst_1
      40: iconst_0
      41: iastore
      42: aload_1
      43: iconst_2
      44: iconst_1
      45: iastore
      46: aload_1
      47: iconst_3
      48: iconst_m1
      49: iastore
      50: aload_1
      51: putfield      #15                 // Field dc:[I
      54: return

  public final int[] getDr();
    Code:
       0: aload_0
       1: getfield      #12                 // Field dr:[I
       4: areturn

  public final void setDr(int[]);
    Code:
       0: aload_1
       1: ldc           #24                 // String <set-?>
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #12                 // Field dr:[I
      11: return

  public final int[] getDc();
    Code:
       0: aload_0
       1: getfield      #15                 // Field dc:[I
       4: areturn

  public final void setDc(int[]);
    Code:
       0: aload_1
       1: ldc           #24                 // String <set-?>
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #15                 // Field dc:[I
      11: return

  private final int getArea(int, int);
    Code:
       0: aload_0
       1: getfield      #38                 // Field count:I
       4: istore_3
       5: aload_0
       6: iload_3
       7: iconst_1
       8: iadd
       9: putfield      #38                 // Field count:I
      12: iload_1
      13: iflt          109
      16: iload_1
      17: aload_0
      18: getfield      #42                 // Field grid:[[I
      21: dup
      22: ifnonnull     32
      25: pop
      26: ldc           #43                 // String grid
      28: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      31: aconst_null
      32: checkcast     #49                 // class "[Ljava/lang/Object;"
      35: arraylength
      36: if_icmpge     109
      39: iload_2
      40: iflt          109
      43: iload_2
      44: aload_0
      45: getfield      #42                 // Field grid:[[I
      48: dup
      49: ifnonnull     59
      52: pop
      53: ldc           #43                 // String grid
      55: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      58: aconst_null
      59: iconst_0
      60: aaload
      61: arraylength
      62: if_icmpge     109
      65: aload_0
      66: getfield      #53                 // Field seen:[[Z
      69: dup
      70: ifnonnull     80
      73: pop
      74: ldc           #54                 // String seen
      76: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      79: aconst_null
      80: iload_1
      81: aaload
      82: iload_2
      83: baload
      84: ifne          109
      87: aload_0
      88: getfield      #42                 // Field grid:[[I
      91: dup
      92: ifnonnull     102
      95: pop
      96: ldc           #43                 // String grid
      98: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     101: aconst_null
     102: iload_1
     103: aaload
     104: iload_2
     105: iaload
     106: ifne          111
     109: iconst_0
     110: ireturn
     111: aload_0
     112: getfield      #53                 // Field seen:[[Z
     115: dup
     116: ifnonnull     126
     119: pop
     120: ldc           #54                 // String seen
     122: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     125: aconst_null
     126: iload_1
     127: aaload
     128: iload_2
     129: iconst_1
     130: bastore
     131: iconst_1
     132: aload_0
     133: iload_1
     134: iconst_1
     135: iadd
     136: iload_2
     137: invokespecial #56                 // Method getArea:(II)I
     140: iadd
     141: aload_0
     142: iload_1
     143: iconst_1
     144: isub
     145: iload_2
     146: invokespecial #56                 // Method getArea:(II)I
     149: iadd
     150: aload_0
     151: iload_1
     152: iload_2
     153: iconst_1
     154: isub
     155: invokespecial #56                 // Method getArea:(II)I
     158: iadd
     159: aload_0
     160: iload_1
     161: iload_2
     162: iconst_1
     163: iadd
     164: invokespecial #56                 // Method getArea:(II)I
     167: iadd
     168: ireturn

  public final int maxAreaOfIsland(int[][]);
    Code:
       0: aload_1
       1: ldc           #43                 // String grid
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #42                 // Field grid:[[I
      11: aload_0
      12: iconst_0
      13: istore_2
      14: aload_1
      15: checkcast     #49                 // class "[Ljava/lang/Object;"
      18: arraylength
      19: istore_3
      20: iload_3
      21: anewarray     #64                 // class "[Z"
      24: astore        4
      26: astore        7
      28: iload_2
      29: iload_3
      30: if_icmpge     53
      33: iload_2
      34: istore        5
      36: aload         4
      38: iload         5
      40: aload_1
      41: iconst_0
      42: aaload
      43: arraylength
      44: newarray       boolean
      46: aastore
      47: iinc          2, 1
      50: goto          28
      53: aload         7
      55: aload         4
      57: putfield      #53                 // Field seen:[[Z
      60: iconst_0
      61: istore_2
      62: iconst_0
      63: istore_3
      64: aload_1
      65: checkcast     #49                 // class "[Ljava/lang/Object;"
      68: arraylength
      69: istore        4
      71: iload_3
      72: iload         4
      74: if_icmpge     170
      77: iconst_0
      78: istore        5
      80: aload_1
      81: iconst_0
      82: aaload
      83: arraylength
      84: istore        6
      86: iload         5
      88: iload         6
      90: if_icmpge     164
      93: aload_0
      94: iconst_0
      95: putfield      #38                 // Field count:I
      98: iload_2
      99: aload_0
     100: iload_3
     101: iload         5
     103: invokespecial #56                 // Method getArea:(II)I
     106: invokestatic  #69                 // Method java/lang/Math.max:(II)I
     109: istore_2
     110: new           #71                 // class java/lang/StringBuilder
     113: dup
     114: invokespecial #72                 // Method java/lang/StringBuilder."<init>":()V
     117: ldc           #74                 // String r:
     119: invokevirtual #78                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     122: iload_3
     123: invokevirtual #81                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     126: ldc           #83                 // String , c:
     128: invokevirtual #78                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     131: iload         5
     133: invokevirtual #81                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     136: ldc           #85                 // String , count:
     138: invokevirtual #78                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     141: aload_0
     142: getfield      #38                 // Field count:I
     145: invokevirtual #81                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     148: invokevirtual #89                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     151: getstatic     #95                 // Field java/lang/System.out:Ljava/io/PrintStream;
     154: swap
     155: invokevirtual #101                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     158: iinc          5, 1
     161: goto          86
     164: iinc          3, 1
     167: goto          71
     170: iload_2
     171: ireturn

  public final int maxAreaOfIsland2(int[][]);
    Code:
       0: aload_1
       1: ldc           #43                 // String grid
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #42                 // Field grid:[[I
      11: aload_0
      12: iconst_0
      13: istore_2
      14: aload_1
      15: checkcast     #49                 // class "[Ljava/lang/Object;"
      18: arraylength
      19: istore_3
      20: iload_3
      21: anewarray     #64                 // class "[Z"
      24: astore        4
      26: astore        16
      28: iload_2
      29: iload_3
      30: if_icmpge     53
      33: iload_2
      34: istore        5
      36: aload         4
      38: iload         5
      40: aload_1
      41: iconst_0
      42: aaload
      43: arraylength
      44: newarray       boolean
      46: aastore
      47: iinc          2, 1
      50: goto          28
      53: aload         16
      55: aload         4
      57: putfield      #53                 // Field seen:[[Z
      60: new           #105                // class java/util/ArrayList
      63: dup
      64: invokespecial #106                // Method java/util/ArrayList."<init>":()V
      67: checkcast     #108                // class java/util/List
      70: astore_2
      71: iconst_0
      72: istore_3
      73: iconst_0
      74: istore        4
      76: aload_1
      77: checkcast     #49                 // class "[Ljava/lang/Object;"
      80: arraylength
      81: istore        5
      83: iload         4
      85: iload         5
      87: if_icmpge     435
      90: iconst_0
      91: istore        6
      93: aload_1
      94: iconst_0
      95: aaload
      96: arraylength
      97: istore        7
      99: iload         6
     101: iload         7
     103: if_icmpge     429
     106: aload_0
     107: getfield      #53                 // Field seen:[[Z
     110: dup
     111: ifnonnull     121
     114: pop
     115: ldc           #54                 // String seen
     117: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     120: aconst_null
     121: iload         4
     123: aaload
     124: iload         6
     126: baload
     127: ifne          423
     130: aload_1
     131: iload         4
     133: aaload
     134: iload         6
     136: iaload
     137: iconst_1
     138: if_icmpne     423
     141: iconst_0
     142: istore        8
     144: aload_0
     145: getfield      #53                 // Field seen:[[Z
     148: dup
     149: ifnonnull     159
     152: pop
     153: ldc           #54                 // String seen
     155: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     158: aconst_null
     159: iload         4
     161: aaload
     162: iload         6
     164: iconst_1
     165: bastore
     166: aload_2
     167: iconst_2
     168: newarray       int
     170: astore        9
     172: aload         9
     174: iconst_0
     175: iload         4
     177: iastore
     178: aload         9
     180: iconst_1
     181: iload         6
     183: iastore
     184: aload         9
     186: invokeinterface #112,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     191: pop
     192: aload_2
     193: checkcast     #114                // class java/util/Collection
     196: invokeinterface #118,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     201: ifne          208
     204: iconst_1
     205: goto          209
     208: iconst_0
     209: ifeq          416
     212: aload_2
     213: iconst_0
     214: invokeinterface #122,  2          // InterfaceMethod java/util/List.remove:(I)Ljava/lang/Object;
     219: checkcast     #123                // class "[I"
     222: astore        9
     224: aload         9
     226: iconst_0
     227: iaload
     228: istore        10
     230: aload         9
     232: iconst_1
     233: iaload
     234: istore        11
     236: iinc          8, 1
     239: iconst_0
     240: istore        12
     242: iload         12
     244: iconst_4
     245: if_icmpge     192
     248: iload         10
     250: aload_0
     251: getfield      #12                 // Field dr:[I
     254: iload         12
     256: iaload
     257: iadd
     258: istore        13
     260: iload         11
     262: aload_0
     263: getfield      #15                 // Field dc:[I
     266: iload         12
     268: iaload
     269: iadd
     270: istore        14
     272: iconst_0
     273: iload         13
     275: if_icmpgt     296
     278: iload         13
     280: aload_1
     281: checkcast     #49                 // class "[Ljava/lang/Object;"
     284: arraylength
     285: if_icmpge     292
     288: iconst_1
     289: goto          297
     292: iconst_0
     293: goto          297
     296: iconst_0
     297: ifeq          410
     300: iconst_0
     301: iload         14
     303: if_icmpgt     323
     306: iload         14
     308: aload_1
     309: iconst_0
     310: aaload
     311: arraylength
     312: if_icmpge     319
     315: iconst_1
     316: goto          324
     319: iconst_0
     320: goto          324
     323: iconst_0
     324: ifeq          410
     327: aload_1
     328: iload         13
     330: aaload
     331: iload         14
     333: iaload
     334: iconst_1
     335: if_icmpne     410
     338: aload_0
     339: getfield      #53                 // Field seen:[[Z
     342: dup
     343: ifnonnull     353
     346: pop
     347: ldc           #54                 // String seen
     349: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     352: aconst_null
     353: iload         13
     355: aaload
     356: iload         14
     358: baload
     359: ifne          410
     362: aload_2
     363: iconst_2
     364: newarray       int
     366: astore        15
     368: aload         15
     370: iconst_0
     371: iload         13
     373: iastore
     374: aload         15
     376: iconst_1
     377: iload         14
     379: iastore
     380: aload         15
     382: invokeinterface #112,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     387: pop
     388: aload_0
     389: getfield      #53                 // Field seen:[[Z
     392: dup
     393: ifnonnull     403
     396: pop
     397: ldc           #54                 // String seen
     399: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     402: aconst_null
     403: iload         13
     405: aaload
     406: iload         14
     408: iconst_1
     409: bastore
     410: iinc          12, 1
     413: goto          242
     416: iload_3
     417: iload         8
     419: invokestatic  #69                 // Method java/lang/Math.max:(II)I
     422: istore_3
     423: iinc          6, 1
     426: goto          99
     429: iinc          4, 1
     432: goto          83
     435: iload_3
     436: ireturn
}


package com.eric.leetcode

class SatisfiabilityOfEqualityEquations {
    private class UnionFind(n: Int) {
        private val parent: Array<Int> = Array(n) {
            it
        }

        fun find(input: Int): Int {
            var x = input
            while (x != parent[x]) {
                parent[x] = parent[parent[x]]
                x = parent[x]
            }
            return x
        }

        fun union(x:Int, y:Int) {
            val rootX = find(x)
            val rootY = find(y)
            parent[rootX] = rootY
        }

        fun isConnected(x: Int, y: Int): Boolean {
            return find(x) == find(y)
        }
    }

    fun equationsPossible(equations: Array<String>): Boolean {
        val unionFind = UnionFind(26)
        for (equation in equations) {
            if (equation[1] == '=') {
                val index1 = equation[0] - 'a'
                val index2 = equation[3] - 'a'
                unionFind.union(index1, index2)
            }
        }

        for (equation in equations) {
            if (equation[1] == '!') {
                val index1 = equation[0] - 'a'
                val index2 = equation[3] - 'a'

                if (unionFind.isConnected(index1, index2)) {
                    // 如果合并失败，表示等式有矛盾，根据题意，返回 false
                    return false
                }
            }
        }
        // 如果检查了所有不等式，都没有发现矛盾，返回 true
        return true
    }
}

fun main(args: Array<String>) {
    val equations = arrayOf("a==c","c==d","d!=a")
    val satisfiabilityOfEqualityEquations = SatisfiabilityOfEqualityEquations()
    println(satisfiabilityOfEqualityEquations.equationsPossible(equations))
}

Compiled from "SatisfiabilityOfEqualityEquations.kt"
final class com.eric.leetcode.SatisfiabilityOfEqualityEquations$UnionFind {
  private final java.lang.Integer[] parent;

  public com.eric.leetcode.SatisfiabilityOfEqualityEquations$UnionFind(int);
    Code:
       0: aload_0
       1: invokespecial #9                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_0
       6: istore_2
       7: iload_1
       8: anewarray     #11                 // class java/lang/Integer
      11: astore_3
      12: astore        5
      14: iload_2
      15: iload_1
      16: if_icmpge     37
      19: iload_2
      20: istore        4
      22: aload_3
      23: iload         4
      25: iload         4
      27: invokestatic  #15                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      30: aastore
      31: iinc          2, 1
      34: goto          14
      37: aload         5
      39: aload_3
      40: putfield      #19                 // Field parent:[Ljava/lang/Integer;
      43: return

  public final int find(int);
    Code:
       0: iload_1
       1: istore_2
       2: iload_2
       3: aload_0
       4: getfield      #19                 // Field parent:[Ljava/lang/Integer;
       7: iload_2
       8: aaload
       9: invokevirtual #30                 // Method java/lang/Integer.intValue:()I
      12: if_icmpeq     48
      15: aload_0
      16: getfield      #19                 // Field parent:[Ljava/lang/Integer;
      19: iload_2
      20: aload_0
      21: getfield      #19                 // Field parent:[Ljava/lang/Integer;
      24: aload_0
      25: getfield      #19                 // Field parent:[Ljava/lang/Integer;
      28: iload_2
      29: aaload
      30: invokevirtual #30                 // Method java/lang/Integer.intValue:()I
      33: aaload
      34: aastore
      35: aload_0
      36: getfield      #19                 // Field parent:[Ljava/lang/Integer;
      39: iload_2
      40: aaload
      41: invokevirtual #30                 // Method java/lang/Integer.intValue:()I
      44: istore_2
      45: goto          2
      48: iload_2
      49: ireturn

  public final void union(int, int);
    Code:
       0: aload_0
       1: iload_1
       2: invokevirtual #36                 // Method find:(I)I
       5: istore_3
       6: aload_0
       7: iload_2
       8: invokevirtual #36                 // Method find:(I)I
      11: istore        4
      13: aload_0
      14: getfield      #19                 // Field parent:[Ljava/lang/Integer;
      17: iload_3
      18: iload         4
      20: invokestatic  #15                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      23: aastore
      24: return

  public final boolean isConnected(int, int);
    Code:
       0: aload_0
       1: iload_1
       2: invokevirtual #36                 // Method find:(I)I
       5: aload_0
       6: iload_2
       7: invokevirtual #36                 // Method find:(I)I
      10: if_icmpne     17
      13: iconst_1
      14: goto          18
      17: iconst_0
      18: ireturn
}

Compiled from "SatisfiabilityOfEqualityEquations.kt"
public final class com.eric.leetcode.SatisfiabilityOfEqualityEquations {
  public com.eric.leetcode.SatisfiabilityOfEqualityEquations();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final boolean equationsPossible(java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String equations
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #23                 // class com/eric/leetcode/SatisfiabilityOfEqualityEquations$UnionFind
       9: dup
      10: bipush        26
      12: invokespecial #26                 // Method com/eric/leetcode/SatisfiabilityOfEqualityEquations$UnionFind."<init>":(I)V
      15: astore_2
      16: iconst_0
      17: istore_3
      18: aload_1
      19: arraylength
      20: istore        4
      22: iload_3
      23: iload         4
      25: if_icmpge     80
      28: aload_1
      29: iload_3
      30: aaload
      31: astore        5
      33: aload         5
      35: iconst_1
      36: invokevirtual #32                 // Method java/lang/String.charAt:(I)C
      39: bipush        61
      41: if_icmpne     74
      44: aload         5
      46: iconst_0
      47: invokevirtual #32                 // Method java/lang/String.charAt:(I)C
      50: bipush        97
      52: isub
      53: istore        6
      55: aload         5
      57: iconst_3
      58: invokevirtual #32                 // Method java/lang/String.charAt:(I)C
      61: bipush        97
      63: isub
      64: istore        7
      66: aload_2
      67: iload         6
      69: iload         7
      71: invokevirtual #36                 // Method com/eric/leetcode/SatisfiabilityOfEqualityEquations$UnionFind.union:(II)V
      74: iinc          3, 1
      77: goto          22
      80: iconst_0
      81: istore_3
      82: aload_1
      83: arraylength
      84: istore        4
      86: iload_3
      87: iload         4
      89: if_icmpge     149
      92: aload_1
      93: iload_3
      94: aaload
      95: astore        5
      97: aload         5
      99: iconst_1
     100: invokevirtual #32                 // Method java/lang/String.charAt:(I)C
     103: bipush        33
     105: if_icmpne     143
     108: aload         5
     110: iconst_0
     111: invokevirtual #32                 // Method java/lang/String.charAt:(I)C
     114: bipush        97
     116: isub
     117: istore        6
     119: aload         5
     121: iconst_3
     122: invokevirtual #32                 // Method java/lang/String.charAt:(I)C
     125: bipush        97
     127: isub
     128: istore        7
     130: aload_2
     131: iload         6
     133: iload         7
     135: invokevirtual #40                 // Method com/eric/leetcode/SatisfiabilityOfEqualityEquations$UnionFind.isConnected:(II)Z
     138: ifeq          143
     141: iconst_0
     142: ireturn
     143: iinc          3, 1
     146: goto          86
     149: iconst_1
     150: ireturn
}

Compiled from "SatisfiabilityOfEqualityEquations.kt"
public final class com.eric.leetcode.SatisfiabilityOfEqualityEquationsKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_3
       7: anewarray     #17                 // class java/lang/String
      10: astore_2
      11: aload_2
      12: iconst_0
      13: ldc           #19                 // String a==c
      15: aastore
      16: aload_2
      17: iconst_1
      18: ldc           #21                 // String c==d
      20: aastore
      21: aload_2
      22: iconst_2
      23: ldc           #23                 // String d!=a
      25: aastore
      26: aload_2
      27: astore_1
      28: new           #25                 // class com/eric/leetcode/SatisfiabilityOfEqualityEquations
      31: dup
      32: invokespecial #29                 // Method com/eric/leetcode/SatisfiabilityOfEqualityEquations."<init>":()V
      35: astore_2
      36: aload_2
      37: aload_1
      38: invokevirtual #33                 // Method com/eric/leetcode/SatisfiabilityOfEqualityEquations.equationsPossible:([Ljava/lang/String;)Z
      41: istore_3
      42: getstatic     #39                 // Field java/lang/System.out:Ljava/io/PrintStream;
      45: iload_3
      46: invokevirtual #45                 // Method java/io/PrintStream.println:(Z)V
      49: return
}


package com.eric.leetcode

class MinimumPathSum {
    fun minPathSum(grid: Array<IntArray>): Int {
        val dp = Array(grid.size) { Array(grid[0].size) { 0 } }
        for (i in grid.indices) {
            for (j in grid[0].indices) {
                if (i == 0 && j == 0) {
                    dp[0][0] = grid[0][0]
                } else if (i == 0) {
                    dp[0][j] = dp[0][j - 1] + grid[i][j]
                } else if (j == 0) {
                    dp[i][0] = dp[i - 1][0] + grid[i][j]
                } else {
                    dp[i][j] = minOf(dp[i - 1][j] + grid[i][j], dp[i][j - 1] + grid[i][j])
                }
            }
        }
        return dp[dp.lastIndex][dp[0].lastIndex]
    }
}

fun main(args: Array<String>) {
    val input = arrayOf(intArrayOf(1, 3, 1), intArrayOf(1, 5, 1), intArrayOf(4, 2, 1))
    val minimumPathSum = MinimumPathSum()
    println(minimumPathSum.minPathSum(input))
}

Compiled from "MinimumPathSum.kt"
public final class com.eric.leetcode.MinimumPathSumKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_3
       7: anewarray     #17                 // class "[I"
      10: astore_2
      11: aload_2
      12: iconst_0
      13: iconst_3
      14: newarray       int
      16: astore_3
      17: aload_3
      18: iconst_0
      19: iconst_1
      20: iastore
      21: aload_3
      22: iconst_1
      23: iconst_3
      24: iastore
      25: aload_3
      26: iconst_2
      27: iconst_1
      28: iastore
      29: aload_3
      30: aastore
      31: aload_2
      32: iconst_1
      33: iconst_3
      34: newarray       int
      36: astore_3
      37: aload_3
      38: iconst_0
      39: iconst_1
      40: iastore
      41: aload_3
      42: iconst_1
      43: iconst_5
      44: iastore
      45: aload_3
      46: iconst_2
      47: iconst_1
      48: iastore
      49: aload_3
      50: aastore
      51: aload_2
      52: iconst_2
      53: iconst_3
      54: newarray       int
      56: astore_3
      57: aload_3
      58: iconst_0
      59: iconst_4
      60: iastore
      61: aload_3
      62: iconst_1
      63: iconst_2
      64: iastore
      65: aload_3
      66: iconst_2
      67: iconst_1
      68: iastore
      69: aload_3
      70: aastore
      71: aload_2
      72: astore_1
      73: new           #19                 // class com/eric/leetcode/MinimumPathSum
      76: dup
      77: invokespecial #23                 // Method com/eric/leetcode/MinimumPathSum."<init>":()V
      80: astore_2
      81: aload_2
      82: aload_1
      83: invokevirtual #27                 // Method com/eric/leetcode/MinimumPathSum.minPathSum:([[I)I
      86: istore_3
      87: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
      90: iload_3
      91: invokevirtual #39                 // Method java/io/PrintStream.println:(I)V
      94: return
}

Compiled from "MinimumPathSum.kt"
public final class com.eric.leetcode.MinimumPathSum {
  public com.eric.leetcode.MinimumPathSum();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int minPathSum(int[][]);
    Code:
       0: aload_1
       1: ldc           #15                 // String grid
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_3
       8: aload_1
       9: checkcast     #23                 // class "[Ljava/lang/Object;"
      12: arraylength
      13: istore        4
      15: iload         4
      17: anewarray     #25                 // class "[Ljava/lang/Integer;"
      20: astore        5
      22: iload_3
      23: iload         4
      25: if_icmpge     94
      28: iload_3
      29: istore        6
      31: aload         5
      33: iload         6
      35: iconst_0
      36: istore        7
      38: aload_1
      39: iconst_0
      40: aaload
      41: arraylength
      42: istore        8
      44: iload         8
      46: anewarray     #27                 // class java/lang/Integer
      49: astore        9
      51: istore        12
      53: astore        11
      55: iload         7
      57: iload         8
      59: if_icmpge     81
      62: iload         7
      64: istore        10
      66: aload         9
      68: iload         10
      70: iconst_0
      71: invokestatic  #31                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      74: aastore
      75: iinc          7, 1
      78: goto          55
      81: aload         11
      83: iload         12
      85: aload         9
      87: aastore
      88: iinc          3, 1
      91: goto          22
      94: aload         5
      96: astore_2
      97: iconst_0
      98: istore_3
      99: aload_1
     100: checkcast     #23                 // class "[Ljava/lang/Object;"
     103: arraylength
     104: istore        4
     106: iload_3
     107: iload         4
     109: if_icmpge     280
     112: iconst_0
     113: istore        5
     115: aload_1
     116: iconst_0
     117: aaload
     118: arraylength
     119: istore        6
     121: iload         5
     123: iload         6
     125: if_icmpge     274
     128: iload_3
     129: ifne          153
     132: iload         5
     134: ifne          153
     137: aload_2
     138: iconst_0
     139: aaload
     140: iconst_0
     141: aload_1
     142: iconst_0
     143: aaload
     144: iconst_0
     145: iaload
     146: invokestatic  #31                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     149: aastore
     150: goto          268
     153: iload_3
     154: ifne          187
     157: aload_2
     158: iconst_0
     159: aaload
     160: iload         5
     162: aload_2
     163: iconst_0
     164: aaload
     165: iload         5
     167: iconst_1
     168: isub
     169: aaload
     170: invokevirtual #35                 // Method java/lang/Integer.intValue:()I
     173: aload_1
     174: iload_3
     175: aaload
     176: iload         5
     178: iaload
     179: iadd
     180: invokestatic  #31                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     183: aastore
     184: goto          268
     187: iload         5
     189: ifne          220
     192: aload_2
     193: iload_3
     194: aaload
     195: iconst_0
     196: aload_2
     197: iload_3
     198: iconst_1
     199: isub
     200: aaload
     201: iconst_0
     202: aaload
     203: invokevirtual #35                 // Method java/lang/Integer.intValue:()I
     206: aload_1
     207: iload_3
     208: aaload
     209: iload         5
     211: iaload
     212: iadd
     213: invokestatic  #31                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     216: aastore
     217: goto          268
     220: aload_2
     221: iload_3
     222: aaload
     223: iload         5
     225: aload_2
     226: iload_3
     227: iconst_1
     228: isub
     229: aaload
     230: iload         5
     232: aaload
     233: invokevirtual #35                 // Method java/lang/Integer.intValue:()I
     236: aload_1
     237: iload_3
     238: aaload
     239: iload         5
     241: iaload
     242: iadd
     243: aload_2
     244: iload_3
     245: aaload
     246: iload         5
     248: iconst_1
     249: isub
     250: aaload
     251: invokevirtual #35                 // Method java/lang/Integer.intValue:()I
     254: aload_1
     255: iload_3
     256: aaload
     257: iload         5
     259: iaload
     260: iadd
     261: invokestatic  #41                 // Method java/lang/Math.min:(II)I
     264: invokestatic  #31                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     267: aastore
     268: iinc          5, 1
     271: goto          121
     274: iinc          3, 1
     277: goto          106
     280: aload_2
     281: aload_2
     282: checkcast     #23                 // class "[Ljava/lang/Object;"
     285: invokestatic  #47                 // Method kotlin/collections/ArraysKt.getLastIndex:([Ljava/lang/Object;)I
     288: aaload
     289: aload_2
     290: iconst_0
     291: aaload
     292: invokestatic  #47                 // Method kotlin/collections/ArraysKt.getLastIndex:([Ljava/lang/Object;)I
     295: aaload
     296: invokevirtual #35                 // Method java/lang/Integer.intValue:()I
     299: ireturn
}


package com.eric.leetcode

class FindTheShortestSuperstring {
    fun shortestSuperstring(A: Array<String>): String {
        val list = A.toMutableList()
        while (list.size > 1) {
            var x = 0
            var y = 1
            for (iv in list.withIndex()) {
                for (index in iv.index + 1..list.lastIndex) {
                    if (getOverlapping(iv.value, list[index]) > getOverlapping(list[x], list[y])) {
                        x = iv.index
                        y = index
                    }
                }
            }
            val a = list[x]
            val b = list[y]
            list.remove(a)
            list.remove(b)
            list.add(getOverlappingString(a, b))
        }
        return list.single()
    }

    private fun getOverlapping(x: String, y: String): Int {
        return x.length + y.length - getOverlappingString(x, y).length
    }

    private fun getOverlappingString(str1: String, str2: String): String {
        var result = ""
        loop@ for (iv in str1.withIndex()) {
            if (iv.value != str2[0]) {
                continue
            }
            for (index in iv.index..str1.lastIndex) {
                if (str1[index] != str2[index - iv.index]) {
                     continue@loop
                }
            }
            result = str1 + str2.substring(str1.length-iv.index..str2.lastIndex)
            break
        }
        loop@ for (iv in str2.withIndex()) {
            if (iv.value != str1[0]) {
                continue
            }
            for (index in iv.index..str2.lastIndex) {
                if (str2[index] != str1[index - iv.index]) {
                    continue@loop
                }
            }
            val newResult = str2 + str1.substring(str2.length-iv.index..str1.lastIndex)
            if (newResult.length < result.length || result == "") {
                result = newResult
            }
            break
        }
        if (result == "") {
            result = str1 + str2
        }
        return result
    }
}

fun main(args: Array<String>) {
    val inputArray = arrayOf("sssv","svq","dskss","sksss")
    val findTheShortestSuperstring = FindTheShortestSuperstring()
    println(findTheShortestSuperstring.shortestSuperstring(inputArray))
}

Compiled from "FindTheShortestSuperstring.kt"
public final class com.eric.leetcode.FindTheShortestSuperstring {
  public com.eric.leetcode.FindTheShortestSuperstring();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final java.lang.String shortestSuperstring(java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String A
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: invokestatic  #27                 // Method kotlin/collections/ArraysKt.toMutableList:([Ljava/lang/Object;)Ljava/util/List;
      10: astore_2
      11: aload_2
      12: invokeinterface #33,  1           // InterfaceMethod java/util/List.size:()I
      17: iconst_1
      18: if_icmple     219
      21: iconst_0
      22: istore_3
      23: iconst_1
      24: istore        4
      26: aload_2
      27: checkcast     #35                 // class java/lang/Iterable
      30: invokestatic  #41                 // Method kotlin/collections/CollectionsKt.withIndex:(Ljava/lang/Iterable;)Ljava/lang/Iterable;
      33: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      38: astore        5
      40: aload         5
      42: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      47: ifeq          158
      50: aload         5
      52: invokeinterface #55,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      57: checkcast     #57                 // class kotlin/collections/IndexedValue
      60: astore        6
      62: aload         6
      64: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
      67: iconst_1
      68: iadd
      69: istore        7
      71: aload_2
      72: invokestatic  #64                 // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
      75: istore        8
      77: iload         7
      79: iload         8
      81: if_icmpgt     40
      84: aload_0
      85: aload         6
      87: invokevirtual #67                 // Method kotlin/collections/IndexedValue.getValue:()Ljava/lang/Object;
      90: checkcast     #69                 // class java/lang/String
      93: aload_2
      94: iload         7
      96: invokeinterface #73,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     101: checkcast     #69                 // class java/lang/String
     104: invokespecial #77                 // Method getOverlapping:(Ljava/lang/String;Ljava/lang/String;)I
     107: aload_0
     108: aload_2
     109: iload_3
     110: invokeinterface #73,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     115: checkcast     #69                 // class java/lang/String
     118: aload_2
     119: iload         4
     121: invokeinterface #73,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     126: checkcast     #69                 // class java/lang/String
     129: invokespecial #77                 // Method getOverlapping:(Ljava/lang/String;Ljava/lang/String;)I
     132: if_icmple     145
     135: aload         6
     137: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
     140: istore_3
     141: iload         7
     143: istore        4
     145: iload         7
     147: iload         8
     149: if_icmpeq     40
     152: iinc          7, 1
     155: goto          84
     158: aload_2
     159: iload_3
     160: invokeinterface #73,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     165: checkcast     #69                 // class java/lang/String
     168: astore        5
     170: aload_2
     171: iload         4
     173: invokeinterface #73,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     178: checkcast     #69                 // class java/lang/String
     181: astore        6
     183: aload_2
     184: aload         5
     186: invokeinterface #81,  2           // InterfaceMethod java/util/List.remove:(Ljava/lang/Object;)Z
     191: pop
     192: aload_2
     193: aload         6
     195: invokeinterface #81,  2           // InterfaceMethod java/util/List.remove:(Ljava/lang/Object;)Z
     200: pop
     201: aload_2
     202: aload_0
     203: aload         5
     205: aload         6
     207: invokespecial #85                 // Method getOverlappingString:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
     210: invokeinterface #88,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     215: pop
     216: goto          11
     219: aload_2
     220: invokestatic  #92                 // Method kotlin/collections/CollectionsKt.single:(Ljava/util/List;)Ljava/lang/Object;
     223: checkcast     #69                 // class java/lang/String
     226: areturn

  private final int getOverlapping(java.lang.String, java.lang.String);
    Code:
       0: aload_1
       1: invokevirtual #107                // Method java/lang/String.length:()I
       4: aload_2
       5: invokevirtual #107                // Method java/lang/String.length:()I
       8: iadd
       9: aload_0
      10: aload_1
      11: aload_2
      12: invokespecial #85                 // Method getOverlappingString:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
      15: invokevirtual #107                // Method java/lang/String.length:()I
      18: isub
      19: ireturn

  private final java.lang.String getOverlappingString(java.lang.String, java.lang.String);
    Code:
       0: ldc           #109                // String
       2: astore_3
       3: aload_1
       4: checkcast     #111                // class java/lang/CharSequence
       7: invokestatic  #116                // Method kotlin/text/StringsKt.withIndex:(Ljava/lang/CharSequence;)Ljava/lang/Iterable;
      10: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      15: astore        4
      17: aload         4
      19: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      24: ifeq          168
      27: aload         4
      29: invokeinterface #55,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      34: checkcast     #57                 // class kotlin/collections/IndexedValue
      37: astore        5
      39: aload         5
      41: invokevirtual #67                 // Method kotlin/collections/IndexedValue.getValue:()Ljava/lang/Object;
      44: checkcast     #118                // class java/lang/Character
      47: invokevirtual #122                // Method java/lang/Character.charValue:()C
      50: aload_2
      51: iconst_0
      52: invokevirtual #126                // Method java/lang/String.charAt:(I)C
      55: if_icmpeq     61
      58: goto          17
      61: aload         5
      63: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
      66: istore        6
      68: aload_1
      69: checkcast     #111                // class java/lang/CharSequence
      72: invokestatic  #129                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
      75: istore        7
      77: iload         6
      79: iload         7
      81: if_icmpgt     121
      84: aload_1
      85: iload         6
      87: invokevirtual #126                // Method java/lang/String.charAt:(I)C
      90: aload_2
      91: iload         6
      93: aload         5
      95: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
      98: isub
      99: invokevirtual #126                // Method java/lang/String.charAt:(I)C
     102: if_icmpeq     108
     105: goto          17
     108: iload         6
     110: iload         7
     112: if_icmpeq     121
     115: iinc          6, 1
     118: goto          84
     121: new           #131                // class java/lang/StringBuilder
     124: dup
     125: invokespecial #132                // Method java/lang/StringBuilder."<init>":()V
     128: aload_1
     129: invokevirtual #136                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     132: aload_2
     133: new           #138                // class kotlin/ranges/IntRange
     136: dup
     137: aload_1
     138: invokevirtual #107                // Method java/lang/String.length:()I
     141: aload         5
     143: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
     146: isub
     147: aload_2
     148: checkcast     #111                // class java/lang/CharSequence
     151: invokestatic  #129                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
     154: invokespecial #141                // Method kotlin/ranges/IntRange."<init>":(II)V
     157: invokestatic  #145                // Method kotlin/text/StringsKt.substring:(Ljava/lang/String;Lkotlin/ranges/IntRange;)Ljava/lang/String;
     160: invokevirtual #136                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     163: invokevirtual #149                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     166: astore_3
     167: nop
     168: aload_2
     169: checkcast     #111                // class java/lang/CharSequence
     172: invokestatic  #116                // Method kotlin/text/StringsKt.withIndex:(Ljava/lang/CharSequence;)Ljava/lang/Iterable;
     175: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     180: astore        4
     182: aload         4
     184: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     189: ifeq          358
     192: aload         4
     194: invokeinterface #55,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     199: checkcast     #57                 // class kotlin/collections/IndexedValue
     202: astore        5
     204: aload         5
     206: invokevirtual #67                 // Method kotlin/collections/IndexedValue.getValue:()Ljava/lang/Object;
     209: checkcast     #118                // class java/lang/Character
     212: invokevirtual #122                // Method java/lang/Character.charValue:()C
     215: aload_1
     216: iconst_0
     217: invokevirtual #126                // Method java/lang/String.charAt:(I)C
     220: if_icmpeq     226
     223: goto          182
     226: aload         5
     228: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
     231: istore        6
     233: aload_2
     234: checkcast     #111                // class java/lang/CharSequence
     237: invokestatic  #129                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
     240: istore        7
     242: iload         6
     244: iload         7
     246: if_icmpgt     286
     249: aload_2
     250: iload         6
     252: invokevirtual #126                // Method java/lang/String.charAt:(I)C
     255: aload_1
     256: iload         6
     258: aload         5
     260: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
     263: isub
     264: invokevirtual #126                // Method java/lang/String.charAt:(I)C
     267: if_icmpeq     273
     270: goto          182
     273: iload         6
     275: iload         7
     277: if_icmpeq     286
     280: iinc          6, 1
     283: goto          249
     286: new           #131                // class java/lang/StringBuilder
     289: dup
     290: invokespecial #132                // Method java/lang/StringBuilder."<init>":()V
     293: aload_2
     294: invokevirtual #136                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     297: aload_1
     298: new           #138                // class kotlin/ranges/IntRange
     301: dup
     302: aload_2
     303: invokevirtual #107                // Method java/lang/String.length:()I
     306: aload         5
     308: invokevirtual #60                 // Method kotlin/collections/IndexedValue.getIndex:()I
     311: isub
     312: aload_1
     313: checkcast     #111                // class java/lang/CharSequence
     316: invokestatic  #129                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
     319: invokespecial #141                // Method kotlin/ranges/IntRange."<init>":(II)V
     322: invokestatic  #145                // Method kotlin/text/StringsKt.substring:(Ljava/lang/String;Lkotlin/ranges/IntRange;)Ljava/lang/String;
     325: invokevirtual #136                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     328: invokevirtual #149                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     331: astore        6
     333: aload         6
     335: invokevirtual #107                // Method java/lang/String.length:()I
     338: aload_3
     339: invokevirtual #107                // Method java/lang/String.length:()I
     342: if_icmplt     354
     345: aload_3
     346: ldc           #109                // String
     348: invokestatic  #153                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     351: ifeq          357
     354: aload         6
     356: astore_3
     357: nop
     358: aload_3
     359: ldc           #109                // String
     361: invokestatic  #153                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     364: ifeq          386
     367: new           #131                // class java/lang/StringBuilder
     370: dup
     371: invokespecial #132                // Method java/lang/StringBuilder."<init>":()V
     374: aload_1
     375: invokevirtual #136                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     378: aload_2
     379: invokevirtual #136                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     382: invokevirtual #149                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     385: astore_3
     386: aload_3
     387: areturn
}

Compiled from "FindTheShortestSuperstring.kt"
public final class com.eric.leetcode.FindTheShortestSuperstringKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_4
       7: anewarray     #17                 // class java/lang/String
      10: astore_2
      11: aload_2
      12: iconst_0
      13: ldc           #19                 // String sssv
      15: aastore
      16: aload_2
      17: iconst_1
      18: ldc           #21                 // String svq
      20: aastore
      21: aload_2
      22: iconst_2
      23: ldc           #23                 // String dskss
      25: aastore
      26: aload_2
      27: iconst_3
      28: ldc           #25                 // String sksss
      30: aastore
      31: aload_2
      32: astore_1
      33: new           #27                 // class com/eric/leetcode/FindTheShortestSuperstring
      36: dup
      37: invokespecial #31                 // Method com/eric/leetcode/FindTheShortestSuperstring."<init>":()V
      40: astore_2
      41: aload_2
      42: aload_1
      43: invokevirtual #35                 // Method com/eric/leetcode/FindTheShortestSuperstring.shortestSuperstring:([Ljava/lang/String;)Ljava/lang/String;
      46: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
      49: swap
      50: invokevirtual #47                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      53: return
}


package com.eric.leetcode

import java.util.HashMap

/**
 * 暴力解法，DFS
 */
//class ArithmeticSlicesIISubsequence {
//
//    var num = 0
//
//
//    fun numberOfArithmeticSlices(A: IntArray): Int {
//        num = 0
//        for (index in A.indices) {
//            dfs(A, index, Long.MIN_VALUE, 1)
//        }
//        return num
//    }
//
//    private fun dfs(A: IntArray, s: Int, gap: Long, arraySize: Int) {
//        if (gap == Long.MIN_VALUE) {
//            for (index in s + 1..A.lastIndex) {
//
//                dfs(A, index, A[index].toLong() - A[s].toLong(), arraySize + 1)
//            }
//        } else {
//            for (index in s + 1..A.lastIndex) {
//                if (gap != A[index].toLong() - A[s].toLong()) {
//                    continue
//                } else {
//                    if (arraySize >= 2) {
//                        num++
//                    }
//                }
//                dfs(A, index, gap, arraySize + 1)
//            }
//        }
//    }
//}

class ArithmeticSlicesIISubsequence {

    fun numberOfArithmeticSlices(A: IntArray): Int {
        var result = 0
        val cnt = Array(A.size, { mutableMapOf<Int, Int>()})
        for (i in A.indices) {
            for (j in 0 until i) {
                val delta = A[i].toLong() - A[j].toLong()
                if (delta < Integer.MIN_VALUE || delta > Integer.MAX_VALUE) {
                    continue
                }
                val diff = delta.toInt()
                val sum = cnt[j].getOrDefault(diff, 0)
                val origin = cnt[i].getOrDefault(diff, 0)
                cnt[i].put(delta.toInt(), origin + sum + 1)
                result += sum
            }
        }
        return result
    }
}

fun main(args: Array<String>) {
    val input = intArrayOf(2,2,3,3,4,5)
    val arithmeticSlicesIISubsequence = ArithmeticSlicesIISubsequence()
    println(arithmeticSlicesIISubsequence.numberOfArithmeticSlices(input))
}

Compiled from "ArithmeticSlicesIISubsequence.kt"
public final class com.eric.leetcode.ArithmeticSlicesIISubsequence {
  public com.eric.leetcode.ArithmeticSlicesIISubsequence();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int numberOfArithmeticSlices(int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String A
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: iconst_0
       9: istore        4
      11: aload_1
      12: arraylength
      13: istore        5
      15: iload         5
      17: anewarray     #23                 // class java/util/Map
      20: astore        6
      22: iload         4
      24: iload         5
      26: if_icmpge     54
      29: iload         4
      31: istore        7
      33: aload         6
      35: iload         7
      37: new           #25                 // class java/util/LinkedHashMap
      40: dup
      41: invokespecial #26                 // Method java/util/LinkedHashMap."<init>":()V
      44: checkcast     #23                 // class java/util/Map
      47: aastore
      48: iinc          4, 1
      51: goto          22
      54: aload         6
      56: astore_3
      57: iconst_0
      58: istore        4
      60: aload_1
      61: arraylength
      62: istore        5
      64: iload         4
      66: iload         5
      68: if_icmpge     215
      71: iconst_0
      72: istore        6
      74: iload         6
      76: iload         4
      78: if_icmpge     209
      81: aload_1
      82: iload         4
      84: iaload
      85: i2l
      86: aload_1
      87: iload         6
      89: iaload
      90: i2l
      91: lsub
      92: lstore        7
      94: lload         7
      96: ldc2_w        #27                 // long -2147483648l
      99: lcmp
     100: iflt          112
     103: lload         7
     105: ldc2_w        #29                 // long 2147483647l
     108: lcmp
     109: ifle          115
     112: goto          203
     115: lload         7
     117: l2i
     118: istore        9
     120: aload_3
     121: iload         6
     123: aaload
     124: iload         9
     126: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     129: iconst_0
     130: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     133: invokeinterface #40,  3           // InterfaceMethod java/util/Map.getOrDefault:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     138: checkcast     #42                 // class java/lang/Number
     141: invokevirtual #46                 // Method java/lang/Number.intValue:()I
     144: istore        10
     146: aload_3
     147: iload         4
     149: aaload
     150: iload         9
     152: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     155: iconst_0
     156: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     159: invokeinterface #40,  3           // InterfaceMethod java/util/Map.getOrDefault:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     164: checkcast     #42                 // class java/lang/Number
     167: invokevirtual #46                 // Method java/lang/Number.intValue:()I
     170: istore        11
     172: aload_3
     173: iload         4
     175: aaload
     176: lload         7
     178: l2i
     179: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     182: iload         11
     184: iload         10
     186: iadd
     187: iconst_1
     188: iadd
     189: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     192: invokeinterface #49,  3           // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     197: pop
     198: iload_2
     199: iload         10
     201: iadd
     202: istore_2
     203: iinc          6, 1
     206: goto          74
     209: iinc          4, 1
     212: goto          64
     215: iload_2
     216: ireturn
}

Compiled from "ArithmeticSlicesIISubsequence.kt"
public final class com.eric.leetcode.ArithmeticSlicesIISubsequenceKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        6
       8: newarray       int
      10: astore_2
      11: aload_2
      12: iconst_0
      13: iconst_2
      14: iastore
      15: aload_2
      16: iconst_1
      17: iconst_2
      18: iastore
      19: aload_2
      20: iconst_2
      21: iconst_3
      22: iastore
      23: aload_2
      24: iconst_3
      25: iconst_3
      26: iastore
      27: aload_2
      28: iconst_4
      29: iconst_4
      30: iastore
      31: aload_2
      32: iconst_5
      33: iconst_5
      34: iastore
      35: aload_2
      36: astore_1
      37: new           #17                 // class com/eric/leetcode/ArithmeticSlicesIISubsequence
      40: dup
      41: invokespecial #21                 // Method com/eric/leetcode/ArithmeticSlicesIISubsequence."<init>":()V
      44: astore_2
      45: aload_2
      46: aload_1
      47: invokevirtual #25                 // Method com/eric/leetcode/ArithmeticSlicesIISubsequence.numberOfArithmeticSlices:([I)I
      50: istore_3
      51: getstatic     #31                 // Field java/lang/System.out:Ljava/io/PrintStream;
      54: iload_3
      55: invokevirtual #37                 // Method java/io/PrintStream.println:(I)V
      58: return
}